首页 >后端开发 >php教程 >php 天气预报代码一例

php 天气预报代码一例

WBOY
WBOY原创
2016-07-25 08:55:571492浏览
  1. /**
  2. * php 天气预报代码
  3. * by bbs.it-home.org
  4. */
  5. if(!isset($_GET['q'])){
  6. //根据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" => "北京",
  19. "02" => "上海",
  20. "03" => "天津",
  21. "04" => "重庆",
  22. "05" => "黑龙江",
  23. "06" => "吉林",
  24. "07" => "辽宁",
  25. "08" => "内蒙古",
  26. "09" => "河北",
  27. "10" => "山西",
  28. "11" => "陕西",
  29. "12" => "山东",
  30. "13" => "新疆",
  31. "14" => "西藏",
  32. "15" => "青海",
  33. "16" => "甘肃",
  34. "17" => "宁夏",
  35. "18" => "河南",
  36. "19" => "江苏",
  37. "20" => "湖北",
  38. "21" => "浙江",
  39. "22" => "安徽",
  40. "23" => "福建",
  41. "24" => "江西",
  42. "25" => "湖南",
  43. "26" => "贵州",
  44. "27" => "四川",
  45. "28" => "广东",
  46. "29" => "云南",
  47. "30" => "广西",
  48. "31" => "海南",
  49. "32" => "香港",
  50. "33" => "澳门",
  51. "34" => "台湾"
  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. ?>
复制代码

您可能感兴趣的文章:

php调用yahoo sina api天气预报的实现代码 php调用google天气api的实例代码 Google API 获取当前天气信息的php代码


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn