微信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 '配置文件失败';
 }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值