来源:自学PHP网 时间:2015-04-14 12:58 作者: 阅读:次
[导读] 微信公众平台 微信公众平台开发 消息接口 微信天气预报天气预报接口 中国天气网一、数据接口百度提供天气预报查询接口API,可以根据经纬度 城市名查询天气情况,我们可以在微信公...
|
微信公众平台 微信公众平台开发 消息接口 微信天气预报 天气预报接口 中国天气网 一、数据接口 百度提供天气预报查询接口API,可以根据经纬度/城市名查询天气情况,我们可以在微信公众平台开发中调用这一接口。 接口http://api.map.baidu.com/telematics/v3/weather?location=北京&output=json&ak=5slgyqGDENN7Sy7pw29IUvrZ
接口参数说明
返回结果
返回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:http://www.2cto.com/uploadfile/2014/0526/20140526010253184.zip ,该数据库是方倍工作室整理收集的主要城市、邮编、区号、拼音、及简拼等城市资料。 开发完成之后,效果如图如示: 使用城市名称、拼音、电话区号查询天气预报
使用邮编、地址位置、语音(声音)查询天气预报
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习
京ICP备14009008号-1@版权所有www.zixuephp.com
网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com