php - 警告 : file_get_contents: failed to open stream: Redirection limit reached, 中止

标签 php dom redirect curl simple-html-dom

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

这是我为此编写的代码,遵循 PHP 简单 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() 正常工作。

求助! 谢谢!

最佳答案

今天遇到了类似的问题。我使用的是 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 问题导致了它! :) 和平!

关于php - 警告 : file_get_contents: failed to open stream: Redirection limit reached, 中止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12164196/

相关文章:

php - .htaccess 将子域重定向到 ssl 站点

html - 单击/提交到 iframe 后刷新整个页面

redirect - 条纹 : RedirectResolution; How can I redirect to specific action event?

php - 强制从 s3 亚马逊服务器下载

javascript - 使用 jQuery 获取 session 值

javascript - 如何在 DOM 树显示在屏幕上之前对其进行操作? (javascript, jquery, ...)

php - 使用 php dom 生成 xml 命名空间

php - 防止我的用户重复登录

gmail - PHPMailer 遇到问题

javascript - IE过滤器抗锯齿问题