警告:file_get_contents:无法打开流:达到重定向限制,正在中止 [英] Warning: file_get_contents: failed to open stream: Redirection limit reached, aborting

查看:3717
本文介绍了警告:file_get_contents:无法打开流:达到重定向限制,正在中止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这个网站上阅读超过20个相关问题,在Google搜索,但没有用。我是新的PHP和使用PHP简单的HTML DOM解析器来获取一个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;

?>

这是我得到的错误信息:

 警告:file_get_contents(http://www.farmersagent.com/Results.aspx?isa=1&amp;name=A&amp;csz=AL​​)[功能。文件获取内容]:无法打开流:重定向限制到达,中止在/home/content/html/website.in/test/simple_html_dom.php第70行

请指导我做什么来使它工作。我是新的,所以请建议一种简单的方法。在阅读其他问题和他们的答案在这个网站,我尝试cURL方法创建一个句柄,但我没有使它的工作。我试着的cURL方法不断返回资源或对象。我不知道如何传递给简单的HTML DOM解析器,使$ html-> find()正常工作。



请帮助!
谢谢!

解决方案

今天有一个类似的问题。我使用CURL,它没有返回我的任何错误。测试用file_get_contents()和我有...



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

进行了几个搜索,我结束了对我的案例工作的这个功能...

  function getPage($ url){


$ useragent ='Mozilla / 5.0(Macintosh; Intel Mac OS X 10_8_2)AppleWebKit / 537.36(KHTML,类似Gecko)Chrome / 44.0 .2403.89 Safari / 537.36';
$ timeout = 120;
$ dir = dirname(__ FILE__);
$ cookie_file = $ dir。 '/饼干/' 。 md5($ _ SERVER ['REMOTE_ADDR'])。 '。文本';

$ 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);

}

网站正在检查有效的用户代理, strong> cookies 。



Cookie问题导致了! :)
和平!


I read over 20 related questions on this site, searched in Google but no use. I'm new to PHP and am using PHP Simple HTML DOM Parser to fetch a URL. While this script works with local test pages, it just won't work with the URL that I need the script for.

Here is the code that I wrote for this, following an example file that came with the PHP Simple DOM parser library:

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

?>

And this is the error message that I get:

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

Please guide me what should be done to make it work. I'm new so please suggest a way that is simple. While reading other questions and their answers on this site, I tried cURL method to create a handle but I failed to make it work. The cURL method that I tried keeps returning "Resources" or "Objects". I don't know how to pass that to Simple HTML DOM Parser to make $html->find() work properly.

Please help! Thanks!

解决方案

Had a similar problem today. I was using CURL and it wasn't returning my any error. Tested with file_get_contents() and I got...

failed to open stream: Redirection limit reached, aborting in

Made a few searches and I'v ended with this function that works on my case...

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

}

The website was checking for a valid user agent and for cookies.

The cookie issue was causing it! :) Peace!

这篇关于警告:file_get_contents:无法打开流:达到重定向限制,正在中止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆