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

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名