This article only as my learning process of a little experience, welcome everyone to shoot bricks.
PHP from the mainstream, is a process-oriented language, its biggest drawback is the inability to implement multi-threaded management, the execution of its program is from beginning to end, according to the logic all the way down, it is impossible to branch, this is to limit PHP in the mainstream programming language for the development of a more advanced language one of the reasons.
In PHP we sometimes actually want to perform an operation, at the same time to perform another operation, to give a scene: When the user rob the ticket, you do not want users to queue to connect to the database for query, judgment, insert, and then return to the user results. In fact, we do not need users to wait so long time, the user submitted after the direct tell him that he has robbed the ticket to succeed on it, as for various operations, to the backstage to deal with it good. Of course, this situation we are now using a message list processing, each user submitted the request exists in a message queue, told the user has been done, the user happy to close the page, in fact, the background is still in one from the message queue out of the request to operate. Our article is a kind of heterogeneous approach, to achieve operation in the background, without the user waiting.
First, we'll create a request entry:
Secondly, we need a background handler, whether the user is online or not, does not affect its operation:
The question now is, how do you "submit to the background" in the first piece of code? We implement this function through a non-blocking request. That is, create a URL that can be accessed, run the second program at this URL, request the URL via a request, and activate the second program to run automatically. Next we look directly at the code:
//remote request (do not get content) function functions _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);//Opens the manual for non-blocking mode stream_set_timeout ($FP, 1);//Set timeout $header = "GET $path HTT P/1.1\r\n "; $header. = "Host: $host \ r \ n"; $header. = "connection:close\r\n\r\n";//Long connection close fwrite ($fp, $header); Usleep (1000); This sentence is also the key, if there is no such delay, the Nginx server may not be able to perform a successful fclose ($FP); Return Array (' error_code ' = 0); }}
We have created a function based on Fsockopen, which uses Fsockopen to access the URL, but does not require the URL to be displayed at the time of the visit, but simply makes the access request and closes the access immediately after the request arrives. The advantage of this is that there is no need to wait for the URL to be accessed to return reliable information, saving time, and this code executes in between 0.1-0.2 seconds, almost imperceptible to ordinary visitors. Therefore, when used, you only need to call this function and the corresponding URL. However, there is no part of the data transmission, how to transfer data, in fact, only need to add the post content in the $header.
In addition to Fsockopen,curl can actually achieve this effect, some hosts do not support Fsockopen, we can use curl to achieve.
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 to this code is to provide a timeout, just 1 seconds, that is, curl makes a request, regardless of whether or not to receive the returned content, 1 seconds after the access is closed, so the execution data of this function is between 1.0-1.1 seconds. But for the user, if it is an application that needs data processing, the wait in 1 seconds is almost ignored, and if you want to use a simpler and easier to understand code, you can choose Curl.