PHP 微信开发 利用百度接口实现天气预报功能开发

1、接口使用的是百度的天气预报接口,接口说明:

http://developer.baidu.com/map/carapi-7.htm

2、请求url:

http://api.map.baidu.com/telematics/v3/weather?location=%E6%BB%A8%E5%B7%9E&output=json&ak=*********************************

 

location:城市名称

output:输出类型,此处使用 JSON

ak:开发这密钥,申请很简单:http://lbsyun.baidu.com/apiconsole/key

 

3、返回格式为JSON格式如下:

{
    "erro r":0,
    "status":"success",
    "date":"2014-06-10",
    "results":[
        {
            "currentCity":"\u6ee8\u5dde",
            "weather_data":[
                {
                    "date":"\u5468\u4e8c 06\u670810\u65e5 (\u5b9e\u65f6\uff1a26\u2103)",
                    "dayPictureUrl":"http:\/\/api.map.baidu.com\/images\/weather\/day\/leizhenyu.png",
                    "nightPictureUrl":"http:\/\/api.map.baidu.com\/images\/weather\/night\/leizhenyu.png",
                    "weather":"\u96f7\u9635\u96e8",
                    "wind":"\u4e1c\u5317\u98ce4-5\u7ea7",
                    "temperature":"17\u2103"
                },
                {
                    "date":"\u5468\u4e09",
                    "dayPictureUrl":"http:\/\/api.map.baidu.com\/images\/weather\/day\/zhenyu.png",
                    "nightPictureUrl":"http:\/\/api.map.baidu.com\/images\/weather\/night\/duoyun.png",
                    "weather":"\u9635\u96e8\u8f6c\u591a\u4e91",
                    "wind":"\u4e1c\u98ce4-5\u7ea7",
                    "temperature":"26 ~ 17\u2103"
                },
                {
                    "date":"\u5468\u56db",
                    "dayPictureUrl":"http:\/\/api.map.baidu.com\/images\/weather\/day\/qing.png",
                    "nightPictureUrl":"http:\/\/api.map.baidu.com\/images\/weather\/night\/qing.png",
                    "weather":"\u6674",
                    "wind":"\u5fae\u98ce",
                    "temperature":"30 ~ 18\u2103"
                },
                {
                    "date":"\u5468\u4e94",
                    "dayPictureUrl":"http:\/\/api.map.baidu.com\/images\/weather\/day\/duoyun.png",
                    "nightPictureUrl":"http:\/\/api.map.baidu.com\/images\/weather\/night\/duoyun.png",
                    "weather":"\u591a\u4e91",
                    "wind":"\u5fae\u98ce",
                    "temperature":"31 ~ 20\u2103"
                }
            ]
        }
    ]
}

4、实现代码:

switch($requestMsg->MsgType){
	case 'text':
		//是否有中文,如果有中文把中文作为城市名称
		$flag = preg_matc h('/[\x{4e00}-\x{9fa5}]+/u', $requestMsg->Content, $matchs);
		if($flag){
			$w = new Weather();
			$content = $w->getWeather($matchs[0]);

			if($content != null){
				$responseMsg['Content'] = $content;
			}else{
				$responseMsg['Content'] = '对不起,未找到该城市.发送格式 1.上海';
			}
		}else{
			$responseMsg['Content'] = '对不起,未找到该城市.发送格式 1.上海';
		}

		break;
}
class Weather{

	public function getWeather($name){
			$url = "http://api.map.baidu.com/telematics/v3/weather?location=$name&output=json&ak=A33216daeb5fee2f4bebb59b97a6aed8";
			$arr = json_decode(file_get_contents($url), true);

			$weather = null;
			if($arr['error'] === 0){
				$weather = "[天气预报] \n" . 
					$name . '  ' . $arr['date'] . " \n" . 
					$arr['results'][0]['weather_data'][0]['weather'] . "\n" .
					$arr['results'][0]['weather_data'][0]['wind'] . "\n" .
					$arr['results'][0]['weather_data'][0]['temperature'];
			}

			return $weather;
	}

}

 

发表评论