如何在PHP中发出异步HTTP请求PHP中是否有一种进行异步HTTP调用的方法?我不在乎你的反应,我只想做点什么file_get_contents(),但不要等到请求完成后才执行其余的代码。这对于启动应用程序中的“事件”或触发长进程非常有用。有什么想法吗?
3 回答
元芳怎么了
TA贡献1798条经验 获得超7个赞
function post_without_wait($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);
$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);}
九州编程
TA贡献1785条经验 获得超4个赞
php通过curl打开long task.php(这里没有魔法) php关闭连接并继续(魔术!) 当连接关闭时,curl返回到Quick.php 这两个任务同时进行。
while(ob_get_level()) ob_end_clean();header('Connection: close');ignore_user_abort();ob_start();
echo('Connection Closed');$size = ob_get_length();header("Content-Length: $size");ob_end_flush();flush();- 3 回答
- 0 关注
- 1705 浏览
添加回答
举报
0/150
提交
取消
