Detailed explanation of the 4 methods of asynchronous calls in PHP, detailed explanation of the 4 types of asynchronous calls in PHP_PHP Tutorial

WBOY
Release: 2016-07-12 08:57:10
Original
1459 people have browsed it

Detailed explanation of the 4 methods of asynchronous calls in PHP, detailed explanation of the 4 types of asynchronous calls in PHP

The connection and communication between the browser and the server is through the HTTP protocol. This is a protocol based on a request and response model. The browser initiates a request to the server through the URL. The Web server receives the request, executes a program, and then responds by sending the corresponding HTML code to the client.

There is a problem. When the web server executes a program, it may be completed in a few milliseconds, or it may not be completed in several minutes. If the program executes slowly, the user may not have the patience to wait any longer and close the browser.

Sometimes, we don’t even care about the return results of these time-consuming scripts, but we have to wait for them to finish executing and return before we can continue to the next step.
So is there any way to simply trigger the call of these time-consuming scripts and then continue to the next step, so that these time-consuming scripts can be executed slowly on the server side?

After testing, I have summarized several methods and share them with you:

1. The simplest way is to embed an AJAX call in the HTML code returned to the client, or embed an img tag with src pointing to the time-consuming script to be executed.
This method is the simplest and fastest. The server does not need to make any calls.
But the disadvantage is that generally speaking, Ajax should be triggered after onLoad. That is to say, if the user clicks on the page and then closes it, our background script will not be triggered.
If you use the img tag, this method cannot be called asynchronous execution in the strict sense. The user's browser will wait for a long time for the execution of the php script to be completed, that is, the status bar of the user's browser always shows that it is still loading.
Of course, other methods with similar principles can also be used, such as script tags and so on.

2. popen()

resource popen ( string command, string mode );
//打开一个指向进程的管道,该进程由派生给定的 command 命令执行而产生。打开一个指向进程的管道,该进程由派生给定的 command 命令执行而产生。

Copy after login

So you can call it but ignore its output.

pclose(popen("/home/xinchen/backend.php &", 'r'));
This method avoids the disadvantages of the first method and is also fast. But the problem is that this method cannot request another WebService through the HTTP protocol and can only execute local script files. And it can only be opened in one direction, and cannot pass a large number of parameters to the called script.
And if the number of visits is high, a large number of processes will be generated. If you use external resources, you have to consider the competition yourself.

3. Use CURL
This method sets CUROPT_TIMEOUT to 1 (minimum is 1, depressing). That is, the client must wait at least 1 second.

  $ch = curl_init();
   
  $curl_opt = array(CURLOPT_URL, 'http://www.example.com/backend.php',
                CURLOPT_RETURNTRANSFER, 1,
                CURLOPT_TIMEOUT, 1,);
   
  curl_setopt_array($ch, $curl_opt);
   
  curl_exec($ch);
   
  curl_close($ch);
Copy after login

4. Use fsockopen
This method should be the most perfect, but the disadvantage is that you need to spell out the HTTP header part yourself.

  $fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
  if (!$fp) {
    echo "$errstr ($errno)<br />\n";
  } else {
    $out = "GET /backend.php / HTTP/1.1\r\n";
    $out .= "Host: www.example.com\r\n";
    $out .= "Connection: Close\r\n\r\n";
   
    fwrite($fp, $out);
    /*忽略执行结果
  while (!feof($fp)) {
  echo fgets($fp, 128);
  }*/
    fclose($fp);
  }
Copy after login

So, overall, the best and simplest method is the first method.
The most perfect one should be the last one, but it is more complicated.
The above are 4 ways to implement asynchronous calls in PHP. I hope it will be helpful to everyone's learning.

Articles you may be interested in:

  • Research and sharing of PHP asynchronous calling methods
  • PHP asynchronous calling socket implementation code
  • Benefits of C# asynchronous calling Share with methods
  • Solution to the problem that asynchronous call to webservice returns empty responseXML
  • Solve the problem that occurs when php uses asynchronous call to obtain data (error c00ce56e causes this operation to be unable to be completed)
  • Three calling examples of C# delegates (synchronous calls, asynchronous calls, asynchronous callbacks)
  • A brief analysis of the reasons why global variables cannot be assigned values ​​in the jquery ajax asynchronous calling method and the solutions
  • php asynchronous calling method Implementation example
  • JAVA implementation of asynchronous call example code

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1110080.htmlTechArticleDetailed explanation of the 4 methods of asynchronous calls in PHP, detailed explanation of the 4 types of php asynchronous calls between the browser and the server through HTTP protocol for connection communication. This is a protocol based on the request and response model...
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