php - SimpleXML - fails loading some remote URL's -


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.

example: http://mandeo.meteogalicia.es/thredds/ncss/modelos/wrf_hist/d02/2015/02/wrf_arw_det_history_d02_20150211_0000.nc4?latitude=40.393288&longitude=-8.873433&var=rh%2ctemp%2cswflx%2ccfh%2ccfl%2ccfm%2ccft&point=true&accept=xml&time_start=2015-02-11t00%3a00z&time_end=2015-02-14t20%3a00z

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