PHP Asynchronous Invocation method

Source: Internet
Author: User
Tags curl

PHP Asynchronous Invocation method

The client and server are connected through the HTTP protocol, the client initiates the request, the server side receives the request, executes the processing, and returns the processing result.

Sometimes the server needs to perform time-consuming operations, and the results of this operation do not need to be returned to the client. But because PHP is executed synchronously, the client needs to wait for the service to finish before it can proceed to the next step.

Therefore, for time-consuming operations that are appropriate for asynchronous execution, the server receives the request and returns the data that the client needs, and then asynchronously executes the time-consuming operation on the server.

1. Using Ajax and IMG tags

Principle, the server returns the HTML in which the AJAX code or IMG tag is inserted, and the IMG SRC is required to execute the program.

Benefits: Simple to implement, server-side no need to perform any calls

Disadvantage: The browser is always in the loading state during execution, so this method is not really an asynchronous call.

$.get ("dorequest.php", {name: "Fdipzone"});

2. Use of Popen

Use Popen to execute commands, syntax:

popen-open process file pointer   
resource Popen (string $command, String $mode)

Pclose (Popen (' php/home/fdipzone/dorequest.php & ', ' R '));

Advantages: Fast Execution speed

Disadvantages: 1. Can only be performed on this machine

2. Cannot pass a large number of parameters

3. Many processes are created when there is a high volume of traffic.

3. Use of Curl

Set Curl Timeout Curlopt_timeout 1 (minimum 1), so clients need to wait 1 seconds

<?php  
$ch = Curl_init ();  
$curl _opt = Array (  
    curlopt_url, ' http://www.example.com/doRequest.php '
    curlopt_returntransfer,1,  
    curlopt_timeout,1  
);  
Curl_setopt_array ($ch, $curl _opt);  
Curl_exec ($ch);  
Curl_close ($ch);  
? >

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/PHP/

Related Article

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

Starter Package

SSD Cloud server and data transfer for only $2.50 a month

Get Started >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.