天气预报接口代码分享-公众平台接口开发(一)
开放平台,可以利用现在网络上的各种工具接口,为自己的公众平台增加亮点。天气查询,快递查询,手机归属地查询,身份证查询,公交查询,火车查询,健康指数查询,实时翻译(语音),百度百科,百度问答,人品计算,笑话……等接口开发都已经成熟了。
今天我们先来看看最简单普遍的天气预报功能
中国天气网天气预报接口(官方接口)
请求地址: m.weather/data/101010100.html
首先我们创建一个类。
保存为:weather.class.php 天气预报的处理类
<?php
// 天气类
class getweather{
public function getw($str){
if ($code){
$content = file_get_contents("m.weather/data/{$code}.html");
if(!$content){
file_put_contents('', '没有接受到天气数据', FILE_APPEND);
return '爱油,抱歉,暂时没有取到天气数据,请稍后再试';
}
$result=json_decode($content,true);
$info=$result['weatherinfo'];
//文本形式
$strw="{$info['date_y']} {$info['week']} \n{$info['city']}的天气情况\n";
$strw .= "今天:({$info['temp1']}){$info['weather1']}{$info['wind1']}{$info['fl1']}。\n";
$strw .= "明天:({$info['temp2']}){$info['weather2']}{$info['wind2']}{$info['fl2']}。\n";
return $strw;
}else{
return '没有获取到该城市的天气,请确定输入了正确的城市名称,如\'福州天气\'';
}
}
/**
* 验证接受到的数据是否合法
* @param string $str 传入的接受到的数据
* @return mixed|boolean 有数据返回$code,没有数据返回false;
*
*/
private function check($str){
// 引入天气代码数组
include"city_code.php";
if (is_array($CSArr)) {
return array_search($str, $CSArr);
}else {
return false;
}
}
}
?>
返回数据说明:
JSON格式解释
{
"weatherinfo":{
<!-- 基本信息 -->
"weatherinfo":{
<!-- 基本信息 -->
"city":"北京",
"city_en":"beijing",
"date_y":"2013年6月21日",
"date":"",
"week":"星期五",
"fchh":"08",
"cityid":"101010100",
<!-- 从今天开始到第六天的每天的天气情况,这里的温度是摄氏温度 -->
"temp1":"29℃~23℃","temp2":"26℃~20℃","temp3":"24℃~20℃","temp4":
"city_en":"beijing",
"date_y":"2013年6月21日",
"date":"",
"week":"星期五",
"fchh":"08",
"cityid":"101010100",
<!-- 从今天开始到第六天的每天的天气情况,这里的温度是摄氏温度 -->
"temp1":"29℃~23℃","temp2":"26℃~20℃","temp3":"24℃~20℃","temp4":
"25℃~20℃","temp5":"24℃~21℃","temp6":"25℃~22℃",
<!-- 从今天开始到第六天的每天的天气情况,这里的温度是华氏温度 -->
"tempF1":"84.2℉~73.4℉","tempF2":"78.8℉~68℉","tempF3":"75.2℉~68℉","tempF4":"77℉~68℉",
<!-- 从今天开始到第六天的每天的天气情况,这里的温度是华氏温度 -->
"tempF1":"84.2℉~73.4℉","tempF2":"78.8℉~68℉","tempF3":"75.2℉~68℉","tempF4":"77℉~68℉",
"tempF5":"75.2℉~69.8℉","tempF6":"77℉~71.6℉",
<!-- 天气描述 -->
"weather1":"阵雨转中雨","weather2":"中雨转小雨","weather3":"小雨","weather4":"小雨",
"weather1":"阵雨转中雨","weather2":"中雨转小雨","weather3":"小雨","weather4":"小雨",
"weather5":"小雨转阵雨","weather6":"阵雨转小雨",
<!-- 天气描述图片序号 -->
"img1":"3","img2":"8","img3":"8","img4":"7","img5":"7","img6":"99","img7":"7",
<!-- 天气描述图片序号 -->
"img1":"3","img2":"8","img3":"8","img4":"7","img5":"7","img6":"99","img7":"7",
"img8":"99","img9":"7","img10":"3","img11":"3","img12":"7","img_single":"3",
<!-- 图片名称 -->
"img_title1":"阵雨","img_title2":"中雨","img_title3":"中雨","img_title4":"小雨","img_title5":
<!-- 图片名称 -->
"img_title1":"阵雨","img_title2":"中雨","img_title3":"中雨","img_title4":"小雨","img_title5":
"小雨","img_title6":"小雨","img_title7":"小雨","img_title8":"小雨","img_title9":"小雨","img_title10":"阵雨","img_title11":"阵雨","img_title12":"小雨","img_title_single":"代码转换阵雨",
<!-- 风速描述 -->
"wind1":"微风","wind2":"微风","wind3":"微风","wind4":"微风","wind5":"微风","wind6":
<!-- 风速描述 -->
"wind1":"微风","wind2":"微风","wind3":"微风","wind4":"微风","wind5":"微风","wind6":
"微风","fx1":"微风","fx2":"微风",
<!-- 风速级别描述 -->
"fl1":"小于3级","fl2":"小于3级","fl3":"小于3级","fl4":"小于3级","fl5":"小于3级","fl6":"小于3级",
<!-- 今天穿衣指数 -->
"index":"热",
"index_d":"天气较热,建议着短裙、短裤、短套装、T恤等夏季服装。年老体弱者宜着长袖衬衫和单裤。",
<!-- 48小时穿衣指数 -->
"index48":"暖","index48_d":"较凉爽,建议着长袖衬衫加单裤等春秋过渡装。年老体弱者宜着针织长袖衬衫、马甲和长裤。",
<!-- 紫外线及48小时紫外线 -->
"index_uv":"弱","index48_uv":"最弱",
<!-- 洗车 -->
"index_xc":"不宜",
<!-- 风速级别描述 -->
"fl1":"小于3级","fl2":"小于3级","fl3":"小于3级","fl4":"小于3级","fl5":"小于3级","fl6":"小于3级",
<!-- 今天穿衣指数 -->
"index":"热",
"index_d":"天气较热,建议着短裙、短裤、短套装、T恤等夏季服装。年老体弱者宜着长袖衬衫和单裤。",
<!-- 48小时穿衣指数 -->
"index48":"暖","index48_d":"较凉爽,建议着长袖衬衫加单裤等春秋过渡装。年老体弱者宜着针织长袖衬衫、马甲和长裤。",
<!-- 紫外线及48小时紫外线 -->
"index_uv":"弱","index48_uv":"最弱",
<!-- 洗车 -->
"index_xc":"不宜",
<!-- 旅游 -->
"index_tr":"适宜",、
<!-- 舒适指数 -->
"index_co":"较不舒适",
"st1":"27","st2":"21","st3":"24","st4":"18","st5":"22","st6":"18",
<!-- 晨练 -->
"index_cl":"较不宜",
<!-- 晾晒 -->
"index_ls":"不太适宜",
<!-- 过敏 -->
"index_ag":"不易发"
}
}
"index_tr":"适宜",、
<!-- 舒适指数 -->
"index_co":"较不舒适",
"st1":"27","st2":"21","st3":"24","st4":"18","st5":"22","st6":"18",
<!-- 晨练 -->
"index_cl":"较不宜",
<!-- 晾晒 -->
"index_ls":"不太适宜",
<!-- 过敏 -->
"index_ag":"不易发"
}
}
城市代码数组:可以单独放到一个文件里。 city_code.php
$CSArr = array(
"101010100"=>"北京",
"101020100"=>"上海",
"101030100"=>"天津",
"101040100"=>"重庆",
"101320101"=>"香港",
"101330101"=>"澳门",
"101050101"=>"哈尔滨",
"101050201"=>"齐齐哈尔",
"101050301"=>"牡丹江",
"101050901"=>"大庆",
"101050801"=>"伊春",
"101051301"=>"双鸭山",
"101051201"=>"鹤岗",
"101051101"=>"鸡西",
"101050401"=>"佳木斯",
"101051002"=>"七台河",
"101050601"=>"黑河",
"101050501"=>"绥化",
"101050701"=>"大兴安岭",
"101060101"=>"长春",
"101060301"=>"延吉",
"101060201"=>"吉林",
"101060901"=>"白山",
"101060601"=>"白城",
"101060401"=>"四平",
"101060801"=>"松原",
"101060701"=>"辽源",
"101060603"=>"大安",
"101060501"=>"通化",
"101070101"=>"沈阳",
"101070201"=>"大连",
"101071401"=>"葫芦岛",
"101071301"=>"盘锦",
"101070501"=>"本溪",
"101070401"=>"抚顺",
"101071101"=>"铁岭",
"101071001"=>"辽阳",
"101070801"=>"营口",
"101070901"=>"阜新",
"101071201"=>"朝阳",
"101070701"=>"锦州",
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论