PHP Asynchronous invocation
The connection between the browser and the server is communicated through the HTTP protocol. This is a protocol based on the request and response model. The browser initiates a request to the server via a URL, the WEB server receives the request, executes a program, and responds, sending the appropriate HTML code to the client.
This is a problem, the WEB server executes a program, it may be a few milliseconds to complete, it may not be a few minutes to finish. If the program executes slowly, the user may not have the patience to wait until the browser is closed.
And sometimes, we don't care about the results of these time-consuming scripts, but we still have to wait until he finishes returning to continue the next step.
So is there any way to simply trigger calls to these time-consuming scripts and then move on to the next step and let these time-consuming scripts run slowly on the server?
After testing, summed up several methods, and everyone share:
1. The simplest approach is to embed an AJAX call in the HTML code returned to the client, or embed an IMG tag, which points to the time-consuming script to execute.
This method is the simplest and fastest. The server side does not have to make any calls.
However, the disadvantage is that, in general, Ajax should be triggered after the onload, that is, the user points to open the page, then close, it will not trigger our background script.
In the case of an IMG tag, this approach cannot be called a strict asynchronous execution. The user's browser waits for the execution of the PHP script to complete, that is, the status bar of the user's browser is always displayed in load.
Of course, you can also use other similar principles, such as script tags and so on.
2. Popen ()
Resource Popen (String command, string mode);
Opens a pipeline that points to a process that is generated by the execution of a derived command command. Opens a pipeline that points to a process that is generated by the execution of a derived command command.
So you can call it, but ignore its output.
Pclose (Popen ("/home/xinchen/backend.php &", ' R ')); This method avoids the disadvantage of the first method and is also very fast. However, the problem is that this method cannot request another webservice through the HTTP protocol, only local script files can be executed. And can only be opened one way, unable to wear a large number of parameters to the called script.
And if, when the traffic is very high, there will be a lot of process. If you use external resources, consider the competition yourself.
3. Using Curl
This method, set Curopt_timeout to 1 (minimum is 1, depressed). In other words, the client must wait at least 1 seconds.
$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. Using Fsockopen
This method should be the perfect one, but the downside is that you need to spell out the header part of HTTP yourself.
$fp = Fsockopen ("www.example.com", $errno, $errstr, +), 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\r\n"; Fwrite ($fp, $out); /* Ignore execution result while (!feof ($fp)) {echo fgets ($fp, 128);} * /fclose ($FP);}
?
?
Transferred from: http://www.laruence.com/2008/04/14/318.html