php - file-get-contents: don't stop script when warning occurs (Redirection limit reached) -


i have php script that:

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

one url has redirection problem, redirects more 20 times. warning:

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

unfortunately, other urls of csvarray don't processed, because script stops after url wih redirection problem.

how can ignore warning , continue next urls?

thank help!

unfortunately, other urls of csvarray don't processed, because script stops after url wih redirection problem.

your script doesn't stop because of file_get_contents() redirection limit reached error. file_get_contents() triggers e_warning non-fatal error , doesn't halt script.

my guess script stops because of maximum execution time limit or other error. put ini_set('display_errors', true); , error_reporting(-1); on beginning of file call.


how can ignore warning , continue next urls?

1. easiest/noobish solution ignore error @ prefix.

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

2. can increase/descrese max redirects file_get_contents() 3rd parameter $context (context options , 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 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 magic curl.
  • set option curlopt_followlocation true or false if wish follow redirects.
  • you can set option curlopt_maxredirs number of maximum redirects follow.
  • set option curlopt_timeout maximum number of seconds allow curl functions execute.

see other options here.


Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -