配置微信公眾平臺URL關注時自動回覆
阿新 • • 發佈:2018-12-10
//1、配置微信公眾平臺URL
//配置微信平臺URL public function weixinURL(){ //將timestamp,nonce,token按字典排序 $timestamp =$_GET['timestamp']; $nonce =$_GET['nonce']; $echostr =$_GET['echostr']; $token ='weixin'; $signature =$_GET['signature']; $array =array($timestamp,$nonce,$token); sort($array); //將排序後的三個引數拼接之後用sha1加密 $tmpstr=sha1(implode($array)); //將加密後的字串與signature進行對比,判斷該請求是否來自微信 if($tmpstr==$signature && $echostr){ echo $echostr; exit(); } else{ $this->reponseMsg(); } } //關注時回覆 public function reponseMsg(){ //查詢微信推送訊息內容 $rootM=D('wechatconfig'); $where['id']=1; $res=$rootM->where($where)->find(); //1.獲取微信推送過來的post資料(xml格式) $postArr=file_get_contents("php://input"); //2.處理訊息型別,並設定回覆型別和內容 $postObj=simplexml_load_string($postArr); //判斷該資料包是否是訂閱的事件推送 //關注時回覆 if(strtolower($postObj->MsgType=='event')){ //如果是關注subscribe事件 if(strtolower($postObj->Event=='subscribe')){ //回覆使用者訊息 $toUser=$postObj->FromUserName; $fromUser=$postObj->ToUserName; $time=time(); $msgType='text'; $answerM=D('answer'); $whereM['id']=1; $resultM=$answerM->where($whereM)->find(); $content=array( array( 'title'=>$resultM['answer_name'], 'description'=>$resultM['answer_charact'], 'picUrl'=>$res['domain_name'].'/'.$resultM['answer_img'], 'url'=>$resultM['answer_url'], ), ); $template="<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <ArticleCount>1</ArticleCount> <Articles>"; foreach ($content as $k=>$v) { $template.="<item> <Title><![CDATA[".$v['title']."]]></Title> <Description><![CDATA[".$v['description']."]]></Description> <PicUrl><![CDATA[".$v['picUrl']."]]></PicUrl> <Url><![CDATA[".$v['url']."]]></Url> </item>"; } $template.="</Articles> </xml>"; echo sprintf($template,$toUser,$fromUser,$time,'news'); } } //點選回覆圖文 if(strtolower($postObj->Event)=='click'){ if($postObj->EventKey==$postObj->EventKey){ $titleaa=$postObj->EventKey; $whereaa['t_explain']=strtolower($titleaa); $imgtextM=D('imgtext'); $result=$imgtextM->where($whereaa)->find(); $fromUser=$postObj->ToUserName; $toUser=$postObj->FromUserName; $time=time(); if($result['t_explain']==$postObj->EventKey){ if($result['t_url']==""){ $arr=array( array( 'title'=>$result['t_title'], 'description'=>$result['t_synopsis'], 'picUrl'=>$res['domain_name'].'/'.$result['t_img'], 'url'=>$res['domain_name'].'/index.php/index/imgtext/id/'.$result['id'], ), ); }else{ $arr=array( array( 'title'=>$result['t_title'], 'description'=>$result['t_synopsis'], 'picUrl'=>$res['domain_name'].'/'.$result['t_img'], 'url'=>$result['t_url'], ), ); } $template="<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <ArticleCount>1</ArticleCount> <Articles>"; foreach ($arr as $k=>$v) { $template.="<item> <Title><![CDATA[".$v['title']."]]></Title> <Description><![CDATA[".$v['description']."]]></Description> <PicUrl><![CDATA[".$v['picUrl']."]]></PicUrl> <Url><![CDATA[".$v['url']."]]></Url> </item>"; } $template.="</Articles> </xml>"; echo sprintf($template,$toUser,$fromUser,$time,'news'); }else{ $toUser=$postObj->FromUserName; $fromUser=$postObj->ToUserName; $time=time(); $msgType='text'; $content='沒有設定回覆'; $template=" <xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> </xml> "; $info=sprintf($template,$toUser,$fromUser,$time,$msgType,$content); echo $info; } } } }