PHP Asynchronous call implementation method the browser and the server communicate only with the connectionless HTTP protocol. connectionless programs are characterized by client requests to the server, the server outputs the corresponding program according to the request and cannot maintain a persistent connection. In this way, a problem occurs. a client calls PHP asynchronously.
PHP Asynchronous call implementation method
The browser and server communicate with each other over the connectionless HTTP protocol. connectionless programs are characterized by client requests to the server. the server outputs the corresponding program according to the request, persistent connections cannot be maintained.
In this case, a problem occurs. the corresponding server of a client may execute for 1 second or 1 minute, so that the browser will remain in the waiting state. if the program runs slowly, the user may be impatient to close the browser.
Sometimes, we do not need to care about the results of program execution, and there is no need to waste time and patience, so we have to figure out a way for the program to wait for silent execution in the background.
For example, in the current scenario, a recommendation email is sent to 1000 users. the user enters or imports the email account and submits it to the server for sending.
The user experience of this code is very poor and cannot be used in practice. first, sending so many emails will result in server operation timeout. In fact, a long waiting time will cause the user to doubt and lose confidence in the system products. However, you do not need to wait until all the 1000 emails are sent before submitting the email successfully. you can submit the email to the backend and then send the message to the user.
At this time, we need"Asynchronous execution"Technology to execute code, asynchronous execution is characterized by silent execution in the background, users do not have to wait for the code execution results, the advantages of using asynchronous execution:
1. get rid of the application's dependency on a single task
2. improved program execution efficiency
3. improved program scalability
4. improved user experience in certain scenarios
5. because PHP does not support multithreading, the Asynchronous call of multiple HTTP requests achieves parallel execution of the program. However, if there are too many HTTP requests, the system overhead will be greatly increased.
?
Common PHP asynchronous execution methods:
1. client pages use AJAX technology to request servers
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 () function
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 also very 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. 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.
$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);
To use CURL, set CUROPT_TIMEOUT to 1 (minimum: 1, depressing ). That is to say, the client must wait at least 1 second.
4. fscokopen () function
Fsockopen is a very powerful function that supports socket programming. you can use fsockopen to implement socket programs such as email sending. to use fcockopen, you need to manually splice the header part.
Official Document :? Http://cn.php.net/fsockopen/
?
$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\r\n" ; ?? ????? fwrite( $fp , $out ); ???? /* Ignore execution results ???? while (!feof($fp)) { ???????? echo fgets($fp, 128); ???? }*/ ???? fclose( $fp ); } |