当前位置:Gxlcms > PHP教程 > 离线执行php任务

离线执行php任务

时间:2021-07-01 10:21:17 帮助过:5人阅读

直接上代码,主要函数

ignore_user_abort(true);

这个函数忽略了终端被关闭(打开的网页被关闭),后面

getfiles()这函数是执行采集任务的自定义函数,后面又配置了下路径

打开写好的页面,关闭,后面发现任务都能执行完成,有兴趣的可以试试。

  1. <?php
  2. //设置忽略是否关闭终端窗口
  3. ignore_user_abort(true);
  4. ini_set('max_execution_time', '0');
  5. //采集页面函数,看不懂执行百度curl php
  6. function getfiles($url,$name){
  7. $name = $name.".txt";
  8. $ch = curl_init("$url");
  9. $fp = fopen("$name", "w");
  10. curl_setopt($ch, CURLOPT_FILE, $fp);
  11. curl_setopt($ch, CURLOPT_HEADER, 0);
  12. curl_exec($ch);
  13. curl_close($ch);
  14. fclose($fp);
  15. sleep(5);
  16. echo '<script>window.close();</script>';
  17. }
  18. //配置需要采集的页面路径数组
  19. $urls = array(
  20. 'http://www.cnblogs.com/jianqingwang/p/6373168.html',
  21. 'http://www.cnblogs.com/jianqingwang/p/6148644.html',
  22. 'http://www.61916.com/news_view_2423.html',
  23. 'http://blog.sina.com.cn/s/blog_8e326c350102w1ah.html',
  24. 'http://www.w3school.com.cn/php/func_misc_ignore_user_abort.asp',
  25. 'http://xinwen.eastday.com/a/170219205305597.html',
  26. 'http://society.huanqiu.com/article/2017-02/10162630.html?from=bdwz',
  27. 'http://www.cankaoxiaoxi.com/roll10/bd/20170220/1699670.shtml',
  28. 'http://news.china.com/socialgd/10000169/20170220/30266284.html',
  29. 'http://news.k618.cn/society/201702/t20170220_10368740.html',
  30. 'http://fj.qq.com/a/20170218/029521.htm'
  31. );
  32. //遍历数组
  33. foreach($urls as $key=>$val){
  34. getfiles($val,$key);
  35. }
  36. ?>

更多离线执行php任务相关文章请关注PHP中文网!

人气教程排行