简体   繁体   English

php:达到重定向限制

[英]php: Redirection limit reached

I'm running the following loop: 我正在运行以下循环:

for ($i=0; $i < 30; $i++) {
    sleep(2);
    $content = file_get_contents('http://www.oref.org.il/WarningMessages/Alert/alerts.json?v=1444528825');
}

Basically, I'm running a cronjob (every one minute, 30 times) that gets data from a json file. 基本上,我正在运行一个cronjob(每1分钟30次),该工作从json文件获取数据。 The file that I'm receiving is being updated automatically. 我收到的文件正在自动更新。 When there is no data it's empty, if there is data it has data in it.. 没有数据时为空,如果有数据,则为数据。

So basically when the file is empty I get this error: 因此,基本上,当文件为空时,我会收到此错误:

PHP Warning:  file_get_contents(http://www.oref.org.il/WarningMessages/Alert/alerts.json?v=1444534443): failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found

That's ok and understandable, but the problem is that sometimes, I get this error instead: 没关系,可以理解,但是问题是有时我会收到以下错误消息:

PHP Warning:  file_get_contents(http://www.oref.org.il/WarningMessages/Alert/alerts.json?v=1444534451): failed to open stream: Redirection limit reached, aborting in /home/dupdates/domains/dupdates.com/public_html/systems/history/script.php on line 60

I searched it up but I didn't find any reason for the second error to appear. 我进行了搜索,但没有找到第二个错误出现的任何原因。 What can I do? 我能做什么? Is there a smarter way to write this script or avoid getting the error? 有没有更聪明的方式来编写此脚本或避免得到错误?

Example of error log: 错误日志示例:

PHP Warning:  file_get_contents(http://www.oref.org.il/WarningMessages/Alert/alerts.json?v=1444534443): failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found
 in /home/dupdates/domains/dupdates.com/public_html/systems/history/script.php on line 60
PHP Warning:  file_get_contents(http://www.oref.org.il/WarningMessages/Alert/alerts.json?v=1444534445): failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found
 in /home/dupdates/domains/dupdates.com/public_html/systems/history/script.php on line 60
PHP Warning:  file_get_contents(http://www.oref.org.il/WarningMessages/Alert/alerts.json?v=1444534447): failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found
 in /home/dupdates/domains/dupdates.com/public_html/systems/history/script.php on line 60
PHP Warning:  file_get_contents(http://www.oref.org.il/WarningMessages/Alert/alerts.json?v=1444534449): failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found
 in /home/dupdates/domains/dupdates.com/public_html/systems/history/script.php on line 60
PHP Warning:  file_get_contents(http://www.oref.org.il/WarningMessages/Alert/alerts.json?v=1444534451): failed to open stream: Redirection limit reached, aborting in /home/dupdates/domains/dupdates.com/public_html/systems/history/script.php on line 60
PHP Warning:  file_get_contents(http://www.oref.org.il/WarningMessages/Alert/alerts.json?v=1444534454): failed to open stream: Redirection limit reached, aborting in /home/dupdates/domains/dupdates.com/public_html/systems/history/script.php on line 60
PHP Warning:  file_get_contents(http://www.oref.org.il/WarningMessages/Alert/alerts.json?v=1444534456): failed to open stream: Redirection limit reached, aborting in /home/dupdates/domains/dupdates.com/public_html/systems/history/script.php on line 60
PHP Warning:  file_get_contents(http://www.oref.org.il/WarningMessages/Alert/alerts.json?v=1444534458): failed to open stream: Redirection limit reached, aborting in /home/dupdates/domains/dupdates.com/public_html/systems/history/script.php on line 60
PHP Warning:  file_get_contents(http://www.oref.org.il/WarningMessages/Alert/alerts.json?v=1444534460): failed to open stream: Redirection limit reached, aborting in /home/dupdates/domains/dupdates.com/public_html/systems/history/script.php on line 60
PHP Warning:  file_get_contents(http://www.oref.org.il/WarningMessages/Alert/alerts.json?v=1444534462): failed to open stream: Redirection limit reached, aborting in /home/dupdates/domains/dupdates.com/public_html/systems/history/script.php on line 60
PHP Warning:  file_get_contents(http://www.oref.org.il/WarningMessages/Alert/alerts.json?v=1444534464): failed to open stream: Redirection limit reached, aborting in /home/dupdates/domains/dupdates.com/public_html/systems/history/script.php on line 60
PHP Warning:  file_get_contents(http://www.oref.org.il/WarningMessages/Alert/alerts.json?v=1444534466): failed to open stream: Redirection limit reached, aborting in /home/dupdates/domains/dupdates.com/public_html/systems/history/script.php on line 60
PHP Warning:  file_get_contents(http://www.oref.org.il/WarningMessages/Alert/alerts.json?v=1444534468): failed to open stream: Redirection limit reached, aborting in /home/dupdates/domains/dupdates.com/public_html/systems/history/script.php on line 60
PHP Warning:  file_get_contents(http://www.oref.org.il/WarningMessages/Alert/alerts.json?v=1444534470): failed to open stream: Redirection limit reached, aborting in /home/dupdates/domains/dupdates.com/public_html/systems/history/script.php on line 60
PHP Warning:  file_get_contents(http://www.oref.org.il/WarningMessages/Alert/alerts.json?v=1444534500): failed to open stream: Redirection limit reached, aborting in /home/dupdates/domains/dupdates.com/public_html/systems/history/script.php on line 60
PHP Warning:  file_get_contents(http://www.oref.org.il/WarningMessages/Alert/alerts.json?v=1444534502): failed to open stream: Redirection limit reached, aborting in /home/dupdates/domains/dupdates.com/public_html/systems/history/script.php on line 60

I used curl instead: 我改用curl:

<?php
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, 'http://www.oref.org.il/WarningMessages/Alert/alerts.json?v=1'); 

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
        curl_setopt($ch, CURLOPT_TIMEOUT, '3');
        $content = curl_exec($ch);
        curl_close($ch);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM