微信公众号开发 关注 取消关注 没有回调数据
发布于 6 年前 作者 wentao 3065 次浏览 来自 问答

微信公众号开发 在开发的基本配置中已经成功 “服务器配置(已启用)”,为什么点击关注微信公众号,和取消关注 在我们服务器接口中收不到任何数据,方法入口处的日志也没有任何数据。


在微信输入数据显示 “该公众号提供的服务器出现故障,请稍后再试”。

1 回复
public function index(){
    //获得参数 signature nonce token timestamp echostr
    $nonce     = $_GET['nonce'];
    $token     = 'laoweiread';
    $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接口的时候
        logs('Wechat','1',1);
        echo  $echostr;
        exit;
    }else{
        logs('Wechat','2',1);
        $this->reponseMsg();
    }
}
// 接收事件推送并回复
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;
        }
    }

就一个demo的代码,最基本的功能

回到顶部