dregduc63699 2015-10-11 15:50
浏览 60
已采纳

php:达到重定向限制

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. 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
  • 写回答

1条回答 默认 最新

  • dsqpx86002 2015-11-28 11:43
    关注

    I used curl instead:

    <?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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!