The implementation of a Web server script may be a few milliseconds to complete, may also have a few minutes Wanbu Cheng. If the slow implementation of the program, the user may not have the patience to wait on the closure of the browser.
And sometimes, we do not care about them this time of the implementation of the outcome of the script, but they still have to wait for his return to the End, continues to be the next step.
Then there any way, just a simple call to trigger these time-consuming script and then continue on the next step, so that these time-consuming script in the implementation of the server slowly?
Next, I will use fscokopen to achieve this function.
PHP supports socket programming is fsockopen, done in the past CMS, I have used it had sent a letter smtp.
fscokopen return to a remote host to connect to the handle. You can use fopen like to return to the handle, she wrote fwrite, read fgets, fread, and other operations.
Our asynchronous PHP, the main effect is desired, to trigger a PHP script, and then immediately return it to stay in the implementation of the server-side slowly. I also wrote a previous article discussed the issue.
In that case, we will be able to use fsockopen to connect to the local server, triggering the implementation of the script, and then return immediately and not wait for the completion of the implementation of the script.
function triggerRequest ($ url, $ post_data = array (), $ cookie = array ())...{
$ method = \ "GET \"; / / via POST or GET parameters to pass to trigger the script
$ url_array = parse_url ($ url); / / obtain the URL information, in order to level up HTTP HEADER
$ port = isset ($ url_array [ 'port'])? $ url_array [ 'port']: 80;
$ fp = fsockopen ($ url_array [ 'host'], $ port, $ errno, $ errstr, 30);
if (! $ fp) ... (
return FALSE;
)
$ getPath = $ url_array [ 'path']. \ "? \". $ url_array [ 'query'];
if (! empty ($ post_data ))...{
$ method = \ "POST \";
)
$ header = $ method. \ "\". $ getPath;
$ header .= \ "HTTP/1.1 \ \ r \ \ n \";
$ header .= \ "Host: \". $ url_array [ 'host']. \ "\ \ r \ \ n \"; / / HTTP 1.1 Host domain can not be omitted
/**//* The following information in the first domain can be omitted
$ header .= \ "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv: 1.8.1.13) Gecko/20080311 Firefox/2.0.0.13 \ \ r \ \ n \";
$ header .= \ "Accept: text / xml, application / xml, application / xhtml + xml, text / html; q = 0.9, text / plain; q = 0.8, image / png, q = 0.5 \ \ r \ \ n \ ";
$ header .= \ "Accept-Language: en-us, en; q = 0.5 \";
$ header .= \ "Accept-Encoding: gzip, deflate \ \ r \ \ n \";
* /
$ header .= \ "Connection: Close \ \ r \ \ n \"; [Page]
if (! empty ($ cookie ))...{
$ _cookie = strval (NULL);
foreach ($ cookie as $ k => $ v )...{
$ _cookie .= $ k. \ "= \". $ v. \ "; \";
)
$ cookie_str = \ "Cookie: \". base64_encode ($ _cookie). \ "\ \ r \ \ n \"; / / transmission Cookie
$ header .= $ cookie_str;
)
if (! empty ($ post_data ))...{
$ _post = strval (NULL);
foreach ($ post_data as $ k => $ v )...{
$ _post .= $ k. \ "= \". $ v. \ "& \";
)
$ post_str = \ "Content-Type: application / x-www-form-urlencoded \ \ r \ \ n \"; / / POST data
$ post_str .= \ "Content-Length: \". strlen ($ _post). \ "\ \ r \ \ n \"; / / POST data on the length of the
$ post_str .= $ _post. \ "\ \ r \ \ n \ \ r \ \ n \"; / / transmission of data POST
$ header .= $ post_str;
)
fwrite ($ fp, $ header);
/ / echo fread ($ fp, 1024); / / We do not care about the server to return
fclose ($ fp);
return true;
)
Now they can, through this function to trigger a PHP script implementation, and then will return to function. We can then implement the next step.
Another problem is that when the client later disconnected. TriggerRequest request is sent immediately after the closure of the connection, may lead to the implementation of the correct server from the script.
PHP in the internal system to maintain a connection, the state has three possible scenarios:
* 0 - NORMAL (normal)
* 1 - ABORTED (from abnormal)
* 2 - TIMEOUT (overtime)
When the PHP script is running normally NORMAL state, the connection is valid. When the client when disconnected, ABORTED state of the tag will be open. Remote client connections are often interrupted by the user clicks lead to the STOP button. When connecting more than the time limit for PHP (see set_time_limit () function) when, TIMEOUT state of the tag will be open. [Page]
The script can decide whether the client needs to break out of connection. Sometimes the script so that a full run will bring a lot easier, even in the absence of long-range browser to accept the script output. The default is that when the remote client to connect when the script will be suspended from. The process can be ignore_user_abort the php.ini or Apache. Conf set in the corresponding "php_value ignore_user_abort", as well as ignore_user_abort () function to control. If you did not tell users to ignore the interruption of PHP, the script will be interrupted unless through register_shutdown_function () function to trigger the closure of the set. With a shutdown function, when the user clicks on a remote STOP button, the script once again try to export data, PHP will detect the connection has been interrupted, and the shutdown function is called.
Script may be built-in script timer interruption. The default time limit of 30 seconds. This value can be set max_execution_time the php.ini or Apache. Conf set in the corresponding "php_value max_execution_time" parameters or set_time_limit () function to change. When the time-out counter, similar to the script will connect more than interruptions from the previous registration had been triggered by the closure of the function will be executed at this time. In the closure of the trigger function, can call connection_status () function to check whether the overtime led to the closure of the trigger function is called. If overtime led to the closure of the call trigger function, the function will return 2.
Need to pay attention to the fact that ABORTED and TIMEOUT state at the same time effectively. This neglected to tell PHP from the user's operation is possible. PHP will remain suspended for the attention of users to connect, but the script is still running. If the running time limit, will be from the script, set-off triggered by the closure function will be to implement. In this function will find that connection_status () to return 3.
It is also necessary to trigger the script specified:
ignore_user_abort (TRUE); / / If the client disconnected, the script will not give rise to abort.
set_time_limit (0); / / abolish the upper limit or delay implementation of the script, you can use: register_shutdown_function (callback fuction [, parameters ]);// registration from the script when the implementation of the function