【问题标题】:Warning: file_get_contents: failed to open stream: Redirection limit reached, aborting警告:file_get_contents:无法打开流:已达到重定向限制,正在中止
【发布时间】:2012-08-28 17:08:26
【问题描述】:

我在这个网站上阅读了 20 多个相关问题,在 Google 中搜索过但没有用。我是 PHP 新手,正在使用 PHP Simple HTML DOM Parser 来获取 URL。虽然此脚本适用于本地测试页面,但它不适用于我需要该脚本的 URL。

这是我为此编写的代码,遵循 PHP Simple DOM 解析器库附带的示例文件:

<?php

include('simple_html_dom.php');

$html = file_get_html('http://www.farmersagent.com/Results.aspx?isa=1&name=A&csz=AL');

foreach($html->find('li.name ul#generalListing') as $e)
echo $e->plaintext;  

?>

这是我收到的错误消息:

Warning: file_get_contents(http://www.farmersagent.com/Results.aspx?isa=1&amp;name=A&amp;csz=AL) [function.file-get-contents]: failed to open stream: Redirection limit reached, aborting in /home/content/html/website.in/test/simple_html_dom.php on line 70

请指导我应该做些什么来使它工作。我是新手,所以请提出一种简单的方法。在阅读此站点上的其他问题及其答案时,我尝试了 cURL 方法来创建句柄,但未能成功。我尝试的 cURL 方法不断返回“资源”或“对象”。我不知道如何将它传递给 Simple HTML DOM Parser 以使 $html->find() 正常工作。

请帮忙! 谢谢!

【问题讨论】:

  • 我得到200 OK,当我尝试访问该文件时根本没有重定向...
  • 嗨,科林克!谢谢你的评论。奇怪的是它在我的 Windows PC 上显示错误 500,当我在我的 Linux 服务器上运行它时,它显示的是错误消息。
  • @ChandanMishra 如果您选择其中一个答案并将其标记为正确答案会很好,如果它解决了您的问题,好吗?

标签: php dom redirect curl simple-html-dom


【解决方案1】:

今天遇到了类似的问题。我正在使用 CURL,它没有返回我的任何错误。用 file_get_contents() 测试,我得到了...

无法打开流:已达到重定向限制,正在中止

进行了几次搜索,我以适用于我的情况的这个功能结束了...

function getPage ($url) {


$useragent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36';
$timeout= 120;
$dir            = dirname(__FILE__);
$cookie_file    = $dir . '/cookies/' . md5($_SERVER['REMOTE_ADDR']) . '.txt';

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt($ch, CURLOPT_ENCODING, "" );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_AUTOREFERER, true );
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout );
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout );
curl_setopt($ch, CURLOPT_MAXREDIRS, 10 );
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_REFERER, 'http://www.google.com/');
$content = curl_exec($ch);
if(curl_errno($ch))
{
    echo 'error:' . curl_error($ch);
}
else
{
    return $content;        
}
    curl_close($ch);

}

网站正在检查有效的用户代理和 cookies

是 cookie 问题造成的! :) 和平!

【讨论】:

  • 不错,在我的情况下也适用。你为我节省了很多时间,谢谢!
  • 如果你返回,curl_close 永远不会发生。这是故意的吗?如果是这样,您应该将 curl_close 移动到 curl_errno 块中
  • 太棒了! cookie 是我的案例中缺少的参数。非常感谢!
  • 干得好!谢谢。
【解决方案2】:

我还需要添加这个 HTTP 上下文选项ignore_errors

见:https://www.php.net/manual/en/context.http.php

$arrContextOptions = array(
    "ssl" => array(
        // skip error "Failed to enable crypto" + "SSL operation failed with code 1."
        "verify_peer" => false,
        "verify_peer_name" => false,
         ),
     // skyp error "failed to open stream: operation failed" + "Redirection limit reached"
     'http' => array(
          'max_redirects' => 101,
          'ignore_errors' => '1'
      ),
           
  );

  $file = file_get_contents($file_url, false, stream_context_create($arrContextOptions));

显然,我仅将它用于在本地环境中进行快速调试。 不适用于生产

【讨论】:

    【解决方案3】:

    解决方法:

    <?php
    $context = stream_context_create(
        array(
            'http' => array(
                'max_redirects' => 101
            )
        )
    );
    $content = file_get_contents('http://example.org/', false, $context);
    ?>
    

    中间是否有代理也可以告知:

    $aContext = array('http'=>array('proxy'=>$proxy,'request_fulluri'=>true));
    $cxContext = stream_context_create($aContext);
    

    更多详情:https://cweiske.de/tagebuch/php-redirection-limit-reached.htm(感谢@jqpATs2w)

    【讨论】:

    【解决方案4】:

    使用 cURL,您需要将 CURLOPT_RETURNTRANSFER 选项设置为 true,以便通过调用 curl_exec 返回请求正文,如下所示:

    $url = 'http://www.farmersagent.com/Results.aspx?isa=1&name=A&csz=AL';
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    // you may set this options if you need to follow redirects. Though I didn't get any in your case
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    $content = curl_exec($curl);
    curl_close($curl);
    
    $html = str_get_html($content);
    

    【讨论】:

    • 嗨,迈克,感谢您的回答!我已经尝试过了,但我只是不知道如何将句柄从 cURL 传递到 DOM 解析器,因此 find() 方法开始工作。我试过这段代码,但请看看应该怎么写,因为这段代码返回一个警告和一个致命错误(致命错误:在非对象上调用成员函数 find()):
    • @ChandanMishra 我不熟悉您正在使用的库,但是在浏览文档时,有一个函数可以从字符串中填充 DOM 对象,这就是您将从 cURL 中得到的内容。请参阅上面我修改后的答案。
    • 在尝试 str_get_html($content) 之后,我收到一个致命错误:致命错误:调用 /home/content/html/website.in/ 中非对象上的成员函数 find()第 21 行的 test/test.php 可能是什么原因造成的?
    • @ChandanMishra 我不知道您是否尝试过进行变量转储以查看进程在哪里发生故障(即您没有得到 curl 结果,未创建 DOM 对象等)。
    • 嗨 Mike,$content 的 var_dump() 返回 False。
    【解决方案5】:

    我不确定您为什么使用 get html 中的字符串重新定义 $html 对象,该对象旨在用于搜索字符串。如果用字符串覆盖对象,则对象不再存在,无法使用。

    无论如何,要搜索从 curl 返回的字符串。

    <?php
    $url = 'http://www.example.com/Results.aspx?isa=1&name=A&csz=AL';
    
    include('simple_html_dom.php');
    
    # create object
    $html = new simple_html_dom();
    
    #### CURL BLOCK ####
    
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    # you may set this options if you need to follow redirects.
    # Though I didn't get any in your case
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    
    $content = curl_exec($curl);
    curl_close($curl);
    
    # note the variable change.
    $string = str_get_html($content);
    
    # load the curl string into the object.
    $html->load($string);
    
    #### END CURL BLOCK ####
    
    # without the curl block above you would just use this.
    $html->load_file($url);
    
    # choose the tag to find, you're not looking for attributes here.
    $html->find('a');
    
    # this is looking for anchor tags in the given string.
    # you output the attributes contents using the name of the attribute.
    echo $html->href;
    ?>
    

    你可能正在搜索不同的标签,方法是一样的

    # just outputting a different tag attribute
    echo $html->class;
    
    echo $html->id;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-24
      • 2020-10-02
      • 1970-01-01
      • 2015-10-08
      • 1970-01-01
      • 1970-01-01
      • 2021-11-17
      • 2017-06-20
      相关资源
      最近更新 更多