PHP language to usefsockopen () function, which implements the script to run asynchronously, the code is as follows
Asynchronous request function (with debug parameter if True is used for debugging, open debugging can see asynchronous execution, but lose the effect of async)
main.php
<?phpfunction Request_by_fsockopen ($url, $post _data=array (), $debug =false) {$url _array = Parse_url ($url); $hostname = $url _array[' host ']; $port = isset ($url _array[' Port ')? $url _array[' Port ']: 80; @ $requestPath = $url _array[' path ']. "?". $url _array[' query ']; $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);//must do URL transcoding in case the data in the analog post submission has a & character resulting in the post parameter key value pair disorder} $_post = Implode (' & ', $_post); $header. = "content-type:application/x-www-form-urlencoded\r\n";//post data $header. = "Content-length:". Strlen ($_post). " \ r \ n "; The length of the//post data $header. =" connection:close\r\n\r\n ";Long connection close $header. = $_post; Pass the Post data}else{$header. = "connection:close\r\n\r\n";//Long Connection Close} fwrite ($fp, $header); -----------------Debug Code Interval-----------------//Note If the following comment is turned on, async will not take effect but it is convenient to debug if ($debug) {$html = '; while (!feof ($fp)) {$html. =fgets ($FP); } Echo $html; }//-----------------debug code interval-----------------fclose ($fp);} $data =array (' name ' = ' Guoyu ', ' pwd ' = ' 123456 '); $url = ' http://localhost/test/other.php '; request_by_fsockopen ($url, $data, true);//
other.php
<?phpheader ("Content-type:text/html;charset=utf-8");//error_reporting (0);//ini_set (' html_errors ', false);// Ini_set (' display_errors ', false); $name = isset ($_post[' name '])? $_post[' name ']: '; $pwd = isset ($_post[' pwd ')? $_post [' pwd ']: '; Echo $name. $pwd; Echo ' success ok ';d ie;? >
usage Examples:
[Running main.php main script file]
$data =array (' name ' = ' Guoyu ', ' pwd ' = ' 123456 ');
$url = ' http://localhost/test/other.php ';
Request_by_fsockopen ($url, $data, true);//Use the user table of application B asynchronously-synchronize data
[lead execution file other.php]
in other.php, you can use $_post to receive main.php submitted parameters for next steps .
PHP Language Implementation script asynchronous execution