: This article mainly introduces four common PHP asynchronous execution methods. For more information about PHP tutorials, see. This article describes php Asynchronous call methods for your reference. the specific content is as follows:
The client and the server communicate through the HTTP protocol, and the client initiates a request. the server receives the request and executes the processing and returns the processing result.
Sometimes the server needs to perform a very time-consuming operation, and the result of this operation does not need to be returned to the client. However, because php is executed synchronously, the client needs to wait until the service processing is complete before proceeding to the next step.
Therefore, time-consuming operations are suitable for asynchronous execution. after the server receives the request, it returns the data required by the client after processing, and then asynchronously performs time-consuming operations on the server.
1. use Ajax and img tags
Principle: Ajax code or img Mark is inserted into the html returned by the server. src of img is the program to be executed.
Advantage: The implementation is simple, and the server does not need to perform any calls.
Disadvantage: during execution, the browser will remain in the loading state, so this method is not really an Asynchronous call.
$.get("doRequest.php", { name: "fdipzone"} );
2. use popen
Use popen to execute the command. syntax:
// Popen-resource popen (string $ command, string $ mode) pclose (popen ('php/home/fdipzone/doRequest. php & ', 'r '));
Advantage: fast execution speed
Disadvantages:
- 1). It can only be executed on the local machine
- 2). a large number of parameters cannot be passed.
- 3). many processes will be created when the traffic is high.
3. use curl
Set the curl timeout value to 1 (minimum: 1). Therefore, the client needs to wait 1 second.
<?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 fsockopen
Fsockopen is the best, but the disadvantage is that you need to splice the header part by yourself.
<?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); $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 is disconnected or the connection times out, the execution may be incomplete. Therefore, you must add
Ignore_user_abort (true); // ignore client disconnection set_time_limit (0); // set no execution timeout
The above is a detailed introduction of php Asynchronous call methods, and I hope it will help you learn.
The above describes four common PHP asynchronous execution methods, including some content. I hope my friends who are interested in PHP tutorials will be helpful.