4 methods for implementing asynchronous calls in PHP and 4 methods for php asynchronization _ PHP Tutorial

Source: Internet
Author: User
Describes four methods for implementing asynchronous calls in PHP and four asynchronous php methods. 4 methods for implementing asynchronous calls in PHP and 4 methods for connecting and communicating between php asynchronous browsers and servers through the HTTP protocol. This is a request-and response-model-based protocol that describes four methods for implementing asynchronous calls in PHP and four methods for asynchronous calls in 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 wayIn the HTML code returned to the client, embed an AJAX call or 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 a pipeline pointing to a process, which is generated by executing 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.
The above are four php methods for implementing asynchronous calls. I hope this will be helpful for your learning.

Articles you may be interested in:
  • Research and sharing of PHP Asynchronous call methods
  • PHP asynchronous socket call implementation code
  • C # Benefits and methods of asynchronous calling
  • Resolve the problem that responseXML is null when webservice is asynchronously called
  • This problem occurs when php uses an Asynchronous call to obtain data (the error c00ce56e causes this operation to fail)
  • C # Three call examples of delegation (synchronous call Asynchronous call asynchronous callback)
  • Reasons why global variables cannot be assigned in jquery ajax Asynchronous call methods and solutions
  • Php Asynchronous call method implementation example
  • JAVA implements Asynchronous call of instance code

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

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.