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


Warning: file_get_contents: failed to open stream: Redirection limit reached, aborting

我在这个网站上读了20多个相关问题,在谷歌上搜索过,但没有用。我是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方法一直返回"Resources"或"Objects"。我不知道如何将其传递给SimpleHTMLDOMParser以使$HTML->find()正常工作。

请帮忙!谢谢

今天也遇到了类似的问题。我正在使用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);
}

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

cookie问题导致了它!:)和平

使用解决

<?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)

使用cURL时,您需要将CURLOPT_RETURNTTRANSFER选项设置为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);

我不知道你为什么用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;

我还需要添加此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));

显然,我只是为了在本地环境中进行快速调试而使用它它不用于生产