Php Asynchronous call introduction: This is a detailed page of php asynchronous calls. it introduces php, related knowledge, skills, experience, and some php source code.
Class = 'pingjiaf' frameborder = '0' src = 'http: // biancheng.dnbc?info/pingjia.php? Id = 329776 'Rolling = 'no'>
The browser communicates with the server over HTTP. This is a request and response model-based protocol. The browser initiates a request to the server through a URL. the Web server receives the request, executes a program, responds to the request, and sends the corresponding html code to the client.
This poses a problem. The Web server may be able to execute a program within several milliseconds or minutes. If the program runs slowly and the user may not be patient, the browser will be closed.
Sometimes, we don't care about the returned results of these time-consuming scripts, but we still have to wait for them to execute the returned results before continuing the next step.
Is there any way to simply trigger and call these time-consuming scripts and then proceed to the next step so that these time-consuming scripts can be executed slowly on the server?
After testing, we have summarized several methods to share with you:
1. the simplest method is to embed an AJAX call in the HTML code returned to the client, or embed an img tag. src points to the time-consuming script to be executed.
This method is the simplest and fastest. The server does not need to make any calls.
However, the disadvantage is that Ajax should be triggered after onLoad. that is to say, when the user clicks the page and closes it, the background script will not be triggered.
Using the img label cannot be called a strictly asynchronous execution. The user's browser will wait for a long time until the php script is executed, that is, the status bar of the user's browser is still being loaded.
Of course, you can also use other similar methods, such as script labels.
2. popen ()
Resource popen (string command, string mode );
// Open an Pipeline pointing to a process. this process is generated by the execution of the derived command. Open an Pipeline pointing to a process, which is generated by running the command derived from a process.
Therefore, 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 fast. However, the problem is that this method cannot request another WebService through the HTTP protocol and can only execute local script files. In addition, it can only be opened in one way, and a large number of parameters cannot be used to call scripts.
In addition, if the traffic is high, a large number of processes will be generated. If external resources are used, you must consider competition on your own.
3. use CURL
In this method, set CUROPT_TIMEOUT to 1 (minimum: 1, depressing ). That is to say, 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);
4. use fsockopen
This method should be perfect, but the disadvantage is that you need to spell out the HTTP header.
$ Fp = fsockopen ("www.example.com", 80, $ errno, $ errstr, 30); if (! $ Fp) {echo "$ errstr ($ errno)
\ N ";} else {$ out =" GET/backend. php/HTTP/1.1 \ r \ n "; $ out. = "Host: www.example.com \ r \ n"; $ out. = "Connection: Close \ r \ n"; fwrite ($ fp, $ out);/* ignore the execution result while (! Feof ($ fp) {echo fgets ($ fp, 128);} */fclose ($ fp );}