从超时停止无限循环



基本上,我有一个使用file_get_contents进行无限循环的脚本,它基本上只是运行函数然后增加$_GET变量传递给file_get_contents url,问题显然是它抛出了一个错误"太多重定向",是他们的一种方式,我可以在Cron Job上做到这一点或一种没有错误的方式,这是我目前的代码

$id = $_GET['id'];
$next_id = $id + 1;
$url = "http://www.website.com/profile/$id.json";
$json = file_get_contents($url);
$result = json_decode($json, true);
$headers = get_headers($url);
if($headers[0] !== "HTTP/1.1 404 Not Found") {
  $query = mysql_query("query here");
  if($query) {
       echo '<script type="text/javascript">
       <!-- window.location = "import.php?id='.$next_id.'" //-->
       </script>';
  } else {
       echo '<script type="text/javascript">
       <!-- window.location = "import.php?id='.$id.'" //-->
       </script>';
  }
}

是否有其他方法或更有效的方法来做到这一点?在使用重定向之前,我尝试过使用sleep(5)等方法,但它似乎仍然在继续做

尝试在JavaScript中重定向前添加暂停:

echo '<script type="text/javascript">
<!-- window.setTimeout("window.location = "import.php?id='.$next_id.'"", 5000); //-->
</script>';

最新更新