您的位置:首页 > 其它

fsocket发送post实现异步请求

2015-08-04 18:33 483 查看
function triggerRequest($url, $post_data = array(), $cookie = array()){
//可以通过POST或者GET传递一些参数给要触发的脚本
$url_array = parse_url($url); //获取URL信息,以便平凑HTTP HEADER
$port = isset($url_array['port'])? $url_array['port'] : 80;

$fp = fsockopen($url_array['host'], $port, $errno, $errstr, 30);
if (!$fp){
return FALSE;
}

$getPath = $url_array['path'];
isset($url_array['query']) && $getPath.= "?". $url_array['query'];
$method = empty($post_data) ? "GET":"POST";

$header = $method . " " . $getPath;
$header .= " HTTP/1.1\r\n";
$header .= "Host: ". $url_array['host'] . "\r\n"; //HTTP 1.1 Host域不能省略

if(!empty($post_data)){
$_post = http_build_query($post_data);

$header   .= "Content-Type: application/x-www-form-urlencoded\r\n";//POST数据
$header   .= "Content-Length: ". strlen($_post) ." \r\n";//POST数据的长度
$header      .= "Connection:Close\r\n\r\n";

$header .= $_post."\r\n\r\n "; //传递POST数据
}

fwrite($fp, $header);
//echo fread($fp, 1024); //我们不关心服务器返回
fclose($fp);
return true;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: