Home > Article > Backend Development > How to close the web page without stopping the script in PHP
How does PHP close the web page without stopping the script? This article mainly introduces the method of PHP performing tasks offline after the web page is closed, which has a good reference value. I hope to be helpful.
Go directly to the code, the main function
ignore_user_abort(true);
This function ignores that the terminal is closed (the open web page is closed), The following
getfiles() function is a custom function to perform the collection task. Later, the lower path is configured.
Open the written page and close it. Later, it is found that the task can be executed and completed. If you are interested, you can try it.
<?php //设置忽略是否关闭终端窗口 ignore_user_abort(true); ini_set('max_execution_time', '0'); //采集页面函数,看不懂执行百度curl php function getfiles($url,$name){ $name = $name.".txt"; $ch = curl_init("$url"); $fp = fopen("$name", "w"); curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_HEADER, 0); curl_exec($ch); curl_close($ch); fclose($fp); sleep(5); echo '<script>window.close();</script>'; } //配置需要采集的页面路径数组 $urls = array( 'http://www.***.com/jiag/p/6373168.html', 'http://***.com/a/20170218/029521.htm' ); //遍历数组 foreach($urls as $key=>$val){ getfiles($val,$key); } ?>
Related recommendations:
PHP’s powerful CURL POST class
PHP learning CURL crawler example
PHP curl disguise source information
The above is the detailed content of How to close the web page without stopping the script in PHP. For more information, please follow other related articles on the PHP Chinese website!