我正在加载一个 simpleXML url,如下所示:
$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);
我得到的数据是:
<?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>
现在如果$City_and_State = "Bablablablalba"
那么这就是我得到的:
<?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>
所以第一个有天气数据,第二个没有。
如何检查天气数据是否存在?(也许检查元素是否存在或子元素或类似的东西?)
我尝试了以下但它不起作用。
if( $xml->weather->current_conditions->condition->attributes()->data != '' ) {
echo 'Weather Data Exists';
} else {
echo 'Weather Data Does NOT Exists';
}