Heim  >  Artikel  >  php教程  >  微信开放平台获取component_verify_ticket - LazyCat_Ky

微信开放平台获取component_verify_ticket - LazyCat_Ky

WBOY
WBOYOriginal
2016-05-20 10:13:181812Durchsuche

官方文档说明: 

在公众号第三方平台创建审核通过后,微信服务器会向其“授权事件接收URL”每隔10分钟定时推送component_verify_ticket。第三方平台方在收到ticket推送后也需进行解密(详细请见【消息加解密接入指引】),接收到后必须直接返回字符串success。

 

第一步是实例化微信提供的类 WXBizMsgCrypt ,传入开发平台的参数。

<span style="font-size: 14px;"><span style="color: #800080;">$pc</span> = <span style="color: #0000ff;">new</span> WXBizMsgCrypt(WxPayConfig::Token, WxPayConfig::EncodingAesKey, WxPayConfig::open_AppID);</span>

微信开放平台获取component_verify_ticket时,除了通过GET获取 timestamp  nonce  encrypt_type msg_sign四个参数外 还需要通过 file_get_contents('php://input') 获取 postdata加密的参数(encryptMsg)。

获取到的 $encryptMsg 是Xml格式的是数据 需要提取出其中的 Encrypt 节点下的数据 如下:

<span style="font-size: 14px;"><span style="color: #008080;">1</span> <span style="color: #800080;">$xml_tree</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> DOMDocument();
</span><span style="color: #008080;">2</span> <span style="color: #800080;">$xml_tree</span>->loadXML(<span style="color: #800080;">$encryptMsg</span><span style="color: #000000;">);
</span><span style="color: #008080;">3</span> <span style="color: #800080;">$array_e</span> = <span style="color: #800080;">$xml_tree</span>->getElementsByTagName('Encrypt'<span style="color: #000000;">);
</span><span style="color: #008080;">4</span> <span style="color: #800080;">$encrypt</span> = <span style="color: #800080;">$array_e</span>->item(0)->nodeValue;</span>

 接下来需要将获取到的密文代入 另一段Xml中通过微信提供的 WXBizMsgCrypt 中的 decryptMsg 函数进行解密 和 通过sha1计算签名(因为 微信提供的 decryptMsg 函数中 要求 $encrypt是 Xml 格式 然后又再次提取 微信提供的做法

<span style="color: #008080;">1</span> <span style="color: #800080;">$format</span> = "<xml><tousername></tousername><encrypt></encrypt></xml>"<span style="color: #000000;">;</span>
<span style="color: #008080;">2</span> <span style="color: #800080;">$from_xml</span> = <span style="color: #008080;">sprintf</span>(<span style="color: #800080;">$format</span>, <span style="color: #800080;">$encrypt</span>);

这是就可以调用 decryptMsg 函数进行解密了

<span style="font-size: 14px;"><span style="color: #008080;"> 1</span> <span style="color: #800080;">$msg</span> = ''<span style="color: #000000;">;
</span><span style="color: #008080;"> 2</span> 
<span style="color: #008080;"> 3</span> <span style="color: #800080;">$errCode</span> = <span style="color: #800080;">$pc</span>->decryptMsg(<span style="color: #800080;">$msg_sign</span>, <span style="color: #800080;">$timeStamp</span>, <span style="color: #800080;">$nonce</span>, <span style="color: #800080;">$from_xml</span>, <span style="color: #800080;">$msg</span><span style="color: #000000;">);
</span><span style="color: #008080;"> 4</span> 
<span style="color: #008080;"> 5</span> <span style="color: #0000ff;">if</span> (<span style="color: #800080;">$errCode</span> == 0<span style="color: #000000;">) {
</span><span style="color: #008080;"> 6</span> <span style="color: #008000;">//</span><span style="color: #008000;">由于返回的也是Xml格式的数据 所以这里再次提取ComponentVerifyTicket节点中的内容</span>
<span style="color: #008080;"> 7</span> <span style="color: #800080;">$xml</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> DOMDocument();
</span><span style="color: #008080;"> 8</span> <span style="color: #800080;">$xml</span>->loadXML(<span style="color: #800080;">$msg</span><span style="color: #000000;">);
</span><span style="color: #008080;"> 9</span> <span style="color: #800080;">$array_e</span> = <span style="color: #800080;">$xml</span>->getElementsByTagName('ComponentVerifyTicket'<span style="color: #000000;">);
</span><span style="color: #008080;">10</span> <span style="color: #800080;">$component_verify_ticket</span> = <span style="color: #800080;">$array_e</span>->item(0)-><span style="color: #000000;">nodeValue;
</span><span style="color: #008080;">11</span> <span style="color: #008000;">//</span><span style="color: #008000;">获取到了$component_verify_ticket后就可以进行写入数据存储了</span>
<span style="color: #008080;">12</span> <span style="color: #0000ff;">echo</span> "success"<span style="color: #000000;">;
</span><span style="color: #008080;">13</span> }<span style="color: #0000ff;">else</span><span style="color: #000000;">{
</span><span style="color: #008080;">14</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$errCode</span><span style="color: #000000;">;
</span><span style="color: #008080;">15</span> }</span>

到此就已经获取到了component_verify_ticket了。

全部代码:

<span style="color: #0000ff;">require_once ("wxBizMsgCrypt.php");<br>public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> index()
    {    

        </span><span style="color: #800080;">$timeStamp</span>    =<span style="color: #800080;">$_GET</span>['timestamp'<span style="color: #000000;">];
        </span><span style="color: #800080;">$nonce</span>        =<span style="color: #800080;">$_GET</span>['nonce'<span style="color: #000000;">];
        </span><span style="color: #800080;">$encrypt_type</span> =<span style="color: #800080;">$_GET</span>['encrypt_type'<span style="color: #000000;">];
        </span><span style="color: #800080;">$msg_sign</span>     =<span style="color: #800080;">$_GET</span>['msg_signature'<span style="color: #000000;">];
        </span><span style="color: #800080;">$encryptMsg</span>   =<span style="color: #008080;">file_get_contents</span>('php://input'<span style="color: #000000;">);
        
        </span><span style="color: #800080;">$result</span> = <span style="color: #800080;">$this</span>->getVerify_Ticket(<span style="color: #800080;">$timeStamp</span>,<span style="color: #800080;">$nonce</span>,<span style="color: #800080;">$encrypt_type</span>,<span style="color: #800080;">$msg_sign</span>,<span style="color: #800080;">$encryptMsg</span><span style="color: #000000;">);

        </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$result</span><span style="color: #000000;">){
            </span><span style="color: #0000ff;">echo</span> "success"<span style="color: #000000;">;
        }

    }


 </span><span style="color: #008000;">//</span><span style="color: #008000;">获取component_verify_ticket</span>
 <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> getVerify_Ticket(<span style="color: #800080;">$timeStamp</span>,<span style="color: #800080;">$nonce</span>,<span style="color: #800080;">$encrypt_type</span>,<span style="color: #800080;">$msg_sign</span>,<span style="color: #800080;">$encryptMsg</span><span style="color: #000000;">){

        </span><span style="color: #800080;">$pc</span> = <span style="color: #0000ff;">new</span> WXBizMsgCrypt(WxPayConfig::Token, WxPayConfig::EncodingAesKey, WxPayConfig::<span style="color: #000000;">open_AppID);

        </span><span style="color: #800080;">$xml_tree</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> DOMDocument();
        </span><span style="color: #800080;">$xml_tree</span>->loadXML(<span style="color: #800080;">$encryptMsg</span><span style="color: #000000;">);
        </span><span style="color: #800080;">$array_e</span> = <span style="color: #800080;">$xml_tree</span>->getElementsByTagName('Encrypt'<span style="color: #000000;">);
        </span><span style="color: #800080;">$encrypt</span> = <span style="color: #800080;">$array_e</span>->item(0)-><span style="color: #000000;">nodeValue;

        </span><span style="color: #800080;">$format</span> = "<xml><tousername></tousername><encrypt></encrypt></xml>"<span style="color: #000000;">;

        </span><span style="color: #800080;">$from_xml</span> = <span style="color: #008080;">sprintf</span>(<span style="color: #800080;">$format</span>, <span style="color: #800080;">$encrypt</span><span style="color: #000000;">);

        </span><span style="color: #800080;">$msg</span> = ''<span style="color: #000000;">;

        </span><span style="color: #800080;">$errCode</span> = <span style="color: #800080;">$pc</span>->decryptMsg(<span style="color: #800080;">$msg_sign</span>, <span style="color: #800080;">$timeStamp</span>, <span style="color: #800080;">$nonce</span>, <span style="color: #800080;">$from_xml</span>, <span style="color: #800080;">$msg</span><span style="color: #000000;">);

        </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$errCode</span> == 0<span style="color: #000000;">) {

            </span><span style="color: #800080;">$xml</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> DOMDocument();
            </span><span style="color: #800080;">$xml</span>->loadXML(<span style="color: #800080;">$msg</span><span style="color: #000000;">);
            </span><span style="color: #800080;">$array_e</span> = <span style="color: #800080;">$xml</span>->getElementsByTagName('ComponentVerifyTicket'<span style="color: #000000;">);

            </span><span style="color: #800080;">$component_verify_ticket</span> = <span style="color: #800080;">$array_e</span>->item(0)-><span style="color: #000000;">nodeValue;
            DB</span>::getDB()->delete("wechat_verifyticket",'uptime!=1'<span style="color: #000000;">);
            DB</span>::getDB()->insert("wechat_verifyticket",<span style="color: #0000ff;">array</span><span style="color: #000000;">(
                </span>'component_verify_ticket'    => <span style="color: #800080;">$component_verify_ticket</span>,
                'uptime'                    => <span style="color: #008080;">time</span><span style="color: #000000;">()));

            </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;
        }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{
            DB</span>::getDB()->delete("wechat_verifyticket",'uptime!=1'<span style="color: #000000;">);
            DB</span>::getDB()->insert("wechat_verifyticket",<span style="color: #0000ff;">array</span><span style="color: #000000;">(
                </span>'component_verify_ticket'    => <span style="color: #800080;">$errCode</span>,
                'uptime'                    => <span style="color: #008080;">time</span><span style="color: #000000;">()));
            </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;
        }

    }</span>

 

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn