php asynchronous request

By rendering the front-end page and executing Ajax using js, this method is still applicable. It is limited to business scenarios, because it can only be called in browsers, and it will not work if interface requests are encountered.

Open a pipeline to the process, and each request will start a process. You can call it by, but ignore its output.
This method avoids the disadvantages of the first method and is fast. However, the problem is that this method cannot request another web service through HTTP protocol, and can only execute local script files. It can only be opened in one direction and cannot pass a large number of parameters to the called script.
And if the traffic is high, a large number of processes will be generated. If you use external resources, you have to consider competition yourself.

pclose(popen("/home/xinchen/backend.php &", 'r'));

  3. Use CURL extension
Set CUROPT_TIMEOUT is 1 (minimum 1, depressed). That is, the client must wait at least 1 second. This method will actively disconnect, but if the called service does connection detection, it will also interrupt the execution of the server script. If we request a time-consuming interface (20s) of wechat, we call 1s to disconnect. Whether wechat will maintain the request execution for 20s is uncontrollable. So this method is not recommended.

$ch = curl_init();
$curl_opt = array(CURLOPT_URL, '',
curl_setopt_array($ch, $curl_opt);

CURL extension has supported millisecond configuration and will curlopt_ Change timeout to CURLOPT_TIMEOUT_MS will take effect
CURL extension supports concurrency. We can access N interfaces at a time, and the execution time is the longest interface. For example, we can visit Jingdong payment (1s), WeChat payment (1.2s) and Alipay (0.8s) for three different services at a time. The total time consuming is 1.2s. Detailed usage curl_multi_init

4. Use fsockopen
It will actively disconnect, and the connection will be disconnected.

$fp = fsockopen("", 80, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
    $out = "GET /backend.php  / HTTP/1.1\r\n";
    $out .= "Host:\r\n";
    $out .= "Connection: Close\r\n\r\n";
    fwrite($fp, $out);
    /*Ignore execution results
    while (!feof($fp)) {
        echo fgets($fp, 128);


function asyncRequest($url, $param = array(), $method = 'get', $port = 80, $timeout = 30)

    $errno = 0;
    $errstr = '';
    $info = parse_url($url);
    if (isset($param) && !empty($param)) {
        $query = trim(http_build_query($param));//Array to url string
    } else {
        $query = null;
    //If the user's $URL“ "; Missing final backslash
    if (!isset($info['path']) || empty($info['path'])) {
        $info['path'] = "/index.html";
    $fp = fsockopen($info['host'], $port, $errno, $errstr, $timeout);
    if (!$fp) {
        return "connection failed";
    if ($errno || !$fp) {
        return $errstr;
    stream_set_blocking($fp, 0);//Non blocking
    stream_set_timeout($fp, 1);//Corresponding timeout (s)

    //Notice the spaces in the header splice

    if ($method == 'post') {
        $header = "POST " . $info['path'] . " HTTP/1.1\r\n";
        $header .= "Host:" . $info['host'] . "\r\n";
        $header .= "Content-Length:" . strlen($query) . "\r\n";
        $header .= "Content-type:application/x-www-form-urlencoded\r\n";
        $header .= "connection:close\r\n\r\n";
        $header .= $query;
    } else {
        if (empty($query)) {
            $header = "GET " . $info['path'] . " HTTP/1.1\r\n";
        } else {
            $header = "GET " . $info['path'] . "?" . $query . " HTTP/1.0\r\n";
        $header .= "Host:" . $info['host'] . "\r\n";
        $header .= "connection:close\r\n\r\n";
    $result = @fputs($fp, $header);
    return $result;

Keywords: PHP thinkphp

Added by Wolf_22 on Sat, 18 Sep 2021 08:56:44 +0300