我目前使用 CURL 运行 PHP 脚本以将数据发送到另一台服务器,以运行可能需要一分钟才能运行的 PHP 脚本。 该服务器不返回任何数据。但是 CURL 请求仍然需要等待它完成,然后它加载原始页面的其余部分。我希望我的 PHP 脚本只将数据发送到另一台服务器,然后不等待答复。
所以我的问题是我应该如何解决这个问题?我读过 CURL 总是要等待。你有什么建议?
最佳答案
这可能是一个有用的起点,公然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);
}
关于php - 由于等待时间长,因此可以替代 CURL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2822870/