背景:
获取第三方平台令牌(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的格式,接下来要做的就是解密xml,于是继续阅读文档,(参见 微信开放平台 消息加解密指引)
关于解密,微信公众平台提供了c++, php, java, python, c# 5种语言的示例代码(点击下载)
这里基于php版本的示例代码来实现PHP接收component_verify_ticket(基于CI框架)
<span> 1</span> <span>/*</span><span>* </span><span> 2</span> <span> *“授权事件接收URL”每隔10分钟接收component_verify_ticket </span><span> 3</span> <span> *</span><span>*/</span> <span> 4</span> <span>public</span> <span>function</span><span> ticket(){ </span><span> 5</span> <span>require_once</span>(DPL_LIBS.'wx/wxBizMsgCrypt.php'<span>); </span><span> 6</span> <span>$wx</span> = <span>new</span> WXBizMsgCrypt(<span>$this</span>->token, <span>$this</span>->encodingAesKey, <span>$this</span>-><span>appId); </span><span> 7</span> <span> 8</span> <span>$inputs</span> = (<span>object</span>)<span>array</span><span>( </span><span> 9</span> 'encrypt_type' => '', <span>10</span> 'timestamp' => '', <span>11</span> 'nonce' => '', <span>12</span> 'msg_signature' => '', <span>13</span> 'signature' => '' <span>14</span> <span> ); </span><span>15</span> <span>foreach</span> (<span>$inputs</span> <span>as</span> <span>$key</span> => &<span>$value</span><span>) { </span><span>16</span> <span>$tmp</span> = <span>$this</span>->input->get(<span>$key</span><span>); </span><span>17</span> <span>if</span> (!<span>empty</span>(<span>$tmp</span><span>)){ </span><span>18</span> <span>$value</span> = <span>$tmp</span><span>; </span><span>19</span> <span> } </span><span>20</span> <span> } </span><span>21</span> <span>$this</span>->save_key_value('component_verify_ticket_get',json_encode(<span>$inputs</span><span>)); </span><span>22</span> <span>23</span> <span>$fp</span> = <span>fopen</span>("php://input","r"<span>); </span><span>24</span> <span>if</span> (<span>isset</span>(<span>$fp</span>) && !<span>empty</span>(<span>$fp</span><span>)){ </span><span>25</span> <span>$this</span>->post_xml = <span>stream_get_contents</span>(<span>$fp</span><span>); </span><span>26</span> <span>if</span> (<span>empty</span>(<span>$this</span>-><span>post_xml)){ </span><span>27</span> <span>return</span><span>; </span><span>28</span> <span> } </span><span>29</span> <span> } </span><span>30</span> <span>$this</span>->save_key_value('component_verify_ticket_post',<span>$this</span>-><span>post_xml); </span><span>31</span> <span>32</span> <span>$this</span>->xml = <span>str_replace</span>('AppId', 'ToUserName', <span>$this</span>-><span>post_xml); </span><span>33</span> <span>34</span> <span>$msg_xml</span> = ''<span>; </span><span>35</span> <span>$errCode</span> = <span>$wx</span>->decryptMsg(<span>$inputs</span>->msg_signature, <span>$inputs</span>->timestamp, <span>$inputs</span>->nonce, <span>$this</span>->xml, <span>$msg_xml</span><span>); </span><span>36</span> <span>37</span> <span>$componentVerifyTicket</span> = <span>$this</span>->parse_xml(<span>$msg_xml</span>,'ComponentVerifyTicket'<span>); </span><span>38</span> <span>39</span> <span>$this</span>->save_key_value('component_verify_ticket',<span>$componentVerifyTicket</span><span>); </span><span>40</span> <span>if</span> (<span>$errCode</span> == 0<span>){ </span><span>41</span> <span>echo</span> "success"<span>; </span><span>42</span> }<span>else</span><span>{ </span><span>43</span> <span> } </span><span>44</span> <span>return</span><span>; </span><span>45</span> }
分享请注明:
原文出自http://www.cnblogs.com/wenki/p/4700828.html