PHP asynchronous execution implementation of four methods-PHP source code

Source: Internet
Author: User
There are many asynchronous execution methods. Here we have sorted out some common functions to Implement Asynchronous execution. At the same time, we are also familiar with ajax, of course, the focus of this article is not on ajax-related php functions. Let's take a look at many asynchronous execution methods. Here we have sorted out some common functions to Implement Asynchronous execution, I am also familiar with ajax. Of course, this article focuses on php functions related to ajax. Let's take a look at them.

Script ec (2); script

Common PHP asynchronous execution methods:

1. AJAX
1. The simplest method is to embed an AJAX call in the HTML code returned to the client, or embed an img tag. src points to the time-consuming script to be executed. This method is the simplest and fastest. The server does not need to make any calls.
However, the disadvantage is that Ajax should be triggered after onLoad. That is to say, when the user clicks the page and closes it, the background script will not be triggered. Using the img label cannot be called a strictly asynchronous execution. The user's browser will wait for a long time until the php script is executed, that is, the status bar of the user's browser is still being loaded. Of course, you can also use other similar methods, such as script labels.

The Code is as follows:

$. Ajax ({
Url: 'stat. php ',

Type: 'post ',

Data: {Name: "keyun "},

DataType: 'html ',

Timeout: 1000,

Error: function () {alert ('error loading PHP document ');},

Success: function (result) {alert (result );}

});

2. popen () function

Resource popen (string command, string mode );
Open an pipeline pointing to a process, which is generated by running the command derived from a process. Open an pipeline pointing to a process, which is generated by running the command derived from a process. Therefore, you can call it, but ignore its output.
This method avoids the disadvantages of the first method and is also very fast. However, the problem is that this method cannot request another WebService through the HTTP protocol and can only execute local script files. In addition, it can only be opened in one way, and a large number of parameters cannot be used to call scripts.
In addition, a large number of processes are generated when the traffic volume is high. If external resources are used, you must consider them carefully.

The Code is as follows:

Pclose (popen ("/home/xinchen/backend. php &", 'R '));


3. CURL Extension


CURL is a powerful HTTP command line tool that can simulate POST/GET and other HTTP requests and then obtain and extract data, which is displayed on the standard output (stdout ).
To use CURL, set CUROPT_TIMEOUT to 1 (minimum: 1, depressing ). That is to say, the client must wait at least 1 second.

The Code is as follows:

$ Ch = curl_init ();

$ Curl_opt = array (CURLOPT_URL, 'HTTP: // www.111cn.net ',
CURLOPT_RETURNTRANSFER, 1,
CURLOPT_TIMEOUT, 1 ,);

Curl_setopt_array ($ ch, $ curl_opt );

Curl_exec ($ ch );

Curl_close ($ ch );

4. fscokopen () function

Fsockopen is a very powerful function that supports socket programming. You can use fsockopen to implement socket programs such as email sending. To use fcockopen, You need to manually splice the header part.
Official documents: http://cn.php.net/fsockopen/

The Code is as follows:
$ Fp = fsockopen ("www.111cn.net", 80, $ errno, $ errstr, 30 );
If (! $ Fp ){
Echo "$ errstr ($ errno)
\ N ";
} Else {
$ Out = "GET/backend. php/HTTP/1.1 \ r \ n ";
$ Out. = "Host: www.111cn.net \ r \ n ";
$ Out. = "Connection: Close \ r \ n ";

Fwrite ($ fp, $ out );
/* Ignore execution results
While (! Feof ($ fp )){
Echo fgets ($ fp, 128 );
}*/
Fclose ($ fp );
}

Therefore, in general, it is best to use the first method.
The most perfect one is the last one, but it is complicated.
If you have a better solution, please contact us.

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.