Recommend this page to a friend! |
SimpleXML for PHP4 | > | All threads | > | Fatal error: Call to a member... | > | (Un) Subscribe thread alerts |
|
Noel Keene - 2009-02-21 14:11:05
Hi. I'm using your script on a server where php5 is not available...which by the way is probably going to save me on my project...so thank you!!
But I'm having problems that I don't have when using the SimpleXMLElement functionality in php5. I'll show you where the script has problems and returns a fatal error. On the following xml data, it does fine. [2] => SimpleXMLObject Object ( [receipt_time] => 2009-02-21T13:46:29Z [observation_time] => 2009-02-21T13:36:00Z [aircraft_ref] => C182 [latitude] => 41.7833 [longitude] => -107.2 [altitude_ft_msl] => 9000 [turbulence_condition] => SimpleXMLObject Object ( [@attributes] => SimpleXMLObject Object ( [turbulence_intensity] => LGT [turbulence_base_ft_msl] => 8000 ) ) [pirep_type] => PIREP [raw_text] => RWL UA /OV RWL /TM 1336 /FL090 /TP C182 /TB LGT 080 /RM PATCHY MTN OBSC SE SMOOTH AT 090 ) But when there are two elements in [turbulence_condition], my script returns the following error: Fatal error: Call to a member function attributes() on a non-object in C:\perl-scripts\west_pirep_xml_php4.php on line 53. Here's the xml that its hanging up on...note that [turbulence_condition] is now an array and I think that is what's causing problems: [3] => SimpleXMLObject Object ( [receipt_time] => 2009-02-21T13:40:10Z [observation_time] => 2009-02-21T13:34:00Z [quality_control_flags] => SimpleXMLObject Object ( [no_flt_lvl] => TRUE ) [aircraft_ref] => DC6 [latitude] => 61.1742 [longitude] => -149.996 [altitude_ft_msl] => 3000 [sky_condition] => SimpleXMLObject Object ( [@attributes] => SimpleXMLObject Object ( [sky_cover] => SKC [cloud_base_ft_msl] => 3000 [cloud_top_ft_msl] => 60000 ) ) [turbulence_condition] => Array ( [0] => SimpleXMLObject Object ( [@attributes] => SimpleXMLObject Object ( [turbulence_intensity] => LGT [turbulence_base_ft_msl] => 0 [turbulence_top_ft_msl] => 6000 ) ) [1] => SimpleXMLObject Object ( [@attributes] => SimpleXMLObject Object ( [turbulence_intensity] => NEG ) ) ) [icing_condition] => SimpleXMLObject Object ( [@attributes] => SimpleXMLObject Object ( [icing_intensity] => NEGclr [icing_base_ft_msl] => 3000 [icing_top_ft_msl] => 60000 ) ) [visibility_statute_mi] => 10 [pirep_type] => PIREP [raw_text] => ANC UA /OV ANC /TM 1334 /FLUNKN /TP DC6 /SK SKC /WX FV10SM /TB LGT SFC-060/SMOOTH O70 /RM DURGC W BND ) Do you have suggestions on how I can handle this type of situation, and prevent the script from stopping when encountering this array? Thanks for your time, Here's my script if it helps... <?php require_once "simplexml.class.php"; ini_set('display_errors', 1); error_reporting(E_ALL|E_STRICT); $file = "http://www.weather.aero/dataserver_current/httpparam?dataSource=pireps&requestType=retrieve&format=xml&minLat=25&minLon=-170&maxLat=65&maxLon=-102&hoursBefo reNow=3"; $sxml = new simplexml; $data = $sxml->xml_load_file($file); echo "<pre>"; print_r($data); $i = 0; while ($data->data->PIREP[$i]->receipt_time != ''){ $pirep_txt[$i] = $data->data->PIREP[$i]->raw_text; $long1[$i] = $data->data->PIREP[$i]->longitude; $lat1[$i] = $data->data->PIREP[$i]->latitude; $alt1[$i] = $data->data->PIREP[$i]->altitude_ft_msl; $time[$i] = $data->data->PIREP[$i]->observation_time; $acft_type[$i] = $data->data->PIREP[$i]->aircraft_ref; $lonlat_pt[$i] = $long1[$i].' '.$lat1[$i]; $lonlat_pt2[$i] = $long1[$i].','.$lat1[$i]; if ($data->data->PIREP[$i]->turbulence_condition->attributes() != '' ){ if ($data->data->PIREP[$i]->turbulence_condition->attributes()->turbulence_intensity != '') { $turb_int[$i] = $data->data->PIREP[$i]->turbulence_condition->attributes()->turbulence_intensity; } else { $turb_int[$i] = 'NR'; } if ($data->data->PIREP[$i]->turbulence_condition->attributes()->turbulence_base_ft_msl != ''){ $turb_base[$i] = $data->data->PIREP[$i]->turbulence_condition->attributes()->turbulence_base_ft_msl; } else { $turb_base[$i] = $alt1[$i]; } if ($data->data->PIREP[$i]->turbulence_condition->attributes()->turbulence_top_ft_msl != ''){ $turb_top[$i] = $data->data->PIREP[$i]->turbulence_condition->attributes()->turbulence_top_ft_msl; } else { $turb_top[$i] = 99999; } if ($data->data->PIREP[$i]->turbulence_condition->attributes()->turbulence_type != ''){ $turb_type[$i] = $data->data->PIREP[$i]->turbulence_condition->attributes()->turbulence_type; } else { $turb_type[$i] = 'NR'; } if ($data->data->PIREP[$i]->turbulence_condition->attributes()->turbulence_freq != ''){ $turb_freq[$i] = $data->data->PIREP[$i]->turbulence_condition->attributes()->turbulence_freq; } else { $turb_freq[$i] = 'NR'; } } else { $turb_int[$i] = 'NR'; $turb_base[$i] = 99999; $turb_top[$i] = 99999; $turb_type[$i] = 'NR'; $turb_freq[$i] = 'NR'; } ?> Shad
Taha Paksu - 2009-03-18 11:41:53 - In reply to message 1 from Noel Keene
try using
$var = $xml->item->attributes(); $var2 = $var->item2; instead of $var2 = $xml->item->attributes()->item2; this will solve your problem. |
info at phpclasses dot org
.