微信公众号token认证、关注/取消关注事件

本文深入解析微信公众号接口的实现机制,包括消息事件处理、文本消息响应及事件订阅流程。通过实例化wechatCallback类,展示了如何验证token、响应消息及处理不同类型的用户事件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

define("TOKEN", "youtoken");//token内容识别码
$wechatObj = new wechatCallback();//实例化wechatCallback类


if (!isset($_GET["echostr"])) {
    // 消息事件
    $wechatObj->responseMsg();
} else {
    file_put_contents('/var/www/api/logs/debug.log', 'req $_GET:' . print_r($_GET, true) . "\n", FILE_APPEND);
    // 认证token
    $wechatObj->valid();
}

class wechatCallback
{
    public function valid()
    {
        $echoStr = $_GET["echostr"];
        if ($this->checkSignature()) {
            echo $echoStr;
            exit;
        }
    }

    public function responseMsg()//执行接收器方法
    {
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
        if (!empty($postStr)) {
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
            file_put_contents('/var/www/api/logs/debug.log', 'req $postObj =:' . print_r($postObj, true) . "\n", FILE_APPEND);

            $RX_TYPE = trim($postObj->MsgType);
            switch ($RX_TYPE) {
                case "event":
                    $result = $this->receiveEvent($postObj);
                    break;
                case "text":
                    // 用户向公众号发送文本消息
                    $content = '我回复你了';
                    $result = $this->transmitText($postObj, $content);
                    break;
            }
            echo $result;
            exit();
        } else {
            echo "";
            exit;
        }
    }

    private function receiveEvent($postObj)
    {
        $content = "";
        switch ($postObj->Event) {
            case "subscribe":
                // 用户关注公众号
                $content = "欢迎关注暖心有你公众号";
                break;
            case "unsubscribe":
                // 用户取消公众号关注
                $content = "";
                break;
        }
        $result = $this->transmitText($postObj, $content);
//        return $result;
    }

    private function transmitText($object, $content)
    {
        $textTpl = "<xml> 
       <ToUserName><![CDATA[%s]]></ToUserName> 
       <FromUserName><![CDATA[%s]]></FromUserName> 
       <CreateTime>%s</CreateTime> 
       <MsgType><![CDATA[text]]></MsgType> 
       <Content><![CDATA[%s]]></Content> 
       <FuncFlag>0</FuncFlag> 
       </xml>";
        $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content);
        return $result;
    }

    private function checkSignature()
    {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];
        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode($tmpArr);
        $tmpStr = sha1($tmpStr);
        if ($tmpStr == $signature) {
            return true;
        } else {
            return false;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值