第一步:接收component_verify_ticket:
1、微信服务器每隔10分钟会向第三方的消息接收地址推送一次component_verify_ticket,拿到后需要在本地做好存储;
2、微信第三方平台的消息是加密的(下图),需要进行解密才能获取需要的信息;
3、接收并解密消息,代码如下:
- $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 ( OPEN_MSG_VERIFY_TOKEN, OPEN_ENCRYPT_KEY, OPEN_APPID );
- $postArr = ArrayUtil::xml2array ( $encryptMsg ); // xml对象解析
- $format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>";
- $from_xml = sprintf ( $format, $postArr ['Encrypt'] );
- // 第三方收到公众号平台发送的消息
- $msg = '';
- $errCode = $pc->decryptMsg ( $msg_sign, $timeStamp, $nonce, $from_xml, $msg ); // 解密
- if ($errCode == 0) {
- $param = ArrayUtil::xml2array ( $msg );
- switch ($param ['InfoType']) {
- case 'component_verify_ticket' : // 授权凭证
- $component_verify_ticket = $param ['ComponentVerifyTicket'];
- $ret ['component_verify_ticket'] = $component_verify_ticket;
- file_put_contents ( OPEN_COMPONENT_VERIFY_TICKET_PATH, $component_verify_ticke