PHP 解析google天气xml文件
下面是使用PHP解析google天气的代码<br><br><div id="codeText" class="codeText"><ol style="margin: 0pt 1px 0pt 0pt; padding: 5px 0pt;" start="1" class="dp-css"><li><span style="color: rgb(0, 0, 0);">function fromGoogle<span style="color: rgb(0, 0, 204);">(</span><span style="color: rgb(0, 0, 204);">$</span>city<span style="color: rgb(0, 0, 204);">)</span><span style="color: rgb(0, 0, 204);">{</span><br></span></li><li> $city <span style="color: rgb(0, 0, 204);">=</span> mb_convert_encoding<span style="color: rgb(0, 0, 204);">(</span><span style="color: rgb(0, 0, 204);">$</span>city<span style="color: rgb(0, 0, 204);">,</span> <span style="color: rgb(255, 0, 255);">'UTF-8'</span><span style="color: rgb(0, 0, 204);">,</span> <span style="color: rgb(255, 0, 255);">'gb2312'</span><span style="color: rgb(0, 0, 204);">)</span><span style="color: rgb(0, 0, 204);">;</span><br></li><li>
$city <span style="color: rgb(0, 0, 204);">=</span> Pinyin<span style="color: rgb(0, 0, 204);">(</span><span style="color: rgb(0, 0, 204);">$</span>city<span style="color: rgb(0, 0, 204);">,</span>1<span style="color: rgb(0, 0, 204);">)</span><span style="color: rgb(0, 0, 204);">;</span><br></li><li>
print_r<span style="color: rgb(0, 0, 204);">(</span><span style="color: rgb(255, 0, 255);">"http://www.google.com/ig/api?hl=zh-cn&weather=$city"</span><span style="color: rgb(0, 0, 204);">)</span><span style="color: rgb(0, 0, 204);">;</span><br></li><li>
$<span style="color: rgb(255, 0, 0);">content</span> <span style="color: rgb(0, 0, 204);">=</span> file_get_contents<span style="color: rgb(0, 0, 204);">(</span><span style="color: rgb(255, 0, 255);">"http://www.google.com/ig/api?hl=zh-cn&weather=beijing"</span><span style="color: rgb(0, 0, 204);">)</span><span style="color: rgb(0, 0, 204);">;</span><br></li><li>
$<span style="color: rgb(255, 0, 0);">content</span> <span style="color: rgb(0, 0, 204);">|</span><span style="color: rgb(0, 0, 204);">|</span> die<span style="color: rgb(0, 0, 204);">(</span><span style="color: rgb(255, 0, 255);">"No such city's data"</span><span style="color: rgb(0, 0, 204);">)</span><span style="color: rgb(0, 0, 204);">;</span><br></li><li>
$<span style="color: rgb(255, 0, 0);">content</span> <span style="color: rgb(0, 0, 204);">=</span> mb_convert_encoding<span style="color: rgb(0, 0, 204);">(</span><span style="color: rgb(0, 0, 204);">$</span><span style="color: rgb(255, 0, 0);">content</span><span style="color: rgb(0, 0, 204);">,</span> <span style="color: rgb(255, 0, 255);">'UTF-8'</span><span style="color: rgb(0, 0, 204);">,</span> <span style="color: rgb(255, 0, 255);">'gb2312'</span><span style="color: rgb(0, 0, 204);">)</span><span style="color: rgb(0, 0, 204);">;</span><br></li><li>
$xml <span style="color: rgb(0, 0, 204);">=</span> simplexml_load_string<span style="color: rgb(0, 0, 204);">(</span><span style="color: rgb(0, 0, 204);">$</span><span style="color: rgb(255, 0, 0);">content</span><span style="color: rgb(0, 0, 204);">)</span><span style="color: rgb(0, 0, 204);">;</span><br></li><li>
$date <span style="color: rgb(0, 0, 204);">=</span> $xml<span style="color: rgb(0, 0, 204);">-</span><span style="color: rgb(0, 0, 204);">></span>weather<span style="color: rgb(0, 0, 204);">-</span><span style="color: rgb(0, 0, 204);">></span>forecast_information<span style="color: rgb(0, 0, 204);">-</span><span style="color: rgb(0, 0, 204);">></span>forecast_date<span style="color: rgb(0, 0, 204);">-</span><span style="color: rgb(0, 0, 204);">></span>attributes<span style="color: rgb(0, 0, 204);">(</span><span style="color: rgb(0, 0, 204);">)</span><span style="color: rgb(0, 0, 204);">;</span><br></li><li>
<br></li><li>
$forecastinfo <span style="color: rgb(0, 0, 204);">=</span> $xml<span style="color: rgb(0, 0, 204);">-</span><span style="color: rgb(0, 0, 204);">></span>weather<span style="color: rgb(0, 0, 204);">-</span><span style="color: rgb(0, 0, 204);">></span>forecast_information<span style="color: rgb(0, 0, 204);">;</span><br></li><li>
$cityname <span style="color: rgb(0, 0, 204);">=</span> $forecastinfo<span style="color: rgb(0, 0, 204);">-</span><span style="color: rgb(0, 0, 204);">></span>city<span style="color: rgb(0, 0, 204);">-</span><span style="color: rgb(0, 0, 204);">></span>attributes<span style="color: rgb(0, 0, 204);">(</span><span style="color: rgb(0, 0, 204);">)</span><span style="color: rgb(0, 0, 204);">;</span><br></li><li>
$postal_code <span style="color: rgb(0, 0, 204);">=</span> $forecastinfo<span style="color: rgb(0, 0, 204);">-</span><span style="color: rgb(0, 0, 204);">></span>postal_code<span style="color: rgb(0, 0, 204);">-</span><span style="color: rgb(0, 0, 204);">></span>attributes<span style="color: rgb(0, 0, 204);">(</span><span style="color: rgb(0, 0, 204);">)</span><span style="color: rgb(0, 0, 204);">;</span><br></li><li>
$forecast_date <span style="color: rgb(0, 0, 204);">=</span> $forecastinfo<span style="color: rgb(0, 0, 204);">-</span><span style="color: rgb(0, 0, 204);">></span>forecast_date<span style="color: rgb(0, 0, 204);">-</span><span style="color: rgb(0, 0, 204);">></span>attributes<span style="color: rgb(0, 0, 204);">(</span><span style="color: rgb(0, 0, 204);">)</span><span style="color: rgb(0, 0, 204);">;</span><br></li><li>
$current_date_time <span style="color: rgb(0, 0, 204);">=</span> $forecastinfo<span style="color: rgb(0, 0, 204);">-</span><span style="color: rgb(0, 0, 204);">></span>current_date_time<span style="color: rgb(0, 0, 204);">-</span><span style="color: rgb(0, 0, 204);">></span>attributes<span style="color: rgb(0, 0, 204);">(</span><span style="color: rgb(0, 0, 204);">)</span><span style="color: rgb(0, 0, 204);">;</span><br></li><li>
<br></li><li>
$current <span style="color: rgb(0, 0, 204);">=</span> $xml<span style="color: rgb(0, 0, 204);">-</span><span style="color: rgb(0, 0, 204);">></span>weather<span style="color: rgb(0, 0, 204);">-</span><span style="color: rgb(0, 0, 204);">></span>current_conditions<span style="color: rgb(0, 0, 204);">;</span><br></li><li>
$condition <span style="color: rgb(0, 0, 204);">=</span> $current<span style="color: rgb(0, 0, 204);">-</span><span style="color: rgb(0, 0, 204);">></span>condition<span style="color: rgb(0, 0, 204);">-</span><span style="color: rgb(0, 0, 204);">></span>attributes<span style="color: rgb(0, 0, 204);">(</span><span style="color: rgb(0, 0, 204);">)</span><span style="color: rgb(0, 0, 204);">;</span><br></li><li>
$temp_c <span style="color: rgb(0, 0, 204);">=</span> $current<span style="color: rgb(0, 0, 204);">-</span><span style="color: rgb(0, 0, 204);">></span>temp_c<span style="color: rgb(0, 0, 204);">-</span><span style="color: rgb(0, 0, 204);">></span>attributes<span style="color: rgb(0, 0, 204);">(</span><span style="color: rgb(0, 0, 204);">)</span><span style="color: rgb(0, 0, 204);">;</span><br></li><li>
$humidity <span style="color: rgb(0, 0, 204);">=</span> $current<span style="color: rgb(0, 0, 204);">-</span><span style="color: rgb(0, 0, 204);">></span>humidity<span style="color: rgb(0, 0, 204);">-</span><span style="color: rgb(0, 0, 204);">></span>attributes<span style="color: rgb(0, 0, 204);">(</span><span style="color: rgb(0, 0, 204);">)</span><span style="color: rgb(0, 0, 204);">;</span><br></li><li>
$<span style="color: rgb(255, 0, 0);">icon</span> <span style="color: rgb(0, 0, 204);">=</span> $current<span style="color: rgb(0, 0, 204);">-</span><span style="color: rgb(0, 0, 204);">></span><span style="color: rgb(255, 0, 0);">icon</span><span style="color: rgb(0, 0, 204);">-</span><span style="color: rgb(0, 0, 204);">></span>attributes<span style="color: rgb(0, 0, 204);">(</span><span style="color: rgb(0, 0, 204);">)</span><span style="color: rgb(0, 0, 204);">;</span><br></li><li>
$windMsg <span style="color: rgb(0, 0, 204);">=</span> $current<span style="color: rgb(0, 0, 204);">-</span><span style="color: rgb(0, 0, 204);">></span>wind_condition<span style="color: rgb(0, 0, 204);">-</span><span style="color: rgb(0, 0, 204);">></span>attributes<span style="color: rgb(0, 0, 204);">(</span><span style="color: rgb(0, 0, 204);">)</span><span style="color: rgb(0, 0, 204);">;</span><br></li><li>
$condition <span style="color: rgb(0, 0, 204);">&</span><span style="color: rgb(0, 0, 204);">&</span> $condition <span style="color: rgb(0, 0, 204);">=</span> $xml<span style="color: rgb(0, 0, 204);">-</span><span style="color: rgb(0, 0, 204);">></span>weather<span style="color: rgb(0, 0, 204);">-</span><span style="color: rgb(0, 0, 204);">></span>forecast_conditions<span style="color: rgb(0, 0, 204);">-</span><span style="color: rgb(0, 0, 204);">></span>condition<span style="color: rgb(0, 0, 204);">-</span><span style="color: rgb(0, 0, 204);">></span>attributes<span style="color: rgb(0, 0, 204);">(</span><span style="color: rgb(0, 0, 204);">)</span><span style="color: rgb(0, 0, 204);">;</span><br></li><li>
<br></li><li>
$today_info<span style="color: rgb(0, 0, 204);">[</span>currentdate<span style="color: rgb(0, 0, 204);">]</span> <span style="color: rgb(0, 0, 204);">=</span> $forecast_date<span style="color: rgb(0, 0, 204);">;</span><br></li><li>
$today_info<span style="color: rgb(0, 0, 204);">[</span>currenttime<span style="color: rgb(0, 0, 204);">]</span> <span style="color: rgb(0, 0, 204);">=</span> $current_date_time<span style="color: rgb(0, 0, 204);">;</span><br></li><li>
$today_info<span style="color: rgb(0, 0, 204);">[</span>cityname<span style="color: rgb(0, 0, 204);">]</span> <span style="color: rgb(0, 0, 204);">=</span> $postal_code<span style="color: rgb(0, 0, 204);">;</span><br></li><li>
$today_info<span style="color: rgb(0, 0, 204);">[</span>humidity<span style="color: rgb(0, 0, 204);">]</span> <span style="color: rgb(0, 0, 204);">=</span> $humidity<span style="color: rgb(0, 0, 204);">;</span><br></li><li>
$today_info<span style="color: rgb(0, 0, 204);">[</span>windMsg<span style="color: rgb(0, 0, 204);">]</span> <span style="color: rgb(0, 0, 204);">=</span> $windMsg<span style="color: rgb(0, 0, 204);">;</span><br></li><li>
$today_info<span style="color: rgb(0, 0, 204);">[</span>condition<span style="color: rgb(0, 0, 204);">]</span> <span style="color: rgb(0, 0, 204);">=</span> $condition<span style="color: rgb(0, 0, 204);">;</span><br></li><li>
$today_info<span style="color: rgb(0, 0, 204);">[</span>tempcur<span style="color: rgb(0, 0, 204);">]</span> <span style="color: rgb(0, 0, 204);">=</span> $temp_c<span style="color: rgb(0, 0, 204);">;</span><br></li><li>
$<span style="color: rgb(0, 0, 255);">i</span> <span style="color: rgb(0, 0, 204);">=</span> 0<span style="color: rgb(0, 0, 204);">;</span><br></li><li>
<br></li><li>
foreach<span style="color: rgb(0, 0, 204);">(</span><span style="color: rgb(0, 0, 204);">$</span>xml<span style="color: rgb(0, 0, 204);">-</span><span style="color: rgb(0, 0, 204);">></span>weather<span style="color: rgb(0, 0, 204);">-</span><span style="color: rgb(0, 0, 204);">></span>forecast_conditions as $forecast<span style="color: rgb(0, 0, 204);">)</span> <span style="color: rgb(0, 0, 204);">{</span><br></li><li>
$temp <span style="color: rgb(0, 0, 204);">=</span> null<span style="color: rgb(0, 0, 204);">;</span> <br></li><li>
$temp<span style="color: rgb(0, 0, 204);">[</span>low<span style="color: rgb(0, 0, 204);">]</span> <span style="color: rgb(0, 0, 204);">=</span> $forecast<span style="color: rgb(0, 0, 204);">-</span><span style="color: rgb(0, 0, 204);">></span>low<span style="color: rgb(0, 0, 204);">-</span><span style="color: rgb(0, 0, 204);">></span>attributes<span style="color: rgb(0, 0, 204);">(</span><span style="color: rgb(0, 0, 204);">)</span><span style="color: rgb(0, 0, 204);">;</span> <br></li><li>
$temp<span style="color: rgb(0, 0, 204);">[</span>high<span style="color: rgb(0, 0, 204);">]</span> <span style="color: rgb(0, 0, 204);">=</span> $forecast<span style="color: rgb(0, 0, 204);">-</span><span style="color: rgb(0, 0, 204);">></span>high<span style="color: rgb(0, 0, 204);">-</span><span style="color: rgb(0, 0, 204);">></span>attributes<span style="color: rgb(0, 0, 204);">(</span><span style="color: rgb(0, 0, 204);">)</span><span style="color: rgb(0, 0, 204);">;</span> <br></li><li>
$temp<span style="color: rgb(0, 0, 204);">[</span>conditionMsg<span style="color: rgb(0, 0, 204);">]</span> <span style="color: rgb(0, 0, 204);">=</span> $forecast<span style="color: rgb(0, 0, 204);">-</span><span style="color: rgb(0, 0, 204);">></span>condition<span style="color: rgb(0, 0, 204);">-</span><span style="color: rgb(0, 0, 204);">></span>attributes<span style="color: rgb(0, 0, 204);">(</span><span style="color: rgb(0, 0, 204);">)</span><span style="color: rgb(0, 0, 204);">;</span> <br></li><li>
$temp<span style="color: rgb(0, 0, 204);">[</span>day_of_week<span style="color: rgb(0, 0, 204);">]</span> <span style="color: rgb(0, 0, 204);">=</span> $forecast<span style="color: rgb(0, 0, 204);">-</span><span style="color: rgb(0, 0, 204);">></span>day_of_week<span style="color: rgb(0, 0, 204);">-</span><span style="color: rgb(0, 0, 204);">></span>attributes<span style="color: rgb(0, 0, 204);">(</span><span style="color: rgb(0, 0, 204);">)</span><span style="color: rgb(0, 0, 204);">;</span> <br></li><li>
$<span style="color: rgb(0, 0, 255);">i</span><span style="color: rgb(0, 0, 204);">+</span><span style="color: rgb(0, 0, 204);">+</span><span style="color: rgb(0, 0, 204);">;</span><br></li><li>
if<span style="color: rgb(0, 0, 204);">(</span><span style="color: rgb(0, 0, 204);">$</span><span style="color: rgb(0, 0, 255);">i</span> <span style="color: rgb(0, 0, 204);">=</span><span style="color: rgb(0, 0, 204);">=</span> 1<span style="color: rgb(0, 0, 204);">)</span><span style="color: rgb(0, 0, 204);">{</span><br></li><li>
$today_info <span style="color: rgb(0, 0, 204);">=</span> array_merge<span style="color: rgb(0, 0, 204);">(</span><span style="color: rgb(0, 0, 204);">$</span>today_info<span style="color: rgb(0, 0, 204);">,</span><span style="color: rgb(0, 0, 204);">$</span>temp<span style="color: rgb(0, 0, 204);">)</span><span style="color: rgb(0, 0, 204);">;</span><br></li><li>
<span style="color: rgb(0, 0, 204);">}</span>else if<span style="color: rgb(0, 0, 204);">(</span><span style="color: rgb(0, 0, 204);">$</span><span style="color: rgb(0, 0, 255);">i</span> <span style="color: rgb(0, 0, 204);">=</span><span style="color: rgb(0, 0, 204);">=</span> 2 <span style="color: rgb(0, 0, 204);">)</span><span style="color: rgb(0, 0, 204);">{</span><br></li><li>
$SeconDay_info <span style="color: rgb(0, 0, 204);">=</span> array_merge<span style="color: rgb(0, 0, 204);">(</span><span style="color: rgb(0, 0, 204);">$</span>SeconDay_info<span style="color: rgb(0, 0, 204);">,</span><span style="color: rgb(0, 0, 204);">$</span>temp<span style="color: rgb(0, 0, 204);">)</span><span style="color: rgb(0, 0, 204);">;</span><br></li><li>
<span style="color: rgb(0, 0, 204);">}</span>else if<span style="color: rgb(0, 0, 204);">(</span><span style="color: rgb(0, 0, 204);">$</span><span style="color: rgb(0, 0, 255);">i</span> <span style="color: rgb(0, 0, 204);">=</span><span style="color: rgb(0, 0, 204);">=</span> 3 <span style="color: rgb(0, 0, 204);">)</span><span style="color: rgb(0, 0, 204);">{</span><br></li><li>
$ThirdDay_info <span style="color: rgb(0, 0, 204);">=</span> array_merge<span style="color: rgb(0, 0, 204);">(</span><span style="color: rgb(0, 0, 204);">$</span>ThirdDay_info<span style="color: rgb(0, 0, 204);">,</span><span style="color: rgb(0, 0, 204);">$</span>temp<span style="color: rgb(0, 0, 204);">)</span><span style="color: rgb(0, 0, 204);">;</span><br></li><li>
<span style="color: rgb(0, 0, 204);">}</span>else if<span style="color: rgb(0, 0, 204);">(</span><span style="color: rgb(0, 0, 204);">$</span><span style="color: rgb(0, 0, 255);">i</span> <span style="color: rgb(0, 0, 204);">=</span><span style="color: rgb(0, 0, 204);">=</span> 4 <span style="color: rgb(0, 0, 204);">)</span><span style="color: rgb(0, 0, 204);">{</span><br></li><li>
$FourthDay_info <span style="color: rgb(0, 0, 204);">=</span> array_merge<span style="color: rgb(0, 0, 204);">(</span><span style="color: rgb(0, 0, 204);">$</span>FourthDay_info<span style="color: rgb(0, 0, 204);">,</span><span style="color: rgb(0, 0, 204);">$</span>temp<span style="color: rgb(0, 0, 204);">)</span><span style="color: rgb(0, 0, 204);">;</span><br></li><li>
<span style="color: rgb(0, 0, 204);">}</span><br></li><li>
<span style="color: rgb(0, 0, 204);">}</span><br></li><li>
$todayall_info <span style="color: rgb(0, 0, 204);">=</span> Array<span style="color: rgb(0, 0, 204);">(</span><span style="color: rgb(0, 0, 204);">)</span><span style="color: rgb(0, 0, 204);">;</span><br></li><li>
$todayall_info<span style="color: rgb(0, 0, 204);">[</span>0<span style="color: rgb(0, 0, 204);">]</span> <span style="color: rgb(0, 0, 204);">=</span> $today_info<span style="color: rgb(0, 0, 204);">;</span><br></li><li>
$todayall_info<span style="color: rgb(0, 0, 204);">[</span>1<span style="color: rgb(0, 0, 204);">]</span> <span style="color: rgb(0, 0, 204);">=</span> $SeconDay_info<span style="color: rgb(0, 0, 204);">;</span><br></li><li>
$todayall_info<span style="color: rgb(0, 0, 204);">[</span>2<span style="color: rgb(0, 0, 204);">]</span> <span style="color: rgb(0, 0, 204);">=</span> $ThirdDay_info<span style="color: rgb(0, 0, 204);">;</span><br></li><li>
$todayall_info<span style="color: rgb(0, 0, 204);">[</span>3<span style="color: rgb(0, 0, 204);">]</span> <span style="color: rgb(0, 0, 204);">=</span> $FourthDay_info<span style="color: rgb(0, 0, 204);">;</span><br></li><li>
return $todayall_info<span style="color: rgb(0, 0, 204);">;</span><br></li><li>
<span style="color: rgb(0, 0, 204);">}</span></li></ol></div><br>
页:
[1]