views:

183

answers:

4

Hey Guys 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 server 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?

Thank You!

+1  A: 

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.

webbiedave
+1  A: 

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); }
mike clagg
+2  A: 

This might be a useful starting point, flagrantly 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.")");

    $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);
}
timdev
A: 

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 !

Michael