龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

如何实现PHP的计划(定时)任务

时间:2014-07-22 14:50来源: 作者: 点击:
分享到:
a target=_blank href=http://list.qq.com/cgi-bin/qf_invite?id=08f814f703c7b4139f32b06cc2f7c0c1fd1b46f032c64622 rel=nofollow/a a href=http://qita.in/bMv target=_blank rel=nofollowimg alt=填写您的邮件地址,订阅我们的精彩内容:
填写您的邮件地址,订阅我们的精彩内容:
    有时候为了定时去调接口,需要程序自动运行。从网上搜到有两种方法可以实现
    1、ignore_user_abort()
    ignore_user_abort()函数搭配set_time_limit(0)和sleep($interval)即可实现程序自动运行更新。
    例子:(只要运行下面的页面,浏览器关闭程序也会一直运行)
    http://blog.qita.in/?post=489

//即使Client断开(如关掉浏览器),PHP脚本也可以继续执行.
ignore_user_abort();
// 执行时间为无限制,php默认的执行时间是30秒,通过set_time_limit(0)可以让程序无限制的执行下去
set_time_limit(0);
// 每隔5分钟运行
$interval=60*5;
do{
$url = “http://www.yexiwei.com”;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 2);
$result = curl_exec($ch);
curl_close($ch);
// 等待5分钟
sleep($interval);
}while(true);
精彩图集

赞助商链接