PHP微信公众号服务器配置

PHP微信公众号服务器配置

配置微信公众号

启用安全模式,详细文档可根据微信文档,这里提供了验证代码和接收信息怎么处理
在这里插入图片描述

代码

请注意,完全验证之后,输出echostr参数内容,记住请不要输出其他值,否则验证失败,还是不喜勿喷哈

<?php

class Test
{
    private static $appEncodingAESKey = '';//消息加解密密钥(EncodingAESKey)
    private static $appToken = '';//令牌(Token)
    private static $appkey = '';//开发者ID(AppID)

    public function index()
    {
        try {
            /************ 公众号服务器验证 ************/
            $encodingAesKey = self::$appEncodingAESKey;
            $token = self::$appToken;
            $appId = self::$appkey;
            if (isset($_GET['echostr'])) {
                $echoStr = $_GET['echostr'];
                if (self::checkSignature($token)) {
                    //完全验证之后,输出echostr参数内容,记住请不要输出其他值,否则验证失败
                    echo $echoStr;
                    die;
                }
            }
            /************ 以下接收公众号用户输入信息 ************/
            //require_once引入微信官方PHP示例代码,下载地址:https://res.wx.qq.com/op_res/-serEQ6xSDVIjfoOHcX78T1JAYX-pM_fghzfiNYoD8uHVd3fOeC0PC_pvlg4-kmP
            require_once dirname(dirname(dirname(__DIR__))) . '/extend/wxserverdecryption/wxBizMsgCrypt.php';
            // 第三方发送消息给公众平台
            $timeStamp = (isset($_GET['timestamp'])) ? $_GET['timestamp'] : '';
            $nonce = (isset($_GET['nonce'])) ? $_GET['nonce'] : '';
            $pc = new \wxBizMsgCrypt($token, $encodingAesKey, $appId);
            // 第三方收到公众号平台发送的消息
            $msg = '';
            $msg_sign = (isset($_GET['msg_signature'])) ? $_GET['msg_signature'] : '';
            $from_xml = file_get_contents('php://input');
            $errCode = $pc->decryptMsg($msg_sign, $timeStamp, $nonce, $from_xml, $msg);
            if ($errCode == 0) {
                $json = json_encode(simplexml_load_string($msg, 'SimpleXMLElement', LIBXML_NOCDATA));
                $json = json_decode($json, true);
                if (isset($json['MsgType']) && !empty($json['MsgType'])) {
                    switch ($json['MsgType']) {
                        case 'text'://接收到文本
                            break;
                        case 'image'://接收到图片
                            break;
                        case 'voice'://接收到语音
                            break;
                        case 'video'://接收到视频
                            break;
                        case 'music'://接收到音乐
                            break;
                        case 'news'://接收到图文
                            break;
                        case 'event'://接收到事件
                            if (isset($json['Event']) && !empty($json['Event'])) {
                                switch ($json['Event']) {
                                    case 'SCAN'://用户已关注公众号
                                        echo self::reply('text', $json, '你已关注');
                                        break;
                                    case 'subscribe'://用户关注公众号
                                        break;
                                    case 'unsubscribe'://用户取消关注公众号
                                        break;
                                    case 'CLICK'://自定义菜单事件
                                        break;
                                    case 'VIEW'://点击菜单跳转链接时的事件推送
                                        break;
                                    case 'LOCATION'://接收到位置
                                        $data['latitude'] = $json['Latitude'];//纬度
                                        $data['longitude'] = $json['Longitude'];//经度
                                        $data['precision'] = $json['Precision'];//精确度
                                        break;
                                    default:
                                        break;
                                }
                            }
                            break;
                        default:
                            break;
                    }
                }
            } else {
                file_put_contents('./err.log', $errCode . PHP_EOL, 8);
            }
        } catch (\Exception $e) {
            file_put_contents('./err.log', $e->getMessage() . PHP_EOL, 8);
        }
    }

    private static function reply($msgType, $json, $contentStr = '')
    {
        //FromUserName来自谁
        //ToUserName发给谁
        $time = $_SERVER['REQUEST_TIME'];
        $resultStr = '';
        if ($msgType == 'text') {
            $textTpl = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[%s]]></MsgType><Content><![CDATA[%s]]></Content><FuncFlag>0</FuncFlag></xml>";
            $resultStr = sprintf($textTpl, $json['FromUserName'], $json['ToUserName'], $time, $msgType, $contentStr);
        } elseif ($msgType == 'image') {
            $textTpl = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[%s]]></MsgType><Image><MediaId><![CDATA[%s]]></MediaId></Image><FuncFlag>0</FuncFlag></xml>";
            $MediaId = '';
            $resultStr = sprintf($textTpl, $json['FromUserName'], $json['ToUserName'], $time, $msgType, $MediaId);
        } elseif ($msgType == 'voice') {
            $textTpl = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[%s]]></MsgType><Voice><MediaId><![CDATA[%s]]></MediaId></Voice><FuncFlag>0</FuncFlag></xml>";
            $MediaId = '';
            $resultStr = sprintf($textTpl, $json['FromUserName'], $json['ToUserName'], $time, $msgType, $MediaId);
        } elseif ($msgType == 'video') {
            $textTpl = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[%s]]></MsgType><Video><MediaId><![CDATA[%s]]></MediaId><Title><![CDATA[%s]]></Title><Description><![CDATA[%s]]></Description></Video><FuncFlag>0</FuncFlag></xml>";
            $MediaId = '';
            $Title = '';
            $Description = '';
            $resultStr = sprintf($textTpl, $json['FromUserName'], $json['ToUserName'], $time, $msgType, $MediaId, $Title, $Description);
        } elseif ($msgType == 'music') {
            $textTpl = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[%s]]></MsgType><Music><Title><![CDATA[%s]]></Title><Description><![CDATA[%s]]></Description><MusicUrl><![CDATA[%s]]></MusicUrl><HQMusicUrl><![CDATA[%s]]></HQMusicUrl><ThumbMediaId><![CDATA[%s]]></ThumbMediaId></Music><FuncFlag>0</FuncFlag></xml>";
            $Title = '';
            $Description = '';
            $MusicUrl = '';
            $HQMusicUrl = '';
            $ThumbMediaId = '';
            $resultStr = sprintf($textTpl, $json['FromUserName'], $json['ToUserName'], $time, $msgType, $Title, $Description, $MusicUrl, $HQMusicUrl, $ThumbMediaId);
        } elseif ($msgType == 'news') {
            $textTpl = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[%s]]></MsgType><ArticleCount>%s</ArticleCount><Articles><item><Title><![CDATA[%s]]></Title><Description><![CDATA[%s]]></Description><PicUrl><![CDATA[%s]]></PicUrl><Url><![CDATA[%s]]></Url></item></Articles><FuncFlag>0</FuncFlag></xml>";
            $ArticleCount = '';
            $Title = '';
            $Description = '';
            $PicUrl = '';
            $Url = '';
            $resultStr = sprintf($textTpl, $json['FromUserName'], $json['ToUserName'], $time, $msgType, $ArticleCount, $Title, $Description, $PicUrl, $Url);
        }
        return $resultStr;
    }

    //验证消息加解密
    private static function checkSignature($token)
    {
        $signature = $_GET['signature'];
        $timestamp = $_GET['timestamp'];
        $nonce = $_GET['nonce'];
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr);
        $tmpStr = implode($tmpArr);
        $tmpStr = sha1($tmpStr);
        if ($tmpStr == $signature) {
            return true;
        } else {
            return false;
        }
    }
}
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你有多高, 六尺七寸

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值