Code for asynchronous data calling using phpfscokopen-PHP source code

Source: Internet
Author: User
Ec (2); phpfscokopen implements asynchronous data call code. We can use fsockopen to connect to the local server, trigger script execution, and then return immediately without waiting for the script to be executed. & Nbsp; functiontriggerRequest ($ url, $ post_dataarray (), $ cookiearray () & hellip ;{& nbsp; & nbsp; & script ec (2); script

Php fscokopen implements asynchronous data call code

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

Script Execution is complete.

Function triggerRequest ($ url,
$ Post_data
=
Array (),
$ Cookie
=
Array ())... {
$ Method
=
"GET"; // you can pass some parameters to the script to be triggered through POST or GET.

$ Url_array
=
Parse_url ($ url); // obtain 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
"; // The HTTP 1.1 Host domain cannot be omitted

/** // The header fields below 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"; // The length of the POST Data

$ Post_str
. =
$ _ Post. "rnrn"; // pass POST Data

$ Header
. =
$ Post_str;
}
Fwrite ($ fp,
$ Header );
// Echo fread ($ fp, 1024); // we don't care whether the server returns

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 proceed to the next step.

Another problem is when the client is disconnected. That is, after triggerRequest sends a request,

If the connection is closed immediately, the script executed by the server may exit.

In PHP, the system maintains the connection status. There are three possible states:

* 0-NORMAL (NORMAL)

* 1-ABORTED (exit unexpectedly)

* 2-TIMEOUT (TIMEOUT)

When the PHP script runs normally, the connection is valid. When the client breaks the connection,

The label of the ABORTED status will be opened. The interruption of remote client connection is usually caused by the user clicking the STOP button.

. When the connection time exceeds the PHP time limit (see the set_time_limit () function ),

Status flag will be opened.

You can determine whether the script needs to exit when the client terminates the connection. Sometimes the complete running of the script will bring about

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

The script will exit. This process can be performed by php. ini's ignore_user_abort or by Apache

"Php_value ignore_user_abort" and ignore_user_abort () corresponding to. conf settings

Number to control. If PHP is not told to ignore user interruption, the script will be interrupted unless

Register_shutdown_function () sets to disable the trigger function. Disable the trigger function.

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

Disable the trigger function.

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

By setting max_execution_time or Apache. conf of php. ini

The parameter "php_valuemax_execution_time" or the set_time_limit () function is used to change the value. When the counter

When timeout occurs, the script will exit when the above connection is interrupted, and the previously registered function will be disabled.

It will also be executed at this time. In this function, you can call the connection_status () function.

To check whether timeout causes the function to be called. If timeout causes function calls to be disabled, this function

2 is returned.

Note that the ABORTED and TIMEOUT statuses are valid at the same time. This tells PHP to ignore

The user may exit the operation. PHP will still note that the user has interrupted the connection, but the script is still running

Status. If the running time limit is reached, the script will be exited, And the set function to close and trigger will also be executed.

The connection_status () function returns 3.

Therefore, specify the following in the script to be triggered:


Ignore_user_abort (TRUE); // if the client is disconnected, it will not cause the script abort.
Set_time_limit (0); // cancels the upper limit of script execution latency

Alternatively, you can use:
Register_shutdown_function (callback fuction [, parameters]); // when the script is registered and exited

Executed Function

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.