2

我是 PHP/XML 新手,我想添加 Google 的 XML 天气信息。并将该信息添加到我的 .PHP 页面。

我将以这个网址为例:https ://www.google.com/ig/api?weather=12601&hl=en&referrer=googlecalendar

我在那个 URL 得到的输出是:

<?xml version="1.0"?>
<xml_api_reply version="1">
<weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0" >
<forecast_information>
<city data="Poughkeepsie, NY"/>
<postal_code data="12601"/>
<latitude_e6 data=""/>
<longitude_e6 data=""/>
<forecast_date data="2013-08-15"/>
<current_date_time data="1970-01-01 00:00:00 +0000"/>
<unit_system data="US"/>
</forecast_information>
<current_conditions>
<condition data="Partly Cloudy"/>
<temp_f data="78"/>
<temp_c data="26"/>
<humidity data="Humidity: 44%"/>
<icon data="/ig/images/weather/partly_cloudy.gif"/>
<wind_condition data="Wind: N at 2 mph"/>
</current_conditions>
<forecast_conditions>
<day_of_week data="Thu"/>
<low data="52"/>
<high data="81"/>
<icon data="/ig/images/weather/sunny.gif"/>
<condition data="Clear"/>
</forecast_conditions>
<forecast_conditions>
<day_of_week data="Fri"/>
<low data="52"/>
<high data="82"/>
<icon data="/ig/images/weather/partly_cloudy.gif"/>
<condition data="Partly Cloudy"/>
</forecast_conditions>
<forecast_conditions>
<day_of_week data="Sat"/>
<low data="57"/>
<high data="84"/>
<icon data="/ig/images/weather/partly_cloudy.gif"/>
<condition data="Partly Cloudy"/>
</forecast_conditions>
<forecast_conditions>
<day_of_week data="Sun"/>
<low data="59"/>
<high data="79"/>
<icon data="/ig/images/weather/cloudy.gif"/>
<condition data="Cloudy"/>
</forecast_conditions>
</weather>
</xml_api_reply>

我只想提取以下信息。但我不知道该怎么做,我想使用 min. 尽可能多的代码。

喜欢它显示在 .PHP 页面中,如下所示:

Currently: Partly Cloudy - 78*
Thu: Clear - Hi: 81* Lo: 52*
Fri: Partly Cloudy - Hi: 82* Lo: 52*
Sat: Partly Cloudy - Hi: 84* Lo: 57*
Sun: Cloudy - Hi: 79* Lo: 59*

谢谢。

4

1 回答 1

3

用于SimpleXML解析字符串并转换为对象。

$string = '...';
$xml = simplexml_load_string($string); 
print_r($xml);

演示: http ://codepad.org/ig8PuWBQ

于 2013-08-15T20:17:54.370 回答