帮我看一下代码哪儿错了 关注后没有回复消息

来源:3-2 接收事件订阅与回复响应消息(二)

萌萌哒小祝祝

2017-03-23 12:39


<?php
class wei{
   public function index(){
       $timestamp = $_GET['timestamp'];
       $token     = '15228990111';
       $nonce     = $_GET['nonce'];
       $signature = $_GET['signature'];
       $echostr   = $_GET['echostr'];
       $array=array($nonce,$timestamp,$token);
       sort($array);
       $str=implode('',$array);
       $str=sha1($str);
       if($str == $signature && $echostr){
           echo $echostr;
           exit;
       }else{
           $this->info();
       }
   }
   public function info(){
       //获得微信推送过来的 POST 数据
       $postArr = $GLOBALS['HTTP_RAW_POST_DATA'];
       //处理消息类型 XML格式转换,并设置回复类型和内容
       $postObj=simplexml_load_string( $postArr );
//        $postObj->ToUserName='';
//        $postObj->FromUserName='';
//        $postObj->CreateTime='';
//        $postObj->MsgType='';
//        $postObj->Event='';
       //判断该数据是否是订阅事件
       if(strtolower($postObj->MsgType)=='event'){
           //如果是关注 subscribe 事件
           if(strtolower($postObj->Event)=='subscribe'){
               //回复用户消息 纯文本格式
               $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;
           }
       }
   }
}

$wei=new wei();
$wei->index();

写回答 关注

2回答

  • 猫熊_
    2017-12-25 19:41:39

    你那有个小写 TO  to改成To

  • 喵先生_
    2017-03-23 14:14:41

    我是按照官方的文档里的demo写的,你最好还是用官方的试一试,改几个参数就行。然后在原有的基础上做其他的东西

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

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

64947 学习 · 371 问题

查看课程

相似问题