开发者

Alternative to CURL due to long waiting

开发者 https://www.devze.com 2022-12-29 22:24 出处:网络
I currently run a PHP-script using CURL to send data to another server, to do run a PHP-script that could take up to a minute to run.

I currently run a PHP-script using CURL to send data to another server, to do run a PHP-script that could take up to a minute to run. This s开发者_Python百科erver doesn't give any data back. But the CURL-request still has to wait for it to complete, and then it loads the rest of the orignal page. I would like my PHP-script to just send the data to the other server and then not wait for an answer.

So my question is how should I solve this? I have read that CURL always has to wait. What are your suggestions?


This might be a useful starting point, flagrantly copypasted from here

function curl_post_async($url, $params)
{
    $post_string = http_build_query($params);

    $parts=parse_url($url);

    $fp = fsockopen($parts['host'], 
        isset($parts['port'])?$parts['port']:80, 
        $errno, $errstr, 30);

    //pete_assert(($fp!=0), "Couldn't open a socket to ".$url." (".$errstr.")");(optional)

    $out = "POST ".$parts['path']." HTTP/1.1\r\n";
    $out.= "Host: ".$parts['host']."\r\n";
    $out.= "Content-Type: application/x-www-form-urlencoded\r\n";
    $out.= "Content-Length: ".strlen($post_string)."\r\n";
    $out.= "Connection: Close\r\n\r\n";
    if (isset($post_string)) $out.= $post_string;

    fwrite($fp, $out);
    fclose($fp);
}


http://php.net/manual/en/function.fsockopen.php


$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)
\n"; } else { $out = "GET / HTTP/1.1\r\n"; $out .= "Host: www.example.com\r\n"; $out .= "Connection: Close\r\n\r\n"; fwrite($fp, $out); while (!feof($fp)) { echo fgets($fp, 128); } fclose($fp); }


You could create a server socket on the other machine that your PHP web page connects to. This way, you decide the protocol. Otherwise, look to see if a background process meets your needs.


I think that you can set the max execution time in php so it will stop the script working after the time limit. It's not a "pro" solution but it works !


It's simply better to use the TIMEOUT flag to exit within a time frame.

curl_easy_setopt(curl, CURLOPT_TIMEOUT, sec); // sec is int variable

That was C code, adapt yourself up.

0

精彩评论

暂无评论...
验证码 换一张
取 消