php如何執行離線任務

來源:互聯網
上載者:User
本文主要介紹了離線執行php任務的方法,具有很好的參考價值,下面跟著小編一起來看下吧

直接上代碼,主要函數

ignore_user_abort(true);

這個函數忽略了終端被關閉(開啟的網頁被關閉),後面

getfiles()這函數是執行採集任務的自訂函數,後面又配置了下路徑

開啟寫好的頁面,關閉,後面發現任務都能執行完成,有興趣的可以試試。

<?php//設定忽略是否關閉終端視窗ignore_user_abort(true);ini_set('max_execution_time', '0');//採集頁面函數,看不懂執行百度curl phpfunction 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.cnblogs.com/jianqingwang/p/6373168.html',  'http://www.cnblogs.com/jianqingwang/p/6148644.html',  'http://www.61916.com/news_view_2423.html',  'http://blog.sina.com.cn/s/blog_8e326c350102w1ah.html',  'http://www.w3school.com.cn/php/func_misc_ignore_user_abort.asp',  'http://xinwen.eastday.com/a/170219205305597.html',  'http://society.huanqiu.com/article/2017-02/10162630.html?from=bdwz',  'http://www.cankaoxiaoxi.com/roll10/bd/20170220/1699670.shtml',  'http://news.china.com/socialgd/10000169/20170220/30266284.html',  'http://news.k618.cn/society/201702/t20170220_10368740.html',  'http://fj.qq.com/a/20170218/029521.htm');//遍曆數組foreach($urls as $key=>$val){  getfiles($val,$key);}?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.