背景:
獲取第三方平台令牌(component_access_token),增加了component_verify_ticket參數。component_verify_ticket由公眾平台每隔10分鐘,持續推送給第三方平台方(在創建公眾號第三方平台審核通過後,才會開始推送)。
目標:
接收微信服務器推送的component_verify_ticket
文檔說明:(參見微信開放平台文檔)
推送component_verify_ticket協議
在公眾號第三方平台創建審核通過後,微信服務器會向其“授權事件接收URL”每隔10分鐘定時推送component_verify_ticket。第三方平台方在收到ticket推送後也需進行解密(詳細請見【消息加解密接入指引】),接收到後必須直接返回字符串success。
於是在開放平台管理中心中尋找“授權事件接收URL”,如下圖。

截獲微信推送component_verify_ticket的POST請求地址:
http://[授權事件接收URL]?encrypt_type=aes
×tamp=1438521627
&nonce=33431792
&msg_signature=xxxxxx
&signature=xxxxxxx
請求內容格式:
<xml>
<AppId><![CDATA[AppId]]></AppId>
<Encrypt><![CDATA[加密XML]]></Encrypt>
</xml>
知曉了微信推送xml的格式,接下來要做的就是解密xml,於是繼續閱讀文檔,(參見 微信開放平台 消息加解密指引)
關於解密,微信公眾平台提供了c++, php, java, python, c# 5種語言的示例代碼(點擊下載)
這裡基於php版本的示例代碼來實現PHP接收component_verify_ticket(基於CI框架)
1 /**
2 *“授權事件接收URL”每隔10分鐘接收component_verify_ticket
3 **/
4 public function ticket(){
5 require_once(DPL_LIBS.'wx/wxBizMsgCrypt.php');
6 $wx = new WXBizMsgCrypt($this->token, $this->encodingAesKey, $this->appId);
7
8 $inputs = (object)array(
9 'encrypt_type' => '',
10 'timestamp' => '',
11 'nonce' => '',
12 'msg_signature' => '',
13 'signature' => ''
14 );
15 foreach ($inputs as $key => &$value) {
16 $tmp = $this->input->get($key);
17 if (!empty($tmp)){
18 $value = $tmp;
19 }
20 }
21 $this->save_key_value('component_verify_ticket_get',json_encode($inputs));
22
23 $fp = fopen("php://input","r");
24 if (isset($fp) && !empty($fp)){
25 $this->post_xml = stream_get_contents($fp);
26 if (empty($this->post_xml)){
27 return;
28 }
29 }
30 $this->save_key_value('component_verify_ticket_post',$this->post_xml);
31
32 $this->xml = str_replace('AppId', 'ToUserName', $this->post_xml);
33
34 $msg_xml = '';
35 $errCode = $wx->decryptMsg($inputs->msg_signature, $inputs->timestamp, $inputs->nonce, $this->xml, $msg_xml);
36
37 $componentVerifyTicket = $this->parse_xml($msg_xml,'ComponentVerifyTicket');
38
39 $this->save_key_value('component_verify_ticket',$componentVerifyTicket);
40 if ($errCode == 0){
41 echo "success";
42 }else{
43 }
44 return;
45 }
分享請注明:
原文出自http://www.cnblogs.com/wenki/p/4700828.html