微信第三方平臺定時接收component_verify_ticket

背景:php

      獲取第三方平臺令牌(component_access_token),增長了component_verify_ticket參數。component_verify_ticket由公衆平臺每隔10分鐘,持續推送給第三方平臺方(在建立公衆號第三方平臺審覈經過後,纔會開始推送)。html

 

目標:java

      接收微信服務器推送的component_verify_ticketpython

 

文檔說明:(參見微信開放平臺文檔c++

      推送component_verify_ticket協議json

      在公衆號第三方平臺建立審覈經過後,微信服務器會向其「受權事件接收URL」每隔10分鐘定時推送component_verify_ticket。第三方平臺方在收到ticket推送後也需進行解密(詳細請見【消息加解密接入指引】),接收到後必須直接返回字符串success。c#

      因而在開放平臺管理中心中尋找「受權事件接收URL」,以下圖。服務器

      

 

截獲微信推送component_verify_ticket的POST請求地址:微信

      http://[受權事件接收URL]?encrypt_type=aes
          &timestamp=1438521627
          &nonce=33431792
          &msg_signature=xxxxxx
          &signature=xxxxxxxapp

請求內容格式:

       <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 

相關文章
相關標籤/搜索