Home > Backend Development > PHP Tutorial > Execute php tasks offline

Execute php tasks offline

高洛峰
Release: 2023-03-06 06:38:01
Original
1268 people have browsed it

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), Later,

getfiles() function is a custom function to perform the collection task. Later, the lower path was configured.

Open the written page and close it. Later, it was found that the task can be executed and completed. If you are interested, you can try it.

<?php
//设置忽略是否关闭终端窗口
ignore_user_abort(true);
ini_set(&#39;max_execution_time&#39;, &#39;0&#39;);
//采集页面函数,看不懂执行百度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 &#39;<script>window.close();</script>&#39;;
}
//配置需要采集的页面路径数组
$urls = array(
  &#39;http://www.cnblogs.com/jianqingwang/p/6373168.html&#39;,
  &#39;http://www.cnblogs.com/jianqingwang/p/6148644.html&#39;,
  &#39;http://www.61916.com/news_view_2423.html&#39;,
  &#39;http://blog.sina.com.cn/s/blog_8e326c350102w1ah.html&#39;,
  &#39;http://www.w3school.com.cn/php/func_misc_ignore_user_abort.asp&#39;,
  &#39;http://xinwen.eastday.com/a/170219205305597.html&#39;,
  &#39;http://society.huanqiu.com/article/2017-02/10162630.html?from=bdwz&#39;,
  &#39;http://www.cankaoxiaoxi.com/roll10/bd/20170220/1699670.shtml&#39;,
  &#39;http://news.china.com/socialgd/10000169/20170220/30266284.html&#39;,
  &#39;http://news.k618.cn/society/201702/t20170220_10368740.html&#39;,
  &#39;http://fj.qq.com/a/20170218/029521.htm&#39;
);
//遍历数组
foreach($urls as $key=>$val){
  getfiles($val,$key);
}
?>
Copy after login

For more articles related to offline execution of PHP tasks, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template