CURL alternative due to long wait

Hi guys. I am currently running a PHP script using CURL to send data to another server to run a PHP script, which can take up to a minute. This server does not return data. But the CURL request should still wait for it to complete, and then load the rest of the orignal page. I would like my PHP script to simply send data to another server and then not wait for a response.

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

Thank!

+3
source share
5 answers

This may be a useful starting point, roughly copypasted from here

function curl_post_async($url, $params)
{
    foreach ($params as $key => &$val) {
      if (is_array($val)) $val = implode(',', $val);
        $post_params[] = $key.'='.urlencode($val);
    }
    $post_string = implode('&', $post_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);
}
+10
source

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); }
+3

, - PHP. , . , , .

+1

, php, script . "" , !

0

TIMEOUT .

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

C, .

-1

Source: https://habr.com/ru/post/1745182/


All Articles