PHP微信公众号关键词自动回复

<?php
// 接收参数
define("TOKEN", "123456");

$wechatObj = new wx();
if (isset($_GET['echostr'])) {
    $wechatObj->valid();
} else {
    $wechatObj->responseMsg();
}

class wx
{

    /**
     * 微信公众号验证
     */
    public function valid()
    {
        $echoStr = $_GET["echostr"];
        if ($this->checkSignature()) {
            echo $echoStr;
            exit;
        }
    }

    public function responseMsg()
    {
        //$postArr = $GLOBALS['HTTP_RAW_POST_DATA'];
        $postArr = file_get_contents('php://input');
        //2.处理消息类型,并设置回复类型和内容
        $postObj = simplexml_load_string($postArr);
        //判断该数据包是否是订阅的事件推送
        if (strtolower($postObj->MsgType) == 'event') {
            //如果是关注 subscribe 事件
            // 超链接里边指定data-miniprogram-path跳转小程序某个页面
            if (strtolower($postObj->Event == 'subscribe')) {
                //回复用户消息(纯文本格式)
                $fromUsername = $postObj->FromUserName; //请求消息的用户
                $toUsername   = $postObj->ToUserName; //"我"的公众号id
                $time         = time(); //时间戳
                $msgtype      = 'text'; //消息类型:文本
                $textTpl      = "<xml>
            <ToUserName><![CDATA[%s]]></ToUserName>
            <FromUserName><![CDATA[%s]]></FromUserName>
            <CreateTime>%s</CreateTime>
            <MsgType><![CDATA[%s]]></MsgType>
            <Content><![CDATA[%s]]></Content>
            </xml>";

                if (strtolower($postObj->MsgType == 'event')) { //如果XML信息里消息类型为event
                    if ($postObj->Event == 'subscribe') { //如果是订阅事件
                        $followMeText = "关注后自动回复的语句";
                        if (empty($followMeText)) {
                            $followMeText = "嘻嘻,最近小编花了好大时间做了这个自动回复功能!现在可以直接回复关键词就ok拉!";
                        }
                        $contentStrq = $content = $followMeText;
                        $resultStrq  = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgtype, $contentStrq);
                        echo $resultStrq;
                        exit();//一定要加!!!!不加程序运转正常就是不回复消息
                    }
                }
            }
        }
        //判断是否回复关键词
        if (strtolower($postObj->MsgType) == 'text') {
            $keyword = trim($postObj->Content); //消息内容
            if ( ! empty($keyword)) {
                $resContent = keyword;
                $res = $this->transmitText($postObj, $resContent);
                echo $res;
                exit();//一定要加!!!!不加程序运转正常就是不回复消息
            } else {
                echo "";
                exit();
            }
        }
    }

    private function transmitText($object, $content)
    {
        $xmlTpl = '<xml>
            <ToUserName><![CDATA[%s]]></ToUserName>
            <FromUserName><![CDATA[%s]]></FromUserName>
            <CreateTime>%s</CreateTime>
            <MsgType><![CDATA[text]]></MsgType>
            <Content><![CDATA[%s]]></Content>
            </xml>';
        $res    = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time(), $content);

        return $res;
    }

    private function checkSignature()
    {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce     = $_GET["nonce"];

        $token  = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr);
        $tmpStr = implode($tmpArr);
        $tmpStr = sha1($tmpStr);

        if ($tmpStr == $signature) {
            return true;
        } else {
            return false;
        }
    }
}
PHP微信公众号关键词自动回复
© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享
评论 抢沙发

请登录后发表评论

    blank

    暂无评论内容