微信服务器将信息用XML数据格式,以POST办法推送到我们的做事器,官方有供应相应的数据示例以及字段解释。
POST数据示例
<xml>

<AppId></AppId>
<CreateTime>1413192605</CreateTime>
<InfoType></InfoType>
<ComponentVerifyTicket></ComponentVerifyTicket>
</xml>
字段解释
这一步也是折腾我最久的地方,紧张问题便是推送过来的信息是加密的我们须要先将信息解密,解密出component_verify_ticket后将该ticket保存起来。
不过这部分官方有供应相应措辞的解密DEMO,下面附上我自己这块的完全代码,里面用到了数据库操作以及微信公众号取消授权的操作:
public function ticket(){
require_once(dirname(FILE).'/wxBizMsgCrypt.php');//该文件在官方demo里面,下载后引入进来就可以了
$encodingAesKey = '';//创建平台时填写的"大众年夜众号加解密Key
$token = '';//创建平台时填写的公众年夜众号校验Token
$appId = '';//"大众年夜众号第三方平台AppID
$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' );
$pc = new \WXBizMsgCrypt ( $token,$encodingAesKey, $appId );
// 第三方收到"大众号平台发送的
$msg = '';
$errCode = $pc->decryptMsg ($msg_sign, $timeStamp, $nonce, $encryptMsg, $msg );
if ($errCode == 0) {
$data = $this->_xmlToArr ( $msg);
if (isset ( $data['ComponentVerifyTicket'] )) {
$config['componentverifyticket'] = $data ['ComponentVerifyTicket'];
$config['create_time'] =date(\"大众Y-m-d H:i:s\"大众);
$where['id']= '1';
M('Public')->where($where)->setField($config);
} elseif ($data ['InfoType'] =='unauthorized') {
// 在公众年夜众号后台取消授权后,同步把系统里的"大众号删除掉,并更新干系用户缓存
$map ['appid'] = $data['AuthorizerAppid'];
$map2 ['id'] = M ('WechatPublic' )->where ( $map )->getField ( 'id' );
if ($map2 ['id']) {
M ( 'WechatPublic')->where ( $map2 )->delete();
}
}
echo 'success';
} else {
echo '解密失落败'.$errCode;
}
}
public function _xmlToArr($xml) {
$res = @simplexml_load_string ( $xml,NULL, LIBXML_NOCDATA );
$res = json_decode ( json_encode ( $res), true );
return $res;
}
后面吸收微信服务推送的都须要解密,该方法都可以解密。