Home > Backend Development > PHP Problem > What is the asynchronous calling method in php

What is the asynchronous calling method in php

coldplay.xixi
Release: 2023-03-04 18:32:01
Original
2769 people have browsed it

PHP asynchronous calling method: 1. In the HTML code returned to the client, embed an AJAX call, or embed an img tag, src points to the time-consuming script to be executed; 2. Use the popen function to open a link to A pipe for the process spawned by the execution of the given command.

What is the asynchronous calling method in php

[Related learning recommendations: php programming (video)]

1. The most The simple way is to embed the 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, you can also use other methods with similar principles, such as script tags and so on.

2. popen()

resource popen ( string command, string mode );
Copy after login

//Open a pipe pointing to the process resulting from the execution of the given command command. Opens a pipe to the process spawned by execution of the command that spawned the given command.

So you can call it but ignore its output.

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

This method avoids the shortcomings 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 the CURL

method and set CUROPT_TIMEOUT to 1 (the minimum is 1, depressed). 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. Using 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

Related learning recommendations: Programming video

The above is the detailed content of What is the asynchronous calling method in php. For more information, please follow other related articles on the PHP Chinese website!

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