PHP fscokopen implement data asynchronous call code

Source: Internet
Author: User
Tags foreach php script php timeline valid

PHP fscokopen implement data asynchronous call code

We can use Fsockopen to connect to the local server, trigger script execution, and then return immediately without waiting

Script execution completed.

function Triggerrequest ($url,
$post _data
=
Array (),
$cookie
=
Array ()) ... {
$method
=
"Get"; You can pass some arguments to the script you want to trigger by post or get

$url _array
=
Parse_url ($url); Get the URL information to flatten the 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.1rn";
$header
.=
"Host:".
$url _array[' host '].
"RN
"; 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 RN ";
$header. = "Accept:

text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=

0.8,image/png,q=0.5 RN ";
$header. = "accept-language:en-us,en;q=0.5";
$header. = "Accept-encoding:gzip,deflatern";
*/

        $header
. =
"Connection:closern";
         if (!empty ($cookie)) ... {
                $_cookie
=
Strval (NULL);
                foreach ($cookie
as
$k
=>
$v) ... {
                         $_cookie
. =
$k. = ". $v."; ";
               }
                $cookie _str
=
Cookie:
.
Base64_encode ($_cookie). "RN";/Pass 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-URLENCODEDRN";//post data

$post _str
.=
"Content-length:".
Strlen ($_post). "RN"; Length of//post data

$post _str
.=
$_post. " Rnrn "; Passing post data

$header
.=
$post _str;
}
Fwrite ($FP,
$header);
Echo fread ($FP, 1024); We don't care about server return

Fclose ($FP);
Return
True
}

Now, you can use this function to trigger the execution of a PHP script, and then the function will return. We

You can then proceed to the next step.

Another problem is when the client disconnects. That is, after Triggerrequest sends the request,

Shutting down the connection immediately may cause the server to exit the script that is executing correctly.

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 of the aborted state will be opened. The interruption of a remote client connection is usually by the user clicking the STOP button

Lead to. When the connection time exceeds the PHP timeline (see the Set_time_limit () function), TIMEOUT

The flag of the state will be opened.

You can determine whether the script needs to exit when the client disconnects. Sometimes getting the script to run completely will bring

A lot of convenience, even if there is no remote browser to accept script output. The default scenario is when a remote client connection is interrupted

The script will exit. The process can be handled by php.ini Ignore_user_abort or by Apache

The corresponding "Php_value Ignore_user_abort" and Ignore_user_abort () letters in the Conf settings

Number to control. If you don't tell PHP to ignore the user's interruption, the script will be interrupted unless you pass

Register_shutdown_function () Sets the shutdown trigger function. By the Turn off trigger function, when the remote

When the user clicks the STOP button and the script tries to output the data again, PHP detects that the connection has been interrupted and

Use the Shutdown trigger function.

The script may also be interrupted by a built-in script timer. The default time-out limit is 30 seconds. This value can be

By setting the corresponding php.ini in the Max_execution_time or Apache. conf settings.

The "php_valuemax_execution_time" parameter or the Set_time_limit () function to change it. When the counter

Time out, the script will be similar to the above connection interruption of the exit, the previously registered shutdown trigger function

will also be executed at this time. In the shutdown trigger function, you can call the Connection_status () function

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 letter

Number will return 2.

One thing to note is that the aborted and TIMEOUT states can be valid at the same time. This tells PHP to ignore

The user's exit operation is possible. PHP will still note that the user has disconnected the connection but the script is still running

Situation 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:


Ignore_user_abort (TRUE); If the client disconnects, it does not cause script abort.
Set_time_limit (0)//Cancel script execution delay limit

Alternatively, you can also use:
Register_shutdown_function (callback fuction[, parameters])//When the registration script exits

function to execute

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.