gpt4 book ai didi

没有 cron 的 PHP 调度程序

转载 作者:搜寻专家 更新时间:2023-10-31 21:15:44 24 4
gpt4 key购买 nike

我想每 5 分钟运行一次我的 php 脚本。这是我的 PHP 代码。

function call_remote_file($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
}
set_time_limit(0);

$root='http://mywebsiteurl'; //remote location of the invoking and the working script

$url=$root."invoker.php";
$workurl=$root."script.php";

call_remote_file($workurl);//call working script
sleep(60*5);// wait for 300 seconds.
call_remote_file($url); //call again this script

我运行这段代码一次。即使在我关闭整个浏览器窗口后,它也能完美运行。

问题是如果我关闭系统的互联网连接,系统就会停止工作。

如何解决这个问题。请帮帮我。

最佳答案

虽然我真的不建议为一些关键的事情这样做(你会遇到稳定性问题),但这可能会奏效:

function call_remote_file($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
}
set_time_limit(0);


$root='http://mywebsiteurl'; //remote location of the invoking and the working script

$url=$root."invoker.php";
$workurl=$root."script.php";

while(true)
{
call_remote_file($workurl);//call working script
sleep(60*5);// wait for 300 seconds.
}

另一种方法是使用 exec() 从命令行调用它:

function call_remote_file($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
}
set_time_limit(0);


$root='http://mywebsiteurl'; //remote location of the invoking and the working script

$url=$root."invoker.php";
$workurl=$root."script.php";

call_remote_file($workurl);//call working script
sleep(60*5);// wait for 300 seconds.
exec('php ' . $_SERVER['SCRIPT_FILENAME']);

如果可能的话,你真的应该使用 cron。

关于没有 cron 的 PHP 调度程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9238605/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com