PHP Asynchronous call method research, php Asynchronous call _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Research on Asynchronous call methods implemented by PHP, and asynchronous call implemented by php. PHP implements Asynchronous call method research. php implements Asynchronous call between the browser and the server through the HTTP protocol for connection communication. This is a request and response model-based protocol. Research on Asynchronous call methods implemented by using PHP, and asynchronous call implemented by php

The browser communicates with the server over HTTP. This is a request and response model-based protocol. The browser initiates a request to the server through a URL. the Web server receives the request, executes a program, responds to the request, and sends the corresponding html code to the client.

This poses a problem. The Web server may be able to execute a program within several milliseconds or minutes. If the program runs slowly and the user may not be patient, the browser will be closed.

Sometimes, we don't care about the returned results of these time-consuming scripts, but we still have to wait for them to execute the returned results before continuing the next step.
Is there any way to simply trigger and call these time-consuming scripts and then proceed to the next step so that these time-consuming scripts can be executed slowly on the server?

After testing, we have summarized several methods to share with you:


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.

2. popen ()

resource popen ( string command, string mode );

// Open an Pipeline pointing to a process. this process is generated by the execution of the derived command. 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.

pclose(popen("/home/xinchen/backend.php &", 'r'));

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, if the traffic is high, a large number of processes will be generated. If external resources are used, you must consider competition on your own.

3. use CURL
In this method, set CUROPT_TIMEOUT to 1 (minimum: 1, depressing ). That is to say, the client must wait at least 1 second.

    $ch = curl_init();         $curl_opt = array(CURLOPT_URL, 'http://www.example.com/backend.php',                                CURLOPT_RETURNTRANSFER, 1,                                CURLOPT_TIMEOUT, 1,);         curl_setopt_array($ch, $curl_opt);         curl_exec($ch);         curl_close($ch);

4. use fsockopen
This method should be perfect, but the disadvantage is that you need to spell out the HTTP header.

$ Fp = fsockopen ("www.example.com", 80, $ errno, $ errstr, 30); if (! $ Fp) {echo "$ errstr ($ errno)
\ N ";} else {$ out =" GET/backend. php/HTTP/1.1 \ r \ n "; $ out. = "Host: www.example.com \ r \ n"; $ out. = "Connection: Close \ r \ n"; fwrite ($ fp, $ out);/* ignore the execution result 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.

Author: Laruence
Address: http://www.laruence.com/2008/04/14/318.html

The ghost browser communicates with the server over HTTP. This is a request and response model-based protocol. ...

Related Article

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.