代码一样的,也用了get_file_content就是关注没消息

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

慕圣5284035

2019-02-20 23:36

<?php
namespace app\index\controller;

class Index
{
    public function index()
    {
        if (isset($_GET['echostr'])) {
            //获得参数 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->responseMsg();
        }
    }

    public function responseMsg(){
        //1.获取微信推送过来的post数据(xml格式)
        $postArr = file_get_contents("php://input");
        libxml_disable_entity_loader(true);
        $postObj = simplexml_load_string($postArr, 'SimpleXMLElement', LIBXML_NOCDATA);
//        print_r($postObj);
        if(strtolower($postObj->MsgType) == 'event'){
            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;
            }
        }

    }
    
    本人用的tp5框架


写回答 关注

1回答

  • 张小西西
    2020-01-02 13:36:39

    请问你的出来了么?我也遇到同样的问题了

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

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

64945 学习 · 262 问题

查看课程

相似问题