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_followlocationtrueorfalseif wish follow redirects. - you can set option
curlopt_maxredirsnumber of maximum redirects follow. - set option
curlopt_timeoutmaximum number of seconds allow curl functions execute.
see other options here.
Comments
Post a Comment