php 天氣預報代碼一例

WBOY
發布: 2016-07-25 08:55:57
原創
1473 人瀏覽過
  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代码


來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!