Home > Backend Development > PHP Tutorial > An example of php weather forecast code

An example of php weather forecast code

WBOY
Release: 2016-07-25 08:55:57
Original
1504 people have browsed it
  1. /**
  2. * php weather forecast code
  3. * by bbs.it-home.org
  4. */
  5. if(!isset($_GET['q'])){
  6. //Query the location based on IP
  7. $p = file_get_contents("http: //bbs.it-home.org/ip/?q={$_SERVER['HTTP_X_FORWARDED_FOR']}");
  8. }
  9. else{
  10. $p = $_GET['q'];
  11. }
  12. $k = 0;
  13. $encoding = mb_detect_encoding($p);
  14. if($encoding != "UTF-8"){
  15. $p = mb_convert_encoding($p,"utf-8","gbk");
  16. }
  17. $ p_arr = array(
  18. "01" => "Beijing",
  19. "02" => "Shanghai",
  20. "03" => "Tianjin",
  21. "04" => "Chongqing",
  22. " 05" => "Heilongjiang",
  23. "06" => "Jilin",
  24. "07" => "Liaoning",
  25. "08" => "Inner Mongolia",
  26. "09" => " "Hebei",
  27. "10" => "Shanxi",
  28. "11" => "Shaanxi",
  29. "12" => "Shandong",
  30. "13" => "Xinjiang",
  31. "14 " => "Tibet",
  32. "15" => "Qinghai",
  33. "16" => "Gansu",
  34. "17" => "Ningxia",
  35. "18" => "Henan ",
  36. "19" => "Jiangsu",
  37. "20" => "Hubei",
  38. "21" => "Zhejiang",
  39. "22" => "Anhui",
  40. "23" => "Fujian",
  41. "24" => "Jiangxi",
  42. "25" => "Hunan",
  43. "26" => "Guizhou",
  44. "27" => "Sichuan" ,
  45. "28" => "Guangdong",
  46. "29" => "Yunnan",
  47. "30" => "Guangxi",
  48. "31" => "Hainan",
  49. "32" = > "Hong Kong",
  50. "33" => "Macau",
  51. "34" => "Taiwan"
  52. );
  53. function find(&$item,$key,$data){
  54. global $k ;
  55. if(preg_match("/$item/u",$data)){
  56. $k = $key;
  57. }
  58. }
  59. function get_data_arr($key){
  60. if(!file_exists("./data/ city{$key}.xml")){
  61. $c =file_get_contents( "http://m.weather.com.cn/data5/city{$key}.xml" );
  62. file_put_contents("./data/ city{$key}.xml",$c);
  63. }
  64. else{
  65. $c = file_get_contents("./data/city{$key}.xml");
  66. }
  67. $arr = explode("," ,$c);
  68. foreach($arr as $v){
  69. $data = explode("|",$v);
  70. $ret[$data[0]] = $data[1];
  71. }
  72. return $ret;
  73. }
  74. array_walk($p_arr,'find',$p);
  75. array_walk(get_data_arr($k),'find',$p);
  76. array_walk(get_data_arr($k),'find', $p);
  77. $ccode = get_data_arr($k);
  78. echo file_get_contents("http://m.weather.com.cn/data/{$ccode[$k]}.html");
  79. ?>
Copy code

Articles you may be interested in:

PHP calls yahoo sina api weather forecast implementation code Example code of php calling google weather api Google API php code to get current weather information


Related labels:
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