php实现异步请求

使用PHP进行异步HTTP请求

2012 年 02 月 06 日评论暂缺

使用JavaScript/Ajax可轻松实现异步HTTP请求,本文介绍使用PHP进行异 步HTTP请求。所谓异步HTTP请求是指:HTTP协议基于TCP且是基于状态的,client和server建立 连接后发送请求需要等到server处理结束并返回后才可以断开连接。某些情况下,client端只需要发出自己的请求即可,不需要知道 server端的响应,这个时候即需要实现client端发出异步HTTP请求。另外,在长耗时应用中(请求的server端任务比较 耗时,超过HTTP timeout时间甚至更长),也可以考虑使用异步HTTP请求出发该任务。关于长耗时应用也可以参考该文

方法1:使用curl的CURLOPT_TIMEOUTCURLOPT_TIMEOUT_MS

设置CURLOPT_TIMEOUT为最小值1,client端在等待1秒之后即返回。

    $url = "http://www.yoursite.com/background-script.php";
    $ref_url = "http://www.yoursite.com";
    $data = array(
        "key1" => "value1",
        "key2" => "value2",
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    curl_setopt($ch, CURLOPT_REFERER, $ref_url);

    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);        

    curl_setopt($ch, CURLOPT_TIMEOUT, 1);

    curl_exec($ch);
    curl_close($ch);

如果是cURL 7.16.2 or higher and PHP 5.2.3 or above,可以设置Timeout时间为1 ms,实现立即返回,修改如上的curl_setopt($ch, CURLOPT_TIMEOUT, 1);curl_setopt($ch, CURLOPT_TIMEOUT_MS, 1);

方法2:使用socket修改HTTP header

使用socket连接到server上,发送raw HTTP header(注意设置Connection: Close), 完成之后立即关闭socket不等待server做出响应再返回。

GET例子

需要请求的server url为http://example.com/Default.aspx,接受的参数为action=start,method 为GET,需要携带的cookies为ASP.NET_SessionId=zfyaimqgtt1bfiewq0najgah。(这 些信息都可以使用HttpWatch分析得到)。

例如HttpWatch的分析的client端的HTTP请求为:

    GET /Default.aspx?action=start HTTP/1.1
    Accept-Language: zh-cn
    User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727)
    Accept-Encoding: gzip, deflate
    Host: example.com
    Connection: Keep-Alive
    Cookie: ASP.NET_SessionId=zfyaimqgtt1bfiewq0najgah

修改为异步HTTP请求:

    <?php

    $host = "example.com";
    $path = "/Default.aspx?action=start";
    $cookie = "ASP.NET_SessionId=zfyaimqgtt1bfiewq0najgah";

    $start = microtime(true);

    $fp = fsockopen($host, 80, $errno, $errstr, 30);
    if (!$fp) {
       print "$errstr ($errno)<br />\n";
       exit;
    }
    $out = "GET ".$path." HTTP/1.1\r\n";
    $out .= "Host: ".$host."\r\n";          //需要注意Host不能包括`http://`,仅可以使用`example.com`
    $out .= "Connection: Close\r\n";
    $out .= "Cookie: ".$cookie."\r\n\r\n";

    fwrite($fp, $out);  //将请求写入socket

    /*
    //也可以选择获取server端的响应
    while (!feof($fp)) {
        echo fgets($fp, 128);
    }
    */

    //如果不等待server端响应直接关闭socket即可
    fclose($fp);

    $cost = microtime(true) - $start;
    print "\n".$cost."\n";
    exit;

POST例子

需要请求的server url为http://example.com/Login.aspx,接受的参数为username=my-username&password=my-password,method 为POST,需要携带的cookies为ASP.NET_SessionId=zfyaimqgtt1bfiewq0najgah。(这 些信息都可以使用HttpWatch分析得到)。

例如HttpWatch的分析的client端的HTTP请求为:

    POST /Login.aspx HTTP/1.1
    Accept-Language: zh-cn
    User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727)
    Content-Type: application/x-www-form-urlencoded
    Accept-Encoding: gzip, deflate
    Host: example.com
    Connection: Keep-Alive
    Cache-Control: no-cache
    Cookie: ASP.NET_SessionId=zfyaimqgtt1bfiewq0najgah

    username=my-username&password=my-password

修改为异步HTTP请求:

    <?php

    $host = "example.com";
    $path = "/Login.aspx";
    $cookie = "ASP.NET_SessionId=zfyaimqgtt1bfiewq0najgah";
    $params = "username=my-username&password=my-password";

    $start = microtime(true);

    $fp = fsockopen($host, 80, $errno, $errstr, 30);
    if (!$fp) {
       print "$errstr ($errno)<br />\n";
       exit;
    }
    $out = "POST ".$path." HTTP/1.1\r\n";
    $out .= "Host: ".$host."\r\n";
    $out .= "Connection: Close\r\n";
    $out .= "Cookie: ".$cookie."\r\n\r\n";
    $out .= $params;

    fwrite($fp, $out);  

    /*
    //也可以选择获取server端的响应
    while (!feof($fp)) {
        echo fgets($fp, 128);
    }
    */

    //如果不等待server端响应直接关闭socket即可
    fclose($fp);

    $cost = microtime(true) - $start;
    print "\n".$cost."\n";
    exit;

摘自:http://86er.sinaapp.com/?p=147

参考:

 

转自:http://54min.com/post/php-asynchronous-http-request.html

posted on 2016-01-18 10:38  法斯特  阅读(1642)  评论(0编辑  收藏  举报

导航