微信公众平台获取天气

<?php

header('Content-type:text/html;charset=utf-8');
/**
  * 说明:微信智能处理
  * 作者:rlm0909@live.com
  * 时间:2013.11.07
  * 版本:V1.0.0.0
  */

//这里是你申请是写的 token
define("TOKEN", "rlm0909@live.com");
$wechatObj = new wechatCallbackapiTest();
//首次使用,请注释掉下面的一行代码。
//然后添加  
$wechatObj->valid(); //供首次验证需要
//验证成功后,即可以使用下面的代码
//$wechatObj->responseMsg();
class wechatCallbackapiTest { public function valid() { $echoStr = $_GET["echostr"]; //valid signature , option if($this->checkSignature()){ echo $echoStr; exit; } } public function responseMsg() { //get post data, May be due to the different environments $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //extract post data if (!empty($postStr)){ $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $time = time(); if($postObj->MsgType!='text'){ $contentStr = "欢迎加入我们 作者:rlm0909@live.com "; }else{ $keyword = trim($postObj->Content); } $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[%s]]></Content> <FuncFlag>0</FuncFlag> </xml>"; if (!isset($contentStr)) { $getweather = new getweather(); $contentStr=$getweather->getw($keyword); } $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $contentStr); echo $resultStr; }else { echo "请输入信息处理"; exit; } } private function checkSignature() { $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){ return true; }else{ return false; } } } class getweather{ //天气的对应编码,可以通过文件引入 private $arr=array( 101010100=>'北京', ); //天气对应编码的另一种形式 private $arrs=array( 101010100=>'beijing', ); public function getw($str){ $code=$this->check($str); if ($code){ $content = file_get_contents("http://m.weather.com.cn/data/{$code}.html"); if(!$content){ //mysql_query("insert into errorlog values(null,'没有取到天气接口数据','$date',$time)",$link); //echo mysql_error(); file_put_contents('weather.txt', '没有接受到天气数据', FILE_APPEND); return '暂时没有取到天气数据,请稍后再试'; } $result=json_decode($content,true); $info=$result['weatherinfo']; $strw="{$info['date_y']}{$info['week']},{$info['city']}的天气情况\n"; $strw .= "今天:({$info['temp1']}){$info['weather1']}{$info['wind1']}{$info['fl1']}。"; $strw .= "24小时穿衣指数:{$info['index_d']}\n"; $strw .= "明天:({$info['temp2']}){$info['weather2']}{$info['wind2']}{$info['fl2']}。"; $strw .= "48小时穿衣指数:{$info['index48_d']}"; return $strw; }else{ return '没有获取到该城市的天气,请确定输入了正确的城市名称,如\'北京\'或者\'beijing\''; } } /** * 验证接受到的数据是否合法 * @param string $str 传入的接受到的数据 * @return mixed|boolean 有数据返回$code,没有数据返回false; * */ private function check($str){ if (in_array($str, $this->arr)) { return array_search($str, $this->arr); }elseif (in_array($str, $this->arrs)){ return array_search($str, $this->arrs); }else { return false; } } }

转载于:https://www.cnblogs.com/rlm0909/p/3412452.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值