首頁 > php教程 > php手册 > 微信开放平台获取component_verify_ticket,cas客户端获取ticket

微信开放平台获取component_verify_ticket,cas客户端获取ticket

WBOY
發布: 2016-06-13 08:40:08
原創
1029 人瀏覽過

微信开放平台获取component_verify_ticket,cas客户端获取ticket

官方文档说明: 

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

 

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

<span><span>$pc</span> = <span>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><span>1</span> <span>$xml_tree</span> = <span>new</span><span> DOMDocument();
</span><span>2</span> <span>$xml_tree</span>->loadXML(<span>$encryptMsg</span><span>);
</span><span>3</span> <span>$array_e</span> = <span>$xml_tree</span>->getElementsByTagName('Encrypt'<span>);
</span><span>4</span> <span>$encrypt</span> = <span>$array_e</span>->item(0)->nodeValue;</span>
登入後複製

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

<span>1</span> <span>$format</span> = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>"<span>;</span>
<span>2</span> <span>$from_xml</span> = <span>sprintf</span>(<span>$format</span>, <span>$encrypt</span>);
登入後複製

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

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

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

全部代码:

<span>require_once ("wxBizMsgCrypt.php");<br />public</span> <span>function</span><span> index()
    {    

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

        </span><span>if</span>(<span>$result</span><span>){
            </span><span>echo</span> "success"<span>;
        }

    }


 </span><span>//</span><span>获取component_verify_ticket</span>
 <span>public</span> <span>function</span> getVerify_Ticket(<span>$timeStamp</span>,<span>$nonce</span>,<span>$encrypt_type</span>,<span>$msg_sign</span>,<span>$encryptMsg</span><span>){

        </span><span>$pc</span> = <span>new</span> WXBizMsgCrypt(WxPayConfig::Token, WxPayConfig::EncodingAesKey, WxPayConfig::<span>open_AppID);

        </span><span>$xml_tree</span> = <span>new</span><span> DOMDocument();
        </span><span>$xml_tree</span>->loadXML(<span>$encryptMsg</span><span>);
        </span><span>$array_e</span> = <span>$xml_tree</span>->getElementsByTagName('Encrypt'<span>);
        </span><span>$encrypt</span> = <span>$array_e</span>->item(0)-><span>nodeValue;

        </span><span>$format</span> = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>"<span>;

        </span><span>$from_xml</span> = <span>sprintf</span>(<span>$format</span>, <span>$encrypt</span><span>);

        </span><span>$msg</span> = ''<span>;

        </span><span>$errCode</span> = <span>$pc</span>->decryptMsg(<span>$msg_sign</span>, <span>$timeStamp</span>, <span>$nonce</span>, <span>$from_xml</span>, <span>$msg</span><span>);

        </span><span>if</span> (<span>$errCode</span> == 0<span>) {

            </span><span>$xml</span> = <span>new</span><span> DOMDocument();
            </span><span>$xml</span>->loadXML(<span>$msg</span><span>);
            </span><span>$array_e</span> = <span>$xml</span>->getElementsByTagName('ComponentVerifyTicket'<span>);

            </span><span>$component_verify_ticket</span> = <span>$array_e</span>->item(0)-><span>nodeValue;
            DB</span>::getDB()->delete("wechat_verifyticket",'uptime!=1'<span>);
            DB</span>::getDB()->insert("wechat_verifyticket",<span>array</span><span>(
                </span>'component_verify_ticket'    => <span>$component_verify_ticket</span>,
                'uptime'                    => <span>time</span><span>()));

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

    }</span>
登入後複製

 

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板