Asynchronous execution of scripts in PHP language_PHP tutorial

WBOY
Release: 2016-07-13 09:56:42
Original
1142 people have browsed it

PHP language implements asynchronous execution of scripts

In PHP language, you need to use the fsockopen() function to run the script asynchronously. The code is as follows

//Asynchronous request function (if the debug parameter is true, it is used for debugging. If you enable debugging, you can see the asynchronous execution, but the asynchronous effect will be lost)

main.php

<!--?php
function request_by_fsockopen($url,$post_data=array(),$debug=false){
    $url_array = parse_url($url);
    $hostname = $url_array[&#39;host&#39;];
    $port = isset($url_array[&#39;port&#39;])? $url_array[&#39;port&#39;] : 80;
    @$requestPath = $url_array[&#39;path&#39;] ."?". $url_array[&#39;query&#39;];
    $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);//必须做url转码以防模拟post提交的数据中有&符而导致post参数键值对紊乱
        }
        $_post = implode(&#39;&&#39;, $_post);
        $header .= "Content-Type: application/x-www-form-urlencoded\r\n";//POST数据
        $header .= "Content-Length: ". strlen($_post) ."\r\n";//POST数据的长度
        $header.="Connection: Close\r\n\r\n";//长连接关闭
        $header .= $_post; //传递POST数据
    }else{
        $header.="Connection: Close\r\n\r\n";//长连接关闭
    }
    fwrite($fp, $header);
    //-----------------调试代码区间-----------------
    //注如果开启下面的注释,异步将不生效可是方便调试
    if($debug){
    $html = &#39;&#39;;
    while (!feof($fp)) {
    $html.=fgets($fp);
    }
    echo $html;
    }
    //-----------------调试代码区间-----------------
    fclose($fp);
}

$data=array(&#39;name&#39;=>&#39;guoyu&#39;,&#39;pwd&#39;=>&#39;123456&#39;);
$url=&#39;http://localhost/test/other.php&#39;;
request_by_fsockopen($url,$data,true);//
Copy after login

other.php

<!--?php
header("content-type:text/html;charset=utf-8");
//error_reporting(0);
//ini_set(&#39;html_errors&#39;,false);
//ini_set(&#39;display_errors&#39;,false);
$name = isset($_POST[&#39;name&#39;])?$_POST[&#39;name&#39;]:&#39;&#39;;
$pwd = isset($_POST[&#39;pwd&#39;])?$_POST[&#39;pwd&#39;]:&#39;&#39;;
echo $name.$pwd;
echo &#39;success ok&#39;;
die;
?-->
Copy after login


Usage example:
[main.php main script file to run]
$data=array('name'=>'guoyu','pwd'=>'123456');
$url='http://localhost/test/other.php';
request_by_fsockopen($url,$data,true);//Asynchronously-synchronize the user table of application B

[Guide execution file other.php]
In other.php, you can use $_POST to receive the parameters submitted by main.php to proceed with the next step

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/986702.htmlTechArticlePHP language implements asynchronous execution of scripts. PHP language must use the fsockopen() function to implement asynchronous running of scripts. The code is as follows// Asynchronous request function (if the debug parameter is true, it is used for debugging. You can enable debugging...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!