关注后没有回复呢?

来源:-

yyii

2017-04-19 00:40

照老师讲的写的,

代码在这里全部能看到。https://github.com/phpervip/tp5_demo/blob/master/application/index/controller/Wechat.php

// http://tp5.yyii.info/index/wechat/index
class Wechat extends Controller
{
   public function index(){

       //获得参数 signature nonce token timestamp echostr
       $nonce     = $_GET['nonce'];
       $token     = 'weixin';
       $timestamp = $_GET['timestamp'];
       $echostr   = $_GET['echostr'];
       $signature = $_GET['signature'];
       //形成数组,然后按字典序排序
       $array = array();
       $array = array($nonce, $timestamp, $token);
       sort($array);
       //拼接成字符串,sha1加密 ,然后与signature进行校验
       $str = sha1( implode( $array ) );
       if( $str  == $signature && $echostr ){
           //第一次接入weixin api接口的时候
           echo  $echostr;
           exit;
       }else{
           $this->reponseMsg();
       }
   }

// http://tp5.yyii.info/index/wechat/show
   public function show(){
       echo 'this is show';
   }

   // 接收事件推送并回复
   public function reponseMsg(){
       //1.获取到微信推送过来post数据(xml格式)
       $postArr = $GLOBALS['HTTP_RAW_POST_DATA'];
       //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';
               $content  = '欢迎关注我们的微信公众账号'.$postObj->FromUserName.'-'.$postObj->ToUserName;
               $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;

           }
       }


写回答 关注

1回答

  • 十二里河
    2017-04-22 16:10:22

    我也是这个问题

PHP实现微信公众平台开发—提升篇

本课程详细讲解了接收微信用户发送的消息和接收订阅关注的事件推送

64947 学习 · 371 问题

查看课程

相似问题