微信公眾平台開發(4)天氣預報


微信公眾平台 微信公眾平台開發 消息接口 微信天氣預報 天氣預報接口 中國天氣網

原文:http://www.cnblogs.com/txw1958/archive/2013/02/07/weixin-if4-weather-forecast.html 

 

一、數據接口

百度提供天氣預報查詢接口API,可以根據經緯度/城市名查詢天氣情況,我們可以在微信公眾平台開發中調用這一接口。

接口

http://api.map.baidu.com/telematics/v3/weather?location=北京&output=json&ak=yourak

接口參數說明

參數類型 參數名稱 是否必須 具體描述
String location true 輸入城市名或經緯度,城市名稱如:北京或者131,經緯度格式為lng,lat坐標如: location=116.305145,39.982368;全國值為all,返回省會城市自治區,港澳台天氣情況多城市天氣預報中間"|"分隔,location=116.305145,39.982368| 122.305145,36.982368|….
String output false 輸出的數據格式,默認為xml格式,當output設置為’json’時,輸出的為json格式的數據;
String coord_type false 請求參數坐標類型,默認為gcj02經緯度坐標。允許的值為bd09ll、bd09mc、gcj02、wgs84。bd09ll表示百度經緯度坐標,bd09mc表示百度墨卡托坐標,gcj02表示經過國測局加密的坐標。wgs84表示gps獲取的坐標。

返回結果

參數名稱 含義 說明
currentCity 當前城市 返回城市名
status 返回結果狀態信息  
date 當前時間 年-月-日
results 天氣預報信息 白天可返回近期3天的天氣情況(今天、明天、后天)、晚上可返回近期4天的天氣情況(今天、明天、后天、大后天)
results.currentCity 當前城市  
results.weather_data weather_data.date 天氣預報時間  
weather_data.dayPictureUrl 白天的天氣預報圖片url  
weather_data.nightPictureUrl 晚上的天氣預報圖片url  
weather_data.weather 天氣狀況 所有天氣情況(”|”分隔符):晴|多雲|陰|陣雨|雷陣雨|雷陣雨伴有冰雹|雨夾雪|小雨|中雨|大雨|暴雨|大暴雨|特大暴雨|陣雪|小雪|中雪|大雪|暴雪|霧|凍雨|沙塵暴|小雨轉中雨|中雨轉大雨|大雨轉暴雨|暴雨轉大暴雨|大暴雨轉特大暴雨|小雪轉中雪|中雪轉大雪|大雪轉暴雪|浮塵|揚沙|強沙塵暴|霾
weather_data.wind 風力  
weather_data.temperature 溫度  

返回json格式的數據

{
    "error": 0,
    "status": "success",
    "date": "2014-05-03",
    "results": [
        {
            "currentCity": "北京",
            "weather_data": [
                {
                    "date": "周六(今天, 實時:23℃)",
                    "dayPictureUrl": "http://api.map.baidu.com/images/weather/day/leizhenyu.png",
                    "nightPictureUrl": "http://api.map.baidu.com/images/weather/night/duoyun.png",
                    "weather": "雷陣雨轉多雲",
                    "wind": "北風5-6級",
                    "temperature": "24 ~ 11℃"
                },
                {
                    "date": "周日",
                    "dayPictureUrl": "http://api.map.baidu.com/images/weather/day/duoyun.png",
                    "nightPictureUrl": "http://api.map.baidu.com/images/weather/night/qing.png",
                    "weather": "多雲轉晴",
                    "wind": "北風4-5級",
                    "temperature": "19 ~ 8℃"
                },
                {
                    "date": "周一",
                    "dayPictureUrl": "http://api.map.baidu.com/images/weather/day/qing.png",
                    "nightPictureUrl": "http://api.map.baidu.com/images/weather/night/qing.png",
                    "weather": "晴",
                    "wind": "微風",
                    "temperature": "21 ~ 9℃"
                },
                {
                    "date": "周二",
                    "dayPictureUrl": "http://api.map.baidu.com/images/weather/day/duoyun.png",
                    "nightPictureUrl": "http://api.map.baidu.com/images/weather/night/qing.png",
                    "weather": "多雲轉晴",
                    "wind": "微風",
                    "temperature": "21 ~ 10℃"
                }
            ]
        }
    ]
}

 

二、開發實現

獲取百度天氣json數據的代碼如下:

<?php

// var_dump(getWeatherInfo("深圳"));

function getWeatherInfo($cityName)
{
    if ($cityName == "" || (strstr($cityName, "+"))){
        return "發送天氣+城市,例如'天氣深圳'";
    }
    $url = "http://api.map.baidu.com/telematics/v3/weather?location=".urlencode($cityName)."&output=json&ak=ECe3698802b9bf4457f0e01b544eb6aa";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    curl_close($ch);
    $result = json_decode($output, true);
    if ($result["error"] != 0){
        return $result["status"];
    }
    $curHour = (int)date('H',time());
    $weather = $result["results"][0];
    $weatherArray[] = array("Title" =>$weather['currentCity']."天氣預報", "Description" =>"", "PicUrl" =>"", "Url" =>"");
    for ($i = 0; $i < count($weather["weather_data"]); $i++) {
        $weatherArray[] = array("Title"=>
            $weather["weather_data"][$i]["date"]."\n".
            $weather["weather_data"][$i]["weather"]." ".
            $weather["weather_data"][$i]["wind"]." ".
            $weather["weather_data"][$i]["temperature"],
        "Description"=>"", 
        "PicUrl"=>(($curHour >= 6) && ($curHour < 18))?$weather["weather_data"][$i]["dayPictureUrl"]:$weather["weather_data"][$i]["nightPictureUrl"], "Url"=>"");
    }
    return $weatherArray;
}


?>

在微信中調用的方式如下:

 

<?php
/*
    方倍工作室
    CopyRight 2013 All Rights Reserved
*/

    private function receiveText($object)
    {
        $keyword = trim($object->Content);

        include("weather2.php");
        $content = getWeatherInfo($keyword);
        // $result = $this->transmitText($object, count($content));
        $result = $this->transmitNews($object, $content);
        return $result;
    }
   
}

 

 

 

三,效果演示

直接發送城市名稱查詢天氣預報

 

四、其他方式查詢

除了城市名稱之外,還可以使用郵編、電話區號等方式進行查詢,其實是使用轉換的方式,比如010是北京的電話區號,就轉去查詢北京的天氣。

你可以下載 weather.sql.zip ,該數據庫是方倍工作室整理收集的主要城市、郵編、區號、拼音、及簡拼等城市資料。

開發完成之后,效果如圖如示:

使用城市名稱、拼音、電話區號查詢天氣預報

  

使用郵編、地址位置、語音(聲音)查詢天氣預報

  

 

 

 

 

 

★購買天氣預報功能源碼 點擊進入

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM