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/