求大家帮助,关注还是没有回复消息呢

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

慕粉3809271

2016-12-09 18:56

<?php

// 本类由系统自动生成,仅供测试用途

class IndexAction extends Action {

    public function index(){

  $nonce=$_GET['nonce'];

  $token='wtl';

  $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)

{

header('content-type:text');

//第一次接入微信API接口时候验证合法性

echo $echostr;

exit;

}

     else

{

this->responseMsg();

}


}



//接收事件推送回复

      public function responseMsg()

{

//1.获取到微信推送过来post数据(xml格式)

$postArr=$GLOBALS['HTTP_RAW_POST_DATA'];

//2.处理消息类型,并设置回复类型和内容

$postObj=simplexml_load_string($postArr);

        //判断该数据包是否是订阅de事件推送

        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;

        }

        }


}

}


写回答 关注

1回答

  • Iverzhi
    2016-12-09 21:22:13
    已采纳

    终于找到了

    this->responseMsg();

    少了$

    慕粉3809...

    非常感谢!非常感谢!

    2016-12-19 10:36:23

    共 1 条回复 >

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

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

64947 学习 · 371 问题

查看课程

相似问题