配置微信公众号
将启用安全模式
,详细文档可根据微信文档,这里提供了验证代码和接收信息怎么处理
代码
请注意,完全验证之后,输出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;
}
}
}