Describes common PHP-based asynchronous execution methods. 1. client pages use AJAX technology to request servers advantages: the simplest and fastest, namely, embedding AJAX calls in the HTML code returned to the client, or embedding an img tag,
1. client pages use AJAX technology to request servers
Advantages:The simplest and fastest way is to embed AJAX calls in the HTML code returned to the client, or embed an img tag. src points to the time-consuming script to be executed.
Disadvantages:In general, Ajax should be triggered after onLoad. that is to say, when the user clicks the page and closes it, our 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 () function
This function opens an Pipeline pointing to a process, which 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. The code is as follows:
The code is as follows:
Pclose (popen ("/home/xinchen/backend. php &", 'r '));
Advantages:This avoids the disadvantages of the first method and is fast.
Disadvantages:This method cannot request another WebService through HTTP, but 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. CURL extension
CURL is a powerful HTTP command line tool that can simulate POST/GET and other HTTP requests and then obtain and extract data, which is displayed on stdout. The code is as follows:
The code is as follows:
$ 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 );
Disadvantages:As described in your issue, you need to set CUROPT_TIMEOUT to 1 (minimum: 1, depressing) because CURL is used ). That is to say, the client must wait at least 1 second.
4. fscokopen () function
Fsockopen supports socket programming. you can use fsockopen to implement socket programs such as Mail sending. to use fcockopen, you need to manually splice the header part.
Example:
The code is as follows:
$ Fp = fsockopen ("www.34ways.com", 80, $ errno, $ errstr, 30 );
If (! $ Fp ){
Echo "$ errstr ($ errno)
\ N ";
} Else {
$ Out = "GET/index. php/HTTP/1.1 \ r \ n ";
$ Out. = "Host: www.34ways.com \ r \ n ";
$ Out. = "Connection: Close \ r \ n ";
Fwrite ($ fp, $ out );
/* Ignore execution results
While (! Feof ($ fp )){
Echo fgets ($ fp, 128 );
}*/
Fclose ($ fp );
}
To sum up, the fscokopen () function should meet your requirements. You can try it.
Signature advantages: the simplest and fastest, that is, embedding AJAX calls in the HTML code returned to the client, or embedding an img tag ,...