The following small series will bring you an article on php asynchronization: using fsockopencurl in php to implement functions similar to asynchronous processing. I think this is quite good. now I will share it with you and give you a reference. Let's take a look at PHP. from the mainstream, PHP is a process-oriented language. its biggest drawback is that it cannot implement multi-threaded management. its program is executed from start to end and all the way through logic, it is impossible to have branches. this is one of the reasons that limit php's development to a more advanced language in mainstream programming languages.
In PHP, we sometimes want to execute another operation while executing an operation. for example, when a user grabs a ticket, you do not want the user to queue up to connect to the database for query, judgment, and insertion, and then return the user results. In fact, we don't need to wait for a long time for the user. after the user submits the ticket, he can directly tell him that he has successfully obtained the ticket. as for various operations, just hand it over to the background for processing. Of course, in this case, we now use the message list to process the request submitted by each user in a message queue, telling the user that the request has been handled. after the user closes the Page happily, in fact, the backend still extracts requests from the message queue one by one for operations. In this article, we use a different method to perform operations in the background without waiting for users.
First, we need to create a request entry:
Submitted data
Submit to background
Tell the user that it is done.
Second, we need a background processing program. whether the user is online does not affect its operation:
Incoming data
Data processing
The question is, in the first code section, how can I "submit to the background "? We implement this function through a non-blocking request. That is, to create a url that can be accessed, run the second program on this url, request this url through a request, and then activate the second program to run automatically.
Next, let's look at the code:
// Function _ sock ($ url) {$ host = parse_url ($ url, PHP_URL_HOST); $ port = parse_url ($ url, PHP_URL_PORT ); $ port = $ port? $ Port: 80; $ scheme = parse_url ($ url, PHP_URL_SCHEME); $ path = parse_url ($ url, PHP_URL_PATH); $ query = parse_url ($ url, PHP_URL_QUERY ); if ($ query) $ path. = '? '. $ Query; if ($ scheme = 'https') {$ host = 'SSL ://'. $ host ;}$ fp = fsockopen ($ host, $ port, $ error_code, $ error_msg, 1); if (! $ Fp) {return array ('error _ code' => $ error_code, 'error _ msg '=> $ error_msg);} else {stream_set_blocking ($ fp, true ); // enable the non-blocking mode stream_set_timeout ($ fp, 1) in the manual; // Set the timeout value $ header = "GET $ path HTTP/1.1 \ r \ n "; $ header. = "Host: $ host \ r \ n"; $ header. = "Connection: close \ r \ n"; // disable fwrite ($ fp, $ header) for persistent connections; usleep (1000 ); // This sentence is also critical. without this delay, the fclose ($ fp) operation may fail on the nginx server ); return array ('error _ code' => 0 );}}We have created a function based on fsockopen, which uses fsockopen to access the url. However, when accessing the function, it does not require obtaining the content displayed by the url, but only sends an access request, close the access immediately after the request arrives. The advantage of this is that you no longer need to wait for the Accessed url to return reliable information, saving time. the execution time of this code is between 0.1-0.2 seconds. for common visitors, almost imperceptible. Therefore, you only need to call this function and the corresponding url. However, the data transmission section is not provided here. to transmit data, you only need to add the post content to the $ header.
In addition to fsockopen, curl can also achieve this effect. some hosts do not support fsockopen, so we can use curl.
function _curl($url) { $ch = curl_init(); curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch,CURLOPT_TIMEOUT,1); $result = curl_exec($ch); curl_close($ch); return $result;}The key of this code is to provide a Timeout, which is only one second. that is to say, curl will close the access after 1 second regardless of whether or not it receives the returned content, therefore, the execution data of this function is between 1.0-1.1 seconds. However, for users, if an application needs to process data, the wait time in one second is almost ignored. if you want to use a simpler and easier-to-understand code, you can select curl.