微信api + 百度api 开发天气预报

<?php
header('Content-type: text/html; charset=utf-8');#设置头信息
require_once('zhphpWeixinApi.class.php');#加载微信接口类文件
$zhwx=new zhphpWeixinApi();//实例化
$configArr=array(
				'token'=>'wxtest1',
				'appid'=>'wx5b79a26868798826',
				'appSecret'=>'a95952819cc768fc130bc0cc0444ae77',
				'myweixinId'=>'gh_746ed5c6a58b'
				);
$zhwx->setConfig($configArr);//配置文件
if($zhwx->weixinBaseApiMessage()){
    $keyword=$zhwx->getUserTextRequest();//得到用户发的微信内容
    $msgtype=$zhwx->getUserMsgType();//得到用户发的微信数据类型
    $SendTime=$zhwx->getUserSendTime();//得到用户发送的时间
      //如果 keyword 为非空的情况下
    if( ! empty($keyword)){
		 $location=urlencode($keyword);//将中文转化
		 //调用百度 api 接口,ak 是成为百度开发者后的key
			$url='http://api.map.baidu.com/telematics/v3/weather?location='.$location.
			'&output=json&ak=C845576f91f52f4e62bf8d9153644cb8';
			$jsonData=$zhwx->curl_post_https($url);
			$data=json_decode($jsonData,true);
			$weth=$data['results'][0];
			$content='你查询的是:'.$weth['currentCity'].'的天气情况'."\n";
            $content.='pm2.5='.$weth['pm25']."\n";
            $content.='今天的天气是:'.$weth['weather_data'][0]['date']."\n";
	       $zhwx->responseMessage('text',$content);
    }
 }else{
		   echo '配置文件失败';
 }

发布了186 篇原创文章 · 获赞 24 · 访问量 31万+

猜你喜欢

转载自blog.csdn.net/echocdzh/article/details/50699762