我在这个网站上阅读了 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&name=A&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/