Php obtains 72-hour weather forecasts for cities [www.weather.com.cn]
Php obtains 72-hour weather forecasts for cities Download the weather forecast content data.txt from various cities: http://3.ibtf.sinaapp.com/data.txt?#] Demo address: http://3.ibtf.sinaapp.com /? City = % E7 % 9F % B3 % E5 % AE % B6 % E5 % BA % 84
- Function getweather($city,$data='data.txt ') // Obtain the weather forecast content.
- {
- $ Urlarr = unserialize (file_get_contents ($ data ));
- If ($ urlarr [$ city])
- {
- $ Url = $ urlarr [$ city]; $ text = $ city;
- $ Lines_string = file_get_contents ($ url );
- $ Lines_string = explode (" ', '', $ Lines_string [1]), str_replace ('2 -->','', $ lines_string [2]), str_replace ('3 --> ', '', $ lines_string_3 [0]);
- For ($ I = 0; $ I <count ($ lines_array); $ I ++)
- {
- $ Tiqian = array ("℃", "high temperature", "Low temperature"); $ tihou = array ("degree ","",""); $ nowarray = str_replace ($ tiqian, $ tihou, strip_tags ($ lines_array [$ I]);
- $ Datearray = explode ("day", $ nowarray); $ wtext [$ I] = trim ($ datearray [0]). "day"; // Get date
- $ Weather = explode ("daytime", $ nowarray); $ weather = explode ("nighttime", $ weather [1]);
- $ Paiarr = wchangearray (explode ("r", $ weather [0]); // daytime weather
- $ Yearr = wchangearray (explode ("r", $ weather [1]); // nighttime weather
- If ($ baiarr [0] ==$ yearr [0]) {$ wtext [$ I]. = $ baiarr [0];} else {$ wtext [$ I]. = $ baiarr [0]. "Turn ". $ yearr [0];} // add the weather to the return value
- $ Wtext [$ I]. = $ baiarr [1]. "to". $ yearr [1]; // add the temperature to the return value
- If ($ baiarr [2] ==$ yearr [2]) {$ wtext [$ I]. = $ baiarr [2];} else {$ wtext [$ I]. = str_replace ("wind", "", $ mongoarr [2]. "Turn ". $ yearr [2]); $ wtext [$ I]. = "wind";} // add the wind direction to the return value
- If ($ baiarr [3]! = "Breeze") {$ wtext [$ I]. = $ baiarr [3];} // add the wind to the return value
- }
- Return $ text. implode ("", $ wtext );
- } Else {
- Return "unable to obtain the weather information of the city! ";
- }
- }
- Function wchangearray ($ arr) // sorts the values of an array.
- {
- Foreach ($ arr as $ v)
- {If (! Trim ($ v) continue;
- $ Value [] = trim ($ v);} return $ value;
- }
|