微信公众号 服务器接口动态配置

微信公众号 服务器接口动态配置

此处实现接口回复消息(可接入机器人聊天等)

//微信公众号接口返回消息

    public function backinfo()
    {
        //1.获取到微信推送过来post数据(xml格式)
        //$postArr = $GLOBALS['HTTP_RAW_POST_DATA'];//php7以上不能用

        $postArr = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : file_get_contents("php://input");
        //$postArr = file_get_contents("php://input"); 将文件读入字符串
        //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';
                $EventKey = $postObj->EventKey;
                //前缀  qrscene_
                $EventKey = str_replace('qrscene_', '', $EventKey);
                //查询数据库是否有此用户
                $desdata = Db::table('tm_user')->order('yaoqinma', 'desc')->select();
                $decoun = $desdata[0]['yaoqinma'] + 1;
                $secount = Db::table('tm_user')->where('openid', $toUser)->count();
                if ($secount == 1) {

                    Db::table('tm_user')->where('openid', $toUser)->update(['beiyaoqinma' => $EventKey]);
                } else {
                    Db::table('tm_user')->insert(['beiyaoqinma' => $EventKey, 'yaoqinma' => $decoun, 'openid' => $toUser]);
                }
                $content = '您好,欢迎关注天美惠购微信公众账号' . $EventKey . '-' . "您的id" . $toUser;//$content  = '欢迎关注天美惠购微信公众账号';
                $template = "<xml><ToUserName><![CDATA[%s]]></ToUserName>
                            <FromUserName><![CDATA[%s]]></FromUserName>
                            <CreateTime>%s</CreateTime>
                            <MsgType><![CDATA[%s]]></MsgType>
                            <EventKey><![CDATA[%s]]></EventKey>
                            <Content><![CDATA[%s]]></Content></xml>";
                $info = sprintf($template, $toUser, $fromUser, $time, $msgType, $EventKey, $content);
                return $info;
            }
        }
        if (strtolower($postObj->MsgType) == 'text') {
            $toUser = $postObj->FromUserName;
            $fromUser = $postObj->ToUserName;
            $time = time();
            $msgType = 'text';
            $content = '您输入的内容是:' . $postObj->Content;//$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);
            return $info;
        }
    }

微信公众号开发设置里需要配置返回信息接口

上一篇:jedis异常:NoSuchElementException: Timeout waiting for idle object


下一篇:微信扫描带参数二维码事件