SimpleXML Error Handling
I'm loading a simpleXML url like so:
$City_and_State = "Miami,FL"
$url="https://www.google.com/ig/api?weather=$City_and_State&hl=en&referrer=googlecalendar";
$xml = simplexml_load_file($url);
and the data I get back is:
<?xml version="1.0"?>
-<xml_api_reply version="1">-<weather section="0" row="0"
mobile_zipped="1" mobile_row="0" tab_id="0"
module_id="0">-<forecast_information><city data="Miami, FL"/><postal_code
data="Miami,FL"/><latitude_e6 data=""/><longitude_e6
data=""/><forecast_date data="2013-08-26"/><current_date_time
data="1970-01-01 00:00:00 +0000"/><unit_system
data="US"/></forecast_information>-<current_conditions><condition
data="Mostly Cloudy"/><temp_f data="86"/><temp_c data="30"/><humidity
data="Humidity: 76%"/><icon
data="/ig/images/weather/mostly_cloudy.gif"/><wind_condition data="Wind:
NE at 0 mph"/></current_conditions>-<forecast_conditions><day_of_week
data="Mon"/><low data="77"/><high data="93"/><icon
data="/ig/images/weather/thunderstorm.gif"/><condition
data="Thunderstorm"/></forecast_conditions>-<forecast_conditions><day_of_week
data="Tue"/><low data="77"/><high data="93"/><icon
data="/ig/images/weather/chance_of_storm.gif"/><condition data="Chance of
Storm"/></forecast_conditions>-<forecast_conditions><day_of_week
data="Wed"/><low data="77"/><high data="93"/><icon
data="/ig/images/weather/chance_of_storm.gif"/><condition data="Chance of
Storm"/></forecast_conditions>-<forecast_conditions><day_of_week
data="Thu"/><low data="79"/><high data="93"/><icon
data="/ig/images/weather/chance_of_storm.gif"/><condition data="Chance of
Storm"/></forecast_conditions></weather></xml_api_reply>
now if the $City_and_State = "Bablablablalba"
then this is what I get:
<?xml version="1.0"?>
-<xml_api_reply version="1">-<weather section="0" row="0"
mobile_zipped="1" mobile_row="0" tab_id="0" module_id="0"><problem_cause
data=""/></weather></xml_api_reply>
So the first one has the weather data and the 2nd one does not.
How can I check if the weather data exists ? (maybe check if the element
exists or child or something like this ?)
I tried the following but it doesn't work.
if( $xml->weather->current_conditions->condition->attributes()->data !=
'' ) {
echo 'Weather Data Exists';
} else {
echo 'Weather Data Does NOT Exists';
}
No comments:
Post a Comment