PHP obtains the 72-hour weather forecast content of each city
Obtain weather forecast content data.txt download for each city: http://3.ibtf.sinaapp.com/data.txt [note the web page encoding]
Demo address:
http://3.ibtf.sinaapp.com/?city=%E7%9F%B3%E5%AE%B6%E5%BA%84
- function getweather($city,$data='data.txt')//Get 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("日", $nowarray);$wtext[$i]=trim($datearray[0])."Day";//Get the date
- $weather=explode("Day",$nowarray);$weather=explode("Night" ,$weather[1]);
- $baiarr=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]."To".$yearr[0];}//Add 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","",$baiarr[2]."turn".$yearr[2]);$wtext[$i].="wind";}//Add the wind direction to the return value
- if ($baiarr[3]!="Breeze"){$wtext[$i].=$baiarr[3];}//Add wind force to the return value
- }
- return $text.implode("",$wtext );
- }else{
- return "Unable to obtain weather information for this city!";
- }
- }
-
- function wchangearray($arr)//Sort the array by key value
- {
- foreach($arr as $v )
- {if(!trim($v))continue;
- $value[]=trim($v);}return $value;
- }
Copy code
|