Warning: file_get_contents: stream could not be opened: redirect limit reached, aborted

I read over 20 related questions on this site, searched on Google, but did not use. I am new to PHP and use PHP Simple HTML DOM Parser to get the url. Although this script works with local test pages, it just won't work with the URL for which I need a script for.

Here is the code I wrote for this, following the 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 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 tell me what needs to be done to make it work. I am new, so try the easy way. Reading other questions and their answers on this site, I tried to use the cURL method to create the handle, but I was not able to get it to work. The cURL method I tried continues to return "Resources" or "Objects". I don’t know how to pass this into plain HTML DOM Parser to work correctly on $ html-> find ().

Please, help! Thanks!

+6
source share
4 answers

Today there was a similar problem. I used CURL and I did not return any errors. Tested with file_get_contents () and I got ...

Could not open stream: redirect limit reached, interrupted in

Made a few searches, and I ended up with this function, which 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); } 

Website checked valid user agent and cookie .

The cookie problem caused this! :) World!

+4
source

Allowed:

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

You can also tell if you have a proxy server in the middle:

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

Using cURL, you need to set the CURLOPT_RETURNTRANSFER parameter to true in order to return the request body with curl_exec as follows:

 $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); 
+1
source

I don’t know exactly why you redefined the $ html object using the from get html line. The object is intended to search for a string. If you overwrite an object with a string, the object no longer exists and cannot be used.

In any case, to search for the string returned from 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; ?> 

you can search for another tag, the method is the same

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

Source: https://habr.com/ru/post/923983/


All Articles