i'm using simplexml fetch remote xml file , im having issues because simplexml can't load xml. don't know reason suspect remote site takes longer usual return data, resulting in timeout.
the code use following:
$xml = @simplexml_load_file($url); if(!$xml){ $database = config_helper::get_config_option('mysql'); $db = new \db($database['database'], $database['server'], $database['user'], $database['password']); $date = date('y-m-d h:i:s'); $db->query("insert gearman_job_error (timestamp, data, attempt) values ('$date', '{$job->workload()}', '1')"); //$db->query("insert gearman_job_error (timestamp, data, attempt) values ({$date}, {$job->workload()}, 1);"); return $job->sendfail(); } else { foreach($xml->point $key=>$value): $length = count($value); $timestamp = (string) $value->data[0]; $j=0; ($i = 1; $i < $length; $i++) { $forecast[$timestamp][$time_request][] = array($variables[$j] => (string) $value->data[$i]); $j++; } endforeach; return serialize($forecast); }
those url's can't load stored in database , checking them confirm load correctly in browser.. no problem them.
my question is, how can insist simplexml take it's time load url? goal after reasonable time assumes can't load file , store in database.
simplexml_load_file
doesn't have support specifying timeouts, can combine file_get_contents
, simplexml_load_string
, this:
<?php $timeout = 30; $url = 'http://...'; $context = stream_context_create(['http' => ['timeout' => $timeout]]); $data = file_get_contents($url, false, $context); $xml = simplexml_load_string($data); print_r($xml);
Comments
Post a Comment