微信公众号 封装类的代码实现Token验证\消息发送,位置定位等
首页引入类库
weixin.class.php
<?php class WeiXin{ private $postStr; //校验公众号是否连接成功 public function valid(){ $echoStr = $_GET["echostr"]; if($this->checkSignature()){ ob_clean(); echo $echoStr; }else{ LogUtil::writeLog($echoStr."没有和微信成功对接signature:".$_GET['signature'].",TIMESTAMP:".$_GET['timestamp'].",nonce:".$_GET['signature']); } } private function checkSignature(){ $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $token = TOCKEN; $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr,SORT_STRING); $tmpStr = implode($tmpArr); $tmpStr = sha1($tmpStr); if( $tmpStr == $signature){ return true; }else{ return false; } } //处理微信消息 public function responseMsg($postStr){ $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; if (!empty($postStr)){ $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; //内容 $Content = trim($postObj->Content); //消息类型 $MsgType = $postObj->MsgType; $time = time(); LogUtil::writeLog($MsgType); //开始进行逻辑判断 if ($MsgType == 'text') { //正常恢复文本消息 $reply = $this->makeText($fromUsername,$toUsername,$time,'欢迎你关注会购服务号'); echo $reply; }elseif ($MsgType == 'location') { //用户发送的是位置信息 $reply = $this->makeText($fromUsername,$toUsername,$time,'欢迎你关注会购服务号,发送的 是地址信息'); echo $reply; }elseif ($MsgType == 'image') { //用户发送的是图片 $reply = $this->makeText($fromUsername,$toUsername,$time,'欢迎你关注会购服务号,发送的 是图片信息'); echo $reply; }elseif ($type=='voice') { //用户发送的是声音 $reply = $this->makeText($fromUsername,$toUsername,$time,'欢迎你关注会购服务号,,发送的 是声音信息'); echo $reply; }elseif($MsgType == "event"){ $event = $postObj->Event; //事件类型 LogUtil::writeLog($event); $EventKey = $postObj->EventKey; //发送的事件消息 switch ($event){ $keyArray = explode("_", $EventKey); //$keyArray[1] 就是二维码带的参数scene_id //首次关注事件 case ("subscribe"): $reply = $this->makeText($fromUsername,$toUsername,$time,'欢迎你关注会购商城'); echo $reply; break; //扫描已关注事件 case ("SCAN"): $reply = $this->makeText($fromUsername,$toUsername,$time,'欢迎你来到会购商城'); echo $reply; break; //取消关注 case ("unsubscribe"): LogUtil::writeLog("用户取消关注"); break; //获取地理位置 case ("LOCATION"): $array = array( 'openid' =>(String)$fromUsername, 'latitude' =>(String)$postObj->Latitude, //地理位置纬度 'longitude' =>(String)$postObj->Longitude,//地理位置经度 'precision' =>(String)$postObj->Precision//地理位置精度 ); break; case "CLICK": $EventKey=$postObj->EventKey; switch ($EventKey){ //活动内容 case ("Activity_content") : $record[0]=array( 'title' =>'观前街', 'description' =>'观前街位于江苏苏州市区,是成街于清朝时期的百年商业老街,街上老店名店云集,名声远播海内外...', 'picUrl' => 'https://mmbiz.qlogo.cn/mmbiz/Dr69C9SmRib2EUyHokQYe3hBleyQ2xI2QHM0stqjXvS0BNyeYHnXqNhrQxQSBq1dnhBYnU7ZGr3SeebobHmgP0Q/0?wx_fmt=png', 'url' =>'http://huigood.net/wxhuigou/index.php/Merch' ); $record[1]=array( 'title' =>'平江路', 'description' =>'平江路位于苏州古城东北,是一条傍河的小路,北接拙政园,南眺双塔,全长1606米,是苏州一条历史攸久的经典水巷。宋元时候苏州又名平江,以此名路...', 'picUrl' => 'https://mmbiz.qlogo.cn/mmbiz/Dr69C9SmRib2EUyHokQYe3hBleyQ2xI2QHM0stqjXvS0BNyeYHnXqNhrQxQSBq1dnhBYnU7ZGr3SeebobHmgP0Q/0?wx_fmt=png', 'url' =>'http://huigood.net/wxhuigou/index.php/Merch' ); $record[2]=array( 'title' =>'平江路', 'description' =>'平江路位于苏州古城东北,是一条傍河的小路,北接拙政园,南眺双塔,全长1606米,是苏州一条历史攸久的经典水巷。宋元时候苏州又名平江,以此名路...', 'picUrl' => 'https://mmbiz.qlogo.cn/mmbiz/Dr69C9SmRib2EUyHokQYe3hBleyQ2xI2QHM0stqjXvS0BNyeYHnXqNhrQxQSBq1dnhBYnU7ZGr3SeebobHmgP0Q/0?wx_fmt=png', 'url' =>'http://huigood.net/wxhuigou/index.php/Merch' ); $record[3]=array( 'title' =>'平江路', 'description' =>'平江路位于苏州古城东北,是一条傍河的小路,北接拙政园,南眺双塔,全长1606米,是苏州一条历史攸久的经典水巷。宋元时候苏州又名平江,以此名路...', 'picUrl' => 'https://mmbiz.qlogo.cn/mmbiz/Dr69C9SmRib2EUyHokQYe3hBleyQ2xI2QHM0stqjXvS0BNyeYHnXqNhrQxQSBq1dnhBYnU7ZGr3SeebobHmgP0Q/0?wx_fmt=png', 'url' =>'http://huigood.net/wxhuigou/index.php/Merch' ); $record[4]=array( 'title' =>'观前街', 'description' =>'观前街位于江苏苏州市区,是成街于清朝时期的百年商业老街,街上老店名店云集,名声远播海内外...', 'picUrl' => 'https://mmbiz.qlogo.cn/mmbiz/Dr69C9SmRib2EUyHokQYe3hBleyQ2xI2QHM0stqjXvS0BNyeYHnXqNhrQxQSBq1dnhBYnU7ZGr3SeebobHmgP0Q/0?wx_fmt=png', 'url' =>'http://huigood.net/wxhuigou/index.php/Merch' ); $record[5]=array( 'title' =>'观前街', 'description' =>'观前街位于江苏苏州市区,是成街于清朝时期的百年商业老街,街上老店名店云集,名声远播海内外...', 'picUrl' => 'https://mmbiz.qlogo.cn/mmbiz/Dr69C9SmRib2EUyHokQYe3hBleyQ2xI2QHM0stqjXvS0BNyeYHnXqNhrQxQSBq1dnhBYnU7ZGr3SeebobHmgP0Q/0?wx_fmt=png', 'url' =>'http://huigood.net/wxhuigou/index.php/Merch' ); $reply = $this->makeNews($time, $fromUsername, $toUsername,$record); echo $reply; break; //积分查询 case ("HuiGou_integral") : $record[0]=array( 'title' =>'观前街', 'description' =>'观前街位于江苏苏州市区,是成街于清朝时期的百年商业老街,街上老店名店云集,名声远播海内外...', 'picUrl' => 'http://huigood.net/wxhuigou/Public/img/0.png', 'url' =>'http://huigood.net/wxhuigou/index.php/Merch' ); $reply = $this->makeNews($time, $fromUsername, $toUsername,$record); echo $reply; break; //余额查询 case ("HuiGou_balance"): $reply = $this->makeText($fromUsername,$toUsername,$time,'你点击了 余额查询'); echo $reply; break; } break; } }else{ $reply = $this->makeText($fromUsername,$toUsername,$time,'不知道你干了什么,小编晕了'); echo $reply; break; } }else { echo ""; exit; } } //回复文本消息 private function makeText($fromUsername,$toUsername,$time,$text='') { $msgType = "text"; $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>{$time}</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> <FuncFlag>0</FuncFlag> </xml>"; return sprintf($textTpl,$fromUsername,$toUsername,$msgType,$text); } // 根据数组参数回复图文消息 private function makeNews($time,$fromUsername,$toUsername,$newsData = array()) { //头部 $newTplHeader = "<xml> <ToUserName><![CDATA[{$fromUsername}]]></ToUserName> <FromUserName><![CDATA[{$toUsername}]]></FromUserName> <CreateTime>{$time}</CreateTime> <MsgType><![CDATA[news]]></MsgType> <ArticleCount>%s</ArticleCount><Articles>"; //内容页面 $newTplItem = "<item> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> <PicUrl><![CDATA[%s]]></PicUrl> <Url><![CDATA[%s]]></Url> </item>"; //脚部 $newTplFoot = "</Articles> </xml>"; //开始组装 $Content = ''; $itemsCount = count($newsData); $itemsCount = $itemsCount < 10 ? $itemsCount : 10; // 微信公众平台图文回复的消息一次最多10条 if ($itemsCount) { foreach ( $newsData as $key => $item ) { if ($key <= 9) { $Content .= sprintf ( $newTplItem, $item ['title'], $item ['description'], $item ['picurl'], $item ['url'] ); } } } $header = sprintf($newTplHeader,$itemsCount); $footer = sprintf($newTplFoot); return $header . $Content . $footer; } }
日志记录类库 LogUtil.class.php
<?php /** * 日志 类 * @author Administrator */ class LogUtil{ public static function writeLog($text){ // 调用 error_log() 的另一种方式: error_log($text."\r\n", 3, "./my-errors.log"); } } ?>
接下来就可以写控制器了
define("TOCKEN", "huigood520"); //定义TOCKEN常量 与微信公众号配置的一致 class weixinModule extends MainBaseModule { //微信直连 public function weixinConnect(){ $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; $WeiXin = new \WeiXin(); $token = TOCKEN; //echo $token; $WeiXin->valid(); $reply = $WeiXin->responseMsg($postStr); } }