Four common PHP asynchronous execution methods ,. Four common PHP asynchronous execution methods. This article describes php asynchronous calling methods and provides them for your reference, the specific content is as follows: the client and server are commonly used for PHP asynchronous execution through the HTTP protocol,
This article tells you aboutPhp Asynchronous call methodFor your reference, the details are 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.
Advantages:The implementation is simple, and the server does not need to perform any calls.
Disadvantages:During the execution, the browser will remain in the loading state, so this method is not really 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 '));
Advantages: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 isPhp Asynchronous call methodTo help you learn.
Articles you may be interested in:
- In-depth explanation of PHP asynchronous execution
- PHP asynchronous execution method, simulating multi-thread Application Analysis
- PHP ajax asynchronous execution without waiting for execution results
Callback. This article describes the php Asynchronous call method for your reference. the specific content is as follows...