PHP asynchronous: use fsockopen curl in PHP to implement functions similar to asynchronous processing

From the mainstream point of view, PHP is a process-oriented language. Its biggest shortcoming is that it cannot realize multi-thread management. The execution of its program is all the way from the beginning to the end, according to the logic, and it is impossible to branch. This is a limitation. One of the reasons why php is developing towards a more advanced language in the mainstream programming language.

In PHP, sometimes we actually want to perform another operation while performing a certain operation. Let’s take a scenario: when users grab tickets, you don’t want users to queue up to connect to the database for query, judgment, Insert, and return user results when complete. In fact, we don't need the user to wait that long. After the user submits, just tell him that he has successfully grabbed the ticket. As for various operations, just leave it to the background to handle. Of course, we now use the message list to handle this situation, and store each request submitted by the user in a message queue, telling the user that it has been done, and after the user happily closes the page, in fact, the background is still sending messages one by one Take the request out of the queue for operation. Our article uses a heterogeneous method to realize that the operation runs in the background without the user waiting.

First, we need to create a request entry:

<?php

提交的数据

提交给后台

告诉用户已经搞定了

Second, we need a background handler that runs whether the user is online or not:

<?php

ignore_user_abort(true);
set_time_limit(0);

过来的数据
数据处理

Now the question is, in the first piece of code, how to "submit to background"? We do this through a non-blocking request. That is to create a url that can be accessed, run the second program at this url, and request this url through a request, thereby activating the second program to run automatically. Next, let's look directly at the code:

// 远程请求(不获取内容)函数
function _sock($url) {
  $host = parse_url($url,PHP_URL_HOST);
  $port = parse_url($url,PHP_URL_PORT);
  $port = $port ? $port : 80;
  $scheme = parse_url($url,PHP_URL_SCHEME);
  $path = parse_url($url,PHP_URL_PATH);
  $query = parse_url($url,PHP_URL_QUERY);
  if($query) $path .= '?'.$query;
  if($scheme == 'https') {
    $host = 'ssl://'.$host;
  }

  $fp = fsockopen($host,$port,$error_code,$error_msg,1);
  if(!$fp) {
    return array('error_code' => $error_code,'error_msg' => $error_msg);
  }
  else {
    stream_set_blocking($fp,true);//开启了手册上说的非阻塞模式
    stream_set_timeout($fp,1);//设置超时
    $header = "GET $path HTTP/1.1\r\n";
    $header.="Host: $host\r\n";
    $header.="Connection: close\r\n\r\n";//长连接关闭
    fwrite($fp, $header);
    usleep(1000); // 这一句也是关键,如果没有这延时,可能在nginx服务器上就无法执行成功
    fclose($fp);
    return array('error_code' => 0);
  }
}

We have created a function based on fsockopen, which uses fsockopen to access the url, but when accessing, it is not required to obtain the content displayed by the url, but only to send an access request, and close the access immediately after the request arrives. The advantage of this is that there is no need to wait for the visited URL to return reliable information, which saves time. The execution time of this code is between 0.1-0.2 seconds, which is almost imperceptible to ordinary visitors. Therefore, when using, you only need to call this function and the corresponding url. However, the part of data transmission is not provided here. How to transmit data, in fact, only need to add the content of post in $header.

In addition to fsockopen, curl can actually achieve this effect. Some hosts do not support fsockopen, so we can use curl to achieve it.

function _curl($url) {
  $ch = curl_init();
  curl_setopt($ch,CURLOPT_URL,$url);
  curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  curl_setopt($ch,CURLOPT_TIMEOUT,1);
  $result = curl_exec($ch);
  curl_close($ch);
  return $result;
}

The key to this code is to provide a Timeout, which is only 1 second, that is to say, when curl sends a request, no matter whether the returned content is received or not, the access will be closed after 1 second, so the execution data of this function is 1.0-1.1 between seconds. But for users, if it is an application that requires data processing, the 1-second wait is almost ignored. If you want to use a simpler and easier-to-understand code, you can choose curl to achieve it.
Collect and organize learning routes & notes icon-default.png?t=N4P3https://mp.weixin.qq.com/s/KQx_eIwdjCj3QdErxKb7ZQ
 

Guess you like

Origin blog.csdn.net/2301_77162959/article/details/130928135
Recommended