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.
Copy Code code as follows:
$.get ("dorequest.php", {name: "Fdipzone"});
Copy Code code as follows:
2. Use of Popen
Use Popen to execute commands, syntax:
Copy Code code as follows:
popen-Open Process file pointer
Resource Popen (String $command, String $mode)
Copy Code code as follows:
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
Copy Code code as follows:
<?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);
?>
4. Use of Fsockopen
Fsockopen is the best, the disadvantage is that you need to splice the header part.
Copy Code code as follows:
<?php
$url = ' http://www.example.com/doRequest.php ';
$param = Array (
' name ' => ' Fdipzone ',
' gender ' => ' male ',
' age ' =>30
);
DoRequest ($url, $param);
Function DoRequest ($url, $param =array ()) {
$urlinfo = Parse_url ($url);
$host = $urlinfo [' Host '];
$path = $urlinfo [' path '];
$query = isset ($param) http_build_query ($param): ';
$port = 80;
$errno = 0;
$errstr = ';
$timeout = 10;
$fp = Fsockopen ($host, $port, $errno, $errstr, $timeout); The
$out = "POST". $path. " Http/1.1\r\n ";
$out. = "Host:". $host. " \ r \ n ";
$out. = "Content-length:". strlen ($query). \ r \ n ";
$out. = "content-type:application/x-www-form-urlencoded\r\n";
$out. = "connection:close\r\n\r\n";
$out. = $query;
Fputs ($fp, $out);
Fclose ($FP);
}
?>
Note: When the client connection disconnects or the connection times out during execution, it may cause incomplete execution, so you need to add
Copy Code code as follows:
Ignore_user_abort (TRUE); Ignore client Disconnect
Set_time_limit (0); Set execution No timeout