Home > Backend Development > PHP Tutorial > Get 72-hour weather forecast for each city with php [China Weather Network]

Get 72-hour weather forecast for each city with php [China Weather Network]

WBOY
Release: 2016-07-25 09:07:04
Original
1819 people have browsed it
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
  1. function getweather($city,$data='data.txt')//Get weather forecast content
  2. {
  3. $urlarr=unserialize(file_get_contents($data));
  4. if($urlarr[$city])
  5. {
  6. $url = $urlarr[$city];$text=$city;
  7. $lines_string = file_get_contents($url);
  8. $lines_string = explode("','',$lines_string[1]),str_replace('2-->','',$lines_string[2]),str_replace('3-->','' ,$lines_string_3[0]));
  9. for($i=0;$i< count($lines_array); $i ++)
  10. {
  11. $tiqian=array("℃","High temperature","Low temperature" );$tihou=array("degree","","");$nowarray=str_replace($tiqian,$tihou,strip_tags($lines_array[$i]));
  12. $datearray=explode("日", $nowarray);$wtext[$i]=trim($datearray[0])."Day";//Get the date
  13. $weather=explode("Day",$nowarray);$weather=explode("Night" ,$weather[1]);
  14. $baiarr=wchangearray(explode("r",$weather[0]));//Daytime weather
  15. $yearr=wchangearray(explode("r",$weather[1]) );//Nighttime weather
  16. if($baiarr[0]==$yearr[0]){$wtext[$i].=$baiarr[0];}else{$wtext[$i].=$baiarr [0]."To".$yearr[0];}//Add weather to the return value
  17. $wtext[$i].=$baiarr[1]."To".$yearr[1];/ /Add the temperature to the return value
  18. 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
  19. if ($baiarr[3]!="Breeze"){$wtext[$i].=$baiarr[3];}//Add wind force to the return value
  20. }
  21. return $text.implode("",$wtext );
  22. }else{
  23. return "Unable to obtain weather information for this city!";
  24. }
  25. }
  26. function wchangearray($arr)//Sort the array by key value
  27. {
  28. foreach($arr as $v )
  29. {if(!trim($v))continue;
  30. $value[]=trim($v);}return $value;
  31. }
Copy code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template