php异步请求模拟多进程

在A请求页面发起另一个B页面请求 不需要等待B页面执行结束再返回 直接往下执行A页面的请求

A页面代码

<?php
$url = 'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF'])."/send.php";
$param = array(
    'name'=>'fdipzone',
    'gender'=>'male',
    'age'=>30
);
doRequest($url, $param);//发起请求
function doRequest($url, $param=array())
{
    $urlinfo = parse_url($url);
    $host = $urlinfo['host'];
    $path = $urlinfo['path'];
    $query = isset($param)? http_build_query($param) : '';
    $port = 80;
    $errno = 0;
    $errstr = '';
    $timeout = 10;
    $fp = fsockopen($host, $port, $errno, $errstr, $timeout);
    $out = "POST ".$path." HTTP/1.1\r\n";
    $out .= "host:".$host."\r\n";
    $out .= "content-length:".strlen($query)."\r\n";
    $out .= "content-type:application/x-www-form-urlencoded\r\n";
    $out .= "connection:close\r\n\r\n";
    $out .= $query;
    fputs($fp, $out);
    fclose($fp);
}
?>

B页面代码

<?php
for($i=0;$i<10;$i++)
{
    sleep(1);
    file_put_contents(time().".txt",time());
}
?>

然后效果如图:

然后逐步增加了新文件

posted @ 2015-10-08 14:37  李照耀  阅读(388)  评论(0编辑  收藏  举报