1. The client page uses AJAX technology to request the server
advantage:The simplest and fastest is to embed AJAX calls in the HTML code returned to the client, or embed an img tag, and src points to the time-consuming script to be executed.
shortcoming:Generally speaking, Ajax should be triggered after onLoad, which means that after the user clicks on the page, it closes, and our background script will not be triggered.
If you use the img tag, this method cannot be called asynchronous execution in the strict sense. The user's browser will wait for the execution of the php script to be completed for a long time, that is, the status bar of the user's browser is still loading.
Of course, other methods with similar principles can also be used, such as script tags, etc.
()function
This function opens a pipeline to a process that is generated by the execution of the derived given command command. Opens a pipeline to a process that is generated by the execution of the derived command command.
So it can be done by calling it, but ignore its output. The code used is as follows:
pclose(popen("/home/xinchen/ &", 'r'));
advantage:The disadvantages of the first method are avoided and are also very fast.
shortcoming:This method cannot request another WebService through the HTTP protocol, and can only execute local script files. And it can only be opened in one direction, and cannot use a large number of parameters to the called script. And if the number of visits is high, a large number of processes will be generated. If you use external resources, you have to consider competition yourself.
Extended
CURL is a powerful HTTP command line tool that can simulate HTTP requests such as POST/GET, and then obtain and extract data, and display it on "standard output" (stdout). The code is as follows:
$ch = curl_init();
$curl_opt = array(CURLOPT_URL, '/',
CURLOPT_RETURNTRANSFER, 1,
CURLOPT_TIMEOUT, 1,);
curl_setopt_array($ch, $curl_opt);
curl_exec($ch);
curl_close($ch);
shortcoming:As described in your question, since using CURL, you need to set CUROPT_TIMEOUT to 1 (minimum is 1, depressed). That is, the client must wait at least 1 second.
()function
fsocopen supports socket programming. You can use fsocopen to implement socket programs such as email sending, etc. When using fsocopen, you need to manually splice out the header part by yourself.
Examples of use are as follows:
$fp = fsockopen("", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET / / HTTP/1.1\r\n";
$out .= "Host: \r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
/*Ignore the execution result
while (!feof($fp)) {
echo fgets($fp, 128);
}*/
fclose($fp);
}
So in summary, the fscokopen() function should meet your requirements. You can try it.
advantage:The simplest and fastest is to embed AJAX calls in the HTML code returned to the client, or embed an img tag, and src points to the time-consuming script to be executed.
shortcoming:Generally speaking, Ajax should be triggered after onLoad, which means that after the user clicks on the page, it closes, and our background script will not be triggered.
If you use the img tag, this method cannot be called asynchronous execution in the strict sense. The user's browser will wait for the execution of the php script to be completed for a long time, that is, the status bar of the user's browser is still loading.
Of course, other methods with similar principles can also be used, such as script tags, etc.
()function
This function opens a pipeline to a process that is generated by the execution of the derived given command command. Opens a pipeline to a process that is generated by the execution of the derived command command.
So it can be done by calling it, but ignore its output. The code used is as follows:
Copy the codeThe code is as follows:
pclose(popen("/home/xinchen/ &", 'r'));
advantage:The disadvantages of the first method are avoided and are also very fast.
shortcoming:This method cannot request another WebService through the HTTP protocol, and can only execute local script files. And it can only be opened in one direction, and cannot use a large number of parameters to the called script. And if the number of visits is high, a large number of processes will be generated. If you use external resources, you have to consider competition yourself.
Extended
CURL is a powerful HTTP command line tool that can simulate HTTP requests such as POST/GET, and then obtain and extract data, and display it on "standard output" (stdout). The code is as follows:
Copy the codeThe code is as follows:
$ch = curl_init();
$curl_opt = array(CURLOPT_URL, '/',
CURLOPT_RETURNTRANSFER, 1,
CURLOPT_TIMEOUT, 1,);
curl_setopt_array($ch, $curl_opt);
curl_exec($ch);
curl_close($ch);
shortcoming:As described in your question, since using CURL, you need to set CUROPT_TIMEOUT to 1 (minimum is 1, depressed). That is, the client must wait at least 1 second.
()function
fsocopen supports socket programming. You can use fsocopen to implement socket programs such as email sending, etc. When using fsocopen, you need to manually splice out the header part by yourself.
Examples of use are as follows:
Copy the codeThe code is as follows:
$fp = fsockopen("", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET / / HTTP/1.1\r\n";
$out .= "Host: \r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
/*Ignore the execution result
while (!feof($fp)) {
echo fgets($fp, 128);
}*/
fclose($fp);
}
So in summary, the fscokopen() function should meet your requirements. You can try it.