Using PHP for asynchronous HTTP requestsFebruary 06, 2012 Reviews N/A
JavaScript/Ajax
This article describes the use PHP
of asynchronous HTTP requests that can be easily implemented using asynchronous HTTP requests. The so-called asynchronous HTTP request means that the HTTP protocol is based on TCP and is state-based, and the client and server make a connection after the send request needs to wait until the server processing is finished and return before the connection can be disconnected. In some cases, theclient side only needs to make its own request, do not need to know the server side of the response , this time it is necessary to implement the client side issued an asynchronous HTTP request. Also, in a long time-consuming application (the requested server-side task is more time-consuming than HTTP timeout or even longer), consider using asynchronous HTTP requests to start the task. You can also refer to this article for long time-consuming applications.
Method 1: Use Curl's
CURLOPT_TIMEOUT
Or
CURLOPT_TIMEOUT_MS
Set CURLOPT_TIMEOUT
to the minimum value 1
, the client side is returned after waiting 1 seconds.
$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);
If it is curl 7.16.2 or higher and PHP 5.2.3 or above, you can set timeout time to 1 MS, implement immediate return, modify as curl_setopt($ch, CURLOPT_TIMEOUT, 1);
above curl_setopt($ch, CURLOPT_TIMEOUT_MS, 1);
.
Method 2: Modify the HTTP header using the socket
Use the socket to connect to the server, send the raw HTTP header (note Connection: Close
the settings), close the socket immediately after completion without waiting for the server to respond and then return.
Get Example
The server URL that needs to be requested is, the http://example.com/Default.aspx
accepted parameter is, action=start
method is GET
, the cookie that needs to be carried ASP.NET_SessionId=zfyaimqgtt1bfiewq0najgah
. (This information can be obtained using httpwatch analysis).
For example, the HTTP request for the client side of the HttpWatch analysis is:
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
Modify to asynchronous HTTP request:
<?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 Example
The server URL that needs to be requested is, the http://example.com/Login.aspx
accepted parameter is, username=my-username&password=my-password
method is POST
, the cookie that needs to be carried ASP.NET_SessionId=zfyaimqgtt1bfiewq0najgah
. (This information can be obtained using httpwatch analysis).
For example, the HTTP request for the client side of the HttpWatch analysis is:
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
Modify to asynchronous HTTP request:
<?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
Reference:
- http://www.paul-norman.co.uk/2009/06/asynchronous-curl-requests/
- Http://petewarden.typepad.com/searchbrowser/2008/06/how-to-post-an.html
- http://stackoverflow.com/questions/962915/how-do-i-make-an-asynchronous-get-request-in-php
Transferred from: http://54min.com/post/php-asynchronous-http-request.html
PHP implements asynchronous requests