简体   繁体   English

使用cURL从PHP触发页面脚本的最快方法,不要等待响应

[英]Fastest way to trigger a script on a page from PHP using cURL, don't wait for a response

Right now I am using cURL and something like that: 现在我正在使用cURL之类的东西:

foreach ($urls as $url) {

    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url ); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_exec($ch);

}

to trigger a php script on a remote server. 在远程服务器上触发php脚本。

My thing is I just want to trigger that script and doesn't care at all what it returns and I want to trigger another address that is in my loop. 我的事情是我只想触发该脚本而不关心它返回什么,我想触发我循环中的另一个地址。

So, how can I eliminate waiting for the response and just trigger the scripts on the servers (I have about 200 urls in my array I need to loop through and trigger each of these urls). 那么,我怎样才能消除等待响应并只触发服务器上的脚本(我的数组中有大约200个url,我需要循环并触发每个URL)。

So, basically I want just to trigger the script and move to the next one and don't care what it returns. 所以,基本上我只想触发脚本并移动到下一个脚本而不关心它返回什么。

And another concern of mine is that if I can move the curl_init() outside of the loop like that: 我的另一个问题是,如果我可以将curl_init()移动到循环之外,就像那样:

$ch = curl_init(); 

foreach ($urls as $url) {

    curl_setopt($ch, CURLOPT_URL, $url ); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_exec($ch);

}

If there is a faster way how to achieve this without using cURL, please let me know. 如果有更快的方法如何在不使用cURL的情况下实现此目的,请告诉我。 I just need to trigger 100 scripts on remote servers from one loop inside one file. 我只需要从一个文件中的一个循环触发远程服务器上的100个脚本。

<?php
$fp = fsockopen("mesela.dom", 80, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
    $out = "GET / HTTP/1.1\r\n";
    $out .= "Host: mesela.dom\r\n";
    $out .= "Connection: Close\r\n\r\n";
    fwrite($fp, $out);
    fclose($fp);
}
?>

You can use a queue system with your code adding these URLs to be called as jobs and multiple workers doing the CURL calls. 您可以使用队列系统将您的代码添加到作为作业的多个工作人员和执行CURL调用的多个工作人员。

This will make your code asynchronous ( not wait for response from the curl call). 这将使您的代码异步(不等待curl调用的响应)。

A good PHP library you can use https://github.com/chrisboulton/php-resque 一个很好的PHP库,你可以使用https://github.com/chrisboulton/php-resque

curl multi 卷曲多

<?php
    function setcurloptions( $handle=false, $url=false, $cacert=false ){
        if( $handle && $url ){
            if( parse_url( $url, PHP_URL_SCHEME )=='https' ){
                curl_setopt( $handle, CURLOPT_SSL_VERIFYPEER, FALSE );
                curl_setopt( $handle, CURLOPT_SSL_VERIFYHOST, 2 );
                curl_setopt( $handle, CURLOPT_CAINFO, realpath( $cacert ) );
            }
            curl_setopt( $handle, CURLOPT_URL, $url );
            curl_setopt( $handle, CURLOPT_HEADER, false );
            curl_setopt( $handle, CURLOPT_FRESH_CONNECT, true );
            curl_setopt( $handle, CURLOPT_FORBID_REUSE, true );
            curl_setopt( $handle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1 );
            curl_setopt( $handle, CURLOPT_CLOSEPOLICY, CURLCLOSEPOLICY_OLDEST );
            curl_setopt( $handle, CURLOPT_BINARYTRANSFER, true );
            curl_setopt( $handle, CURLOPT_AUTOREFERER, true );
            curl_setopt( $handle, CURLOPT_CONNECTTIMEOUT, 30 );
            curl_setopt( $handle, CURLOPT_RETURNTRANSFER, true );
            curl_setopt( $handle, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT'] );
        }
    }


    $cacert='c:/wwwroot/cacert.pem';
    $urls=array(
        'http://www.example.com',
        'http://www.example.com',
        'http://www.example.com'
    );

    $multi = curl_multi_init();
    $handles = array();


    foreach( $urls as $i => $url ){
        $handle = curl_init();
        setcurloptions( $handle, $url, $cacert );
        curl_multi_add_handle( $multi, $handle );
        $handles[]=$handle;
    }


    $active=null;
    do {
        $mrc = curl_multi_exec( $multi, $active );
        usleep(100);
    } while( $mrc == CURLM_CALL_MULTI_PERFORM );


    while( $active && $mrc == CURLM_OK ) {
        if( curl_multi_select( $multi ) != -1 ) {
            do {
                $mrc = curl_multi_exec( $multi, $active );
            } while( $mrc == CURLM_CALL_MULTI_PERFORM );
        }
        usleep( 100 );
    }


    foreach( $handles as $i => $handle ){
        /*
        if you want to do something at all with response 
        $response=curl_multi_getcontent( $handle );
        */
        curl_multi_remove_handle( $multi, $handle );
        curl_close( $handle );
        usleep(100);
    }



    curl_multi_close( $multi );
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM