PHP implementation of asynchronous data call method, _php tutorial

Source: Internet
Author: User
Tags mail account

PHP implementation of asynchronous data call method,


There is only one communication between the browser and the server for the non-connected HTTP protocol, the non-connected program is characterized by the client request service side, the service side returns the corresponding program according to the request, cannot maintain the persistent connection. This is a problem, a client's corresponding server may execute 1 seconds or 1 minutes, so that the browser will always be waiting, if the program is slow to execute, the user may not be patient to shut down the browser.
And sometimes we do not need to care about the results of the program implementation, there is no need to waste time and patience to wait, then we have to figure out a way to let the program wait in the background silently. For example, there is a scene, to send 1000 users a referral mail, the user input or import mail account to submit the server to perform the send.

This Code user experience is very poor, and can not actually use, the first to send so many messages will result in server run time-out, in fact, long user wait time will let users doubt and loss of confidence in the system products. However, the user does not have to wait until 1000 messages have been sent complete before the submission is successful, we can submit the background directly to the user prompt to send the success, and then let the background program silently sent.
This time we need "asynchronous execution" technology to execute code, asynchronous execution is characterized by the background of silent execution, the user does not have to wait for the result of code execution, using the benefits of asynchronous execution:

    • out of the application's dependency on a single task
    • improves program execution efficiency
    • Improved program extensibility
    • improved user experience in certain scenarios
    • because PHP does not support multi-threading, the use of asynchronous calls to request multiple HTTP in a way that achieves the effect of parallel execution of the program, but note that the requested HTTP too much, will greatly increase the system overhead

common way for asynchronous execution of PHP
1. Client page with Ajax technology request Server
The simplest way of
is to embed an AJAX call in the HTML code returned to the client, or embed an IMG tag that points to the time-consuming script to execute. This method is the simplest and fastest. The server side does not have to make any calls.
However, the downside is that Ajax should generally be triggered after onload, that is, when the user points the page off, it doesn't trigger our background script.
instead of using an IMG tag, this approach cannot be called a strict asynchronous execution. The user's browser waits for the execution of the PHP script to complete, that is, the status bar of the user's browser is always displayed in load. Of course, you can also use other similar principles, such as script tags and so on.
2. Popen () function
resource popen (String command, string mode);
Opens a pipeline that points to a process that is generated by the execution of a derived command command. Opens a pipeline that points to a process that is generated by the execution of a derived command command. So you can call it, but ignore its output.
Pclose (Popen ("/home/xinchen/backend.php &", ' R ')); The
method avoids the disadvantage of the first method and is also very fast. However, the problem is that this method cannot request another webservice through the HTTP protocol, only local script files can be executed. And can only be opened one way, unable to wear a large number of parameters to the called script. And if, when the traffic is very high, there will be a lot of process. If you use external resources, consider the competition yourself.
3. Curl Extension
Curl is a powerful HTTP command-line tool that simulates HTTP requests such as Post/get and then obtains and extracts data that appears on the "standard output" (stdout).

$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);

Use Curl to set the Curopt_timeout to 1 (minimum is 1, depressed). In other words, the client must wait at least 1 seconds.
4. Fscokopen () function
Fsockopen is a very powerful function, support socket programming, you can use Fsockopen to implement the mail and other socket programs, etc., using fcockopen need to manually stitching out the header section.

$fp = Fsockopen (www.jb51.net, N, $errno, $errstr), if (! $fp) {echo "$errstr ($errno)
\ n ";} else {$out = "get/backend.php/http/1.1\r\n"; $out. = "host:www.jb51.net\r\n"; $out. = "connection:close\r\n\r\n"; fwrite ($fp, $out); /* Ignore execution result while (!feof ($fp)) { echo fgets ($FP, 128);} */fclose ($FP);}

The above is for you to share the 4 kinds of PHP asynchronous implementation of the common way, I hope that everyone's learning is helpful.

Articles you may be interested in:

    • Research and sharing of asynchronous calling method in PHP implementation
    • PHP asynchronously invokes the socket implementation code
    • Resolves when php gets data using an asynchronous call (error c00ce56e causes this operation to not complete)
    • PHP Asynchronous Invocation Method Implementation Example

http://www.bkjia.com/PHPjc/1084548.html www.bkjia.com true http://www.bkjia.com/PHPjc/1084548.html techarticle PHP Implementation of asynchronous data call method, the browser and server only one for the non-connected HTTP protocol to communicate, the non-connected program is characterized by the client request service ...

  • 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.