PHP Timing Planning task and fsockopen continuous process instance _php skills

Source: Internet
Author: User
Tags fread php script

The Web server executes a PHP script that can sometimes take a long time to return execution results, and the subsequent script will have to wait long to continue.
You can do this by using the Fscokopen function if you want to implement a simple execution of a time-consuming script without waiting for the result to perform the next step directly.

PHP supports socket programming, the Fscokopen function returns a handle to a remote host connection, which can be fwrite, fgets, Fread, and so on, like a handle returned using fopen.
Using Fsockopen to connect to the local server, triggering the script execution, and then returning immediately, without waiting for the script to complete, you can implement the PHP effect asynchronously.

Example:

Copy Code code as follows:

?
function Triggerrequest ($url, $post _data = Array (), $cookie = Array ()) {
$method = "Get"; Pass some arguments to the script to be triggered by post or get
$url _array = Parse_url ($url); Get URL Information
$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 cannot be omitted
/* The following header information field 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";
if (!empty ($cookie)) {
$_cookie = Strval (NULL);
foreach ($cookie as $k => $v) {
$_cookie. = $k. " = ". $v."; ";
}
$cookie _str = "Cookie:". Base64_encode ($_cookie). "\ r \ n"; Passing cookies
$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 _str. = "Content-length:". Strlen ($_post). "\ r \ n"; Length of post data
$post _str. = $_post. " \r\n\r\n "; Passing post data
$header. = $post _str;
}
Fwrite ($fp, $header);
Echo fread ($FP, 1024); Server returns
Fclose ($FP);
return true;
}

This allows the execution of a PHP script to be triggered by the fsockopen () function, and then the function returns. Then proceed to the next step.
Now there is a problem: when the client disconnects, that is, after the triggerrequest send the request, immediately shut down the connection, it may cause the server to execute the script exit

Within PHP, the system maintains a connection state with three possible conditions:

* 0–normal (normal)
* 1–aborted (Abnormal exit)
* 2–timeout (Timeout)

The connection is valid when the PHP script normally runs in normal state. When the client disconnects, the flag for the aborted state is opened. The interruption of a remote client connection is usually caused by the user clicking the STOP button. When the connection time exceeds the PHP limit (see the Set_time_limit () function), the TIMEOUT state's markup is opened.

You can determine whether the script needs to exit when the client disconnects. Sometimes it can be handy to have a script run completely, even if there is no remote browser to accept the output of the script. The default scenario is that the script exits when a remote client connection is interrupted. The process can be controlled by the php.ini Ignore_user_abort or by the corresponding "Php_value ignore_user_abort" and the Ignore_user_abort () functions in the Apache. conf settings. If you don't tell PHP to ignore the user's interruption, the script will be interrupted unless you pass register_shutdown_function (), Let's set up a second function that can be invoked when execution closes. That is, when our script executes or dies unexpectedly and the PHP execution is about to shut down, our function will be called, and when the remote user clicks the STOP button, the script tries to output the data again, PHP The connection is detected and the shutdown trigger function is invoked.

The script may also be interrupted by a built-in script timer. The default time-out limit is 30 seconds. This value can be changed by setting the corresponding "Php_value max_execution_time" parameter or the Set_time_limit () function in the php.ini max_execution_time or Apache. conf settings. When the counter times out, the script will be similar to the above connection interrupt exit, the previously registered shutdown trigger function will be executed at this time. In the shutdown trigger function, the connection_status () function can be called to check whether the timeout causes the shutdown trigger function to be invoked. If the timeout causes a call to close the triggering function, the function returns 2.

Note that the aborted and TIMEOUT states can be valid at the same time. This is possible when you tell PHP to ignore the user's exit operation. PHP will still be aware that the user has disconnected the connection but the script is still running. If the time limit is run, the script will be exited, and the set off trigger function will be executed. At this point the function connection_status () is found to return 3.

So it is also indicated in the script to be triggered:

Copy Code code as follows:

<?php
Ignore_user_abort (TRUE); If the client disconnects, it does not cause script abort
Set_time_limit (0); Cancel script Execution delay limit
or use:
<?php
Register_shutdown_function (callback fuction[, parameters]); function to execute when registering a script exit

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.