PHP Implementation send Asynchronous Request method

Source: Internet
Author: User
This article is mainly to share with you the PHP implementation of sending asynchronous request method, recently encountered in the work of the need to use the PHP asynchronous request, so on the Internet to find the relevant information. After several tests and revisions, two commonly-available scenarios are summed up:

1. Scenario One: Use curl, but you must set Curopt_timeout to 1.

function _curl ($url, $data =null, $timeout =0, $isProxy =false) {$curl = Curl_init ();   if ($isProxy) {//whether to set proxy $proxy = "127.0.0.1";   Proxy IP $proxyport = "8001";    Proxy Port curl_setopt ($curl, Curlopt_proxy, $proxy. ":". $proxyport);    } curl_setopt ($curl, Curlopt_url, $url);    curl_setopt ($curl, Curlopt_ssl_verifypeer, FALSE);    curl_setopt ($curl, Curlopt_ssl_verifyhost, FALSE);        if (!empty ($data)) {curl_setopt ($curl, Curlopt_post, 1);        curl_setopt ($curl, Curlopt_postfields, $data); curl_setopt ($curl, Curlopt_httpheader, Array ("Cache-control:no-cache", "content-type:appl    Ication/json ",));    } curl_setopt ($curl, Curlopt_returntransfer, TRUE);    if ($timeout > 0) {//time-out seconds curl_setopt ($curl, Curlopt_timeout, $timeout);    } $output = Curl_exec ($curl);    $error = Curl_errno ($curl);    Curl_close ($curl);    if ($error) {return false; } return $output;} _curl (' Http://localhoSt/index.php ', null,1); 


2, scenario two: use Fsockopen, but need to spell out the header part of HTTP yourself

function _fsockopen ($url, $post _data=array (), $cookie =array ()) {$url _arr = Parse_url ($url); $port = isset ($url _arr[' Port '])? $url _arr[' Port ']:80;if ($url _arr[' scheme '] = = ' https ') {$url _arr[' host '] = ' ssl://'. $url _arr[' host ';} $fp = Fsockopen ($url _arr[' host '), $port, $errno, $errstr, (), if (! $fp) return false; $getPath = isset ($url _arr[' path ')? $ url_arr[' path ': '/index.php '; $getPath. = Isset ($url _arr[' query '])? '? '.  $url _arr[' query ': '; $method = ' GET '; The default get mode if (!empty ($post _data)) $method = ' post '; $header = "$method $getPath http/1.1\r\n"; $header. = "Host:" $url _arr[ ' Host ']. " \ r \ n "; if (!empty ($cookie)) {//pass cookie information $_cookie = Strval (NULL); foreach ($cookie as $k = = $v) {$_cookie. = $k." = ". $v."; ";} $cookie _str = "Cookie:". Base64_encode ($_cookie). " \ r \ n "; $header. = $cookie _str;} if (!empty ($post _data)) {//pass post Data $_post = Array (); foreach ($post _data as $_k=>$_v) {$_post[] = $_k. " = ". UrlEncode ($_v);} $_post = Implode (' & ', $_post); $post _str = "content-type:application/x-www-form-urlencoded; CharSet=utf-8\r\n "; $post _str. =" Content-length: ". strlen ($_post)."  \ r \ n ";  Data length $post_str. = "connection:close\r\n\r\n"; $post _str. = $_post; Pass the post data $header. = $post _str;} else{$header. = "connection:close\r\n\r\n";} Fwrite ($fp, $header);//echo fread ($fp, 1024x768); usleep (1000); This sentence is also the key, if there is no such delay, may not be executed on the Nginx server Success fclose ($FP); return true;} _fsockopen (' http://localhost/index.php '));

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.