【微信第三方平台】获取component_verify_ticket
作者: thtomatic 分类: php笔记 评论: [ 0 ] 条 浏览: [ 2525 ] 次
需要引入“wxBizMsgCrypt.php”文件 微信开放平台开发文档有下载 点击查看
在第三方平台创建审核通过之后后,微信服务器会每隔10分钟定时向“授权事件接收URL”推送component_verify_ticket,第三方平台方收到component_verify_ticket推送后需对其进行解密操作,且在接收到之后必须直接返回字符串success(推送过来的component_verify_ticket很重要,这在第三方平台后续功能实现上都需要用到)。
微信服务器将信息用XML数据格式,以POST方式推送到我们的服务器,官方有提供相应的数据示例以及字段说明。
微信官方提供的demo中 需要修改 不修改的话会报-40002错误 xml解析失败
报错原因是 官方返回的xml中没有“ToUserName”这个字段 而是返回了“AppId”这个字段
文末是我修改好的引入文件 直接下载引用就行
public function event()
{
$timeStamp = empty ($_GET ['timestamp']) ? "" : trim($_GET ['timestamp']);
$nonce = empty ($_GET ['nonce']) ? "" : trim($_GET ['nonce']);
$msg_sign = empty ($_GET['msg_signature']) ? "" : trim($_GET ['msg_signature']);
$encryptMsg = file_get_contents('php://input');
$this->wxBizMsgCrypt();
// 第三方收到公众号平台发送的消息
$msg = '';
$errCode = $this->wxBizMsgCrypt ->decryptMsg_ticket ($msg_sign, $timeStamp, $nonce, $encryptMsg, $msg );
if ($errCode == 0) {
file_put_contents(dirname(__DIR__) . '/include/text.php',$msg);
$data = $this->_xmlToArr ($msg);
if (isset ( $data['ComponentVerifyTicket'] )) {
F('ComponentVerifyTicket',$data['ComponentVerifyTicket']);
} elseif ($data ['InfoType'] =='unauthorized') {
}
echo 'success';
} else {
file_put_contents(dirname(__DIR__) . '/include/error.php',$encryptMsg);
echo '解密失败'.$errCode;
}
}
//xml转数组
protected function _xmlToArr($xml)
{
$res = @simplexml_load_string ( $xml,NULL, LIBXML_NOCDATA );
$res = json_decode ( json_encode ( $res), true );
return $res;
}
protected function wxBizMsgCrypt()
{
require_once(dirname(__DIR__) . '/include/wxBizMsgCrypt.php');
$this->wxBizMsgCrypt = new \WXBizMsgCrypt ($this->token, $this->encodingAesKey, $this->appId);
}
版权所有:《thtomatic》 => 《【微信第三方平台】获取component_verify_ticket》
本文地址:https://ask.mykeji.net/phpnotes/206.html
除非注明,文章均为 《简单记录》 原创,欢迎转载!转载请注明本文地址,谢谢。
发表评论: