异步执行的php
$arr=array(
'user'=>'test',
'password'=>'123',
'data'=>time()
);
$url = 'http://wp.tjin.pw/post.php';
// $this->sock_post($url,$arr)
//;
$this->request_by_fsockopen($url,$arr);
echo "1231111";
return json(array("SUCCESS"),201);
function request_by_fsockopen($url,$post_data=array()){
$url_array = parse_url($url);
$hostname = $url_array['host'];
$port = isset($url_array['port'])? $url_array['port'] : 80;
$q = isset( $url_array['query']) ? $url_array['query'] : '';
$requestPath = $url_array['path'] ."?".$q;
$fp = fsockopen($hostname, $port, $errno, $errstr, 10);
if (!$fp) {
echo "$errstr ($errno)";
return false;
}
$method = "GET";
if(!empty($post_data)){
$method = "POST";
}
$header = "$method $requestPath HTTP/1.1\r\n";
$header.="Host: $hostname\r\n";
if(!empty($post_data)){
$_post = strval(NULL);
foreach($post_data as $k => $v){
$_post[]= $k."=".urlencode($v);//必须做url转码以防模拟post提交的数据中有&符而导致post参数键值对紊乱
}
$_post = implode('&', $_post);
$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; //传递POST数据
}else{
$header.="Connection: Close\r\n\r\n";//长连接关闭
}
fwrite($fp, $header);
//-----------------调试代码区间-----------------
/*$html = '';
while (!feof($fp)) {
$html.=fgets($fp);
}
echo $html;*/
//-----------------调试代码区间-----------------
fclose($fp);
}异步调用 不需要返回结果的php
$post = $_POST;
sleep(15);
file_put_contents("header.txt",'header'.date('Y-m-d H:i:s')."\r\n".json_encode($post));本文为Jsky原创文章,转载无需和我联系,但请注明来自Jsky博客 www.tjin.link
