PHP Language Implementation script asynchronous execution

Source: Internet
Author: User
Tags php language

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

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.