1

I have a php script like that:

include_once('simple_html_dom.php');
foreach ($csvarray as $product){
   $url = $product[31];
   $html = file_get_html($url);
   ...
}

One URL has a redirection problem, so it redirects more than 20 times. So I get this warning:

Warning: file_get_contents(http:/...) [function.file-get-contents]: failed to open stream: Redirection limit reached, aborting in ... on line ...

Unfortunately, the other URLs of my csvarray don't get processed, because the script stops after this URL wih the redirection problem.

How can I just ignore this warning and continue with the next urls?

Thank you for your help!

AndiPower
  • 853
  • 10
  • 20

3 Answers3

2

Unfortunately, the other URLs of my csvarray don't get processed, because the script stops after this URL wih the redirection problem.

Your script doesn't stop because of the file_get_contents() redirection limit reached error. file_get_contents() triggers E_WARNING which is non-fatal error and doesn't halt script.

My guess is that your script stops because of the maximum execution time limit or any other error. Put ini_set('display_errors', true); and error_reporting(-1); on the beginning of the file your call.


How can I just ignore this warning and continue with the next urls?

1. The easiest/noobish solution is to just ignore error with @ prefix.

$html = @file_get_contents('http://bit.ly/6wgJO');

2. You can increase/descrese max redirects for file_get_contents() with 3rd parameter $context (context options and parameters) :

$context = stream_context_create(['http' => ['max_redirects' => 50]]);
$html = @file_get_contents('http://bit.ly/6wgJO', false, $context);

3. Other solution of hiding errors is ignore_errors in context options :

$context = stream_context_create(['http' => ['max_redirects' => 0, 'ignore_errors' => true]]);
$html = file_get_contents('http://bit.ly/6wgJO', false, $context);

4. Use cURL.

  • You can do magic with cURL.
  • Set option CURLOPT_FOLLOWLOCATION to true or false if you wish to follow redirects.
  • You can even set option CURLOPT_MAXREDIRS for number of maximum redirects to follow.
  • Set option CURLOPT_TIMEOUT for maximum number of seconds to allow cURL functions to execute.

See other options here.

Glavić
  • 42,781
  • 13
  • 77
  • 107
-1

Run the function with @ prefix:

$html = @file_get_html($url);

Should solve the problem.

You can use cURL and set a parameter called CURLOPT_FOLLOWLOCATION to TRUE.

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
Vish
  • 4,508
  • 10
  • 42
  • 74
-3

there are several ways you can do this one would be to try to catch the exceptions

try {
$html = file_get_html($url);
}catch(Exception $e){
//Do what you want to do with the code
}

another way would be to simply suppress the warning by using @ in front of file_get_html:

$html = @file_get_html($url);

There are better ways of handling this error as well if you want to know more but those are the simplest ones.

Reza
  • 30
  • 3
  • I think that `file_get_contents()` function doesn't throw exceptions. Correct me if I am wrong. – Glavić Aug 16 '13 at 11:59