WeChat Open Platform get component_verify_ticket-LazyCat_Ky

Source: Internet
Author: User
Open platform to get component_verify_ticket-LazyCat_Ky official documentation instructions:

After the third-party platform of the public account is created and approved, the server will push component_verify_ticket to the "URL for receiving authorization events" every 10 minutes. After receiving the push from ticket, the third-party platform also needs to decrypt it (for details, see [message encryption and decryption access guide]). after receiving the push, the success string must be directly returned.

The first step is to instantiate the WXBizMsgCrypt class provided and pass in the parameters of the development platform.

$pc = new WXBizMsgCrypt(WxPayConfig::Token, WxPayConfig::EncodingAesKey, WxPayConfig::open_AppID);

When the open platform obtains component_verify_ticket, in addition to getting timestamp nonce encrypt_type msg_sign through GET, it also needs to GET the postdata encrypted parameter (encryptMsg) through file_get_contents ('php: // input ).

Obtained $EncryptMsg is in Xml format. the data under the Encrypt node must be extracted as follows:

1 $xml_tree = new DOMDocument();2 $xml_tree->loadXML($encryptMsg);3 $array_e = $xml_tree->getElementsByTagName('Encrypt');4 $encrypt = $array_e->item(0)->nodeValue;

Next, you need to substitute the obtained ciphertext into another Xml file to decrypt it and calculate the signature through the decryptMsg function in the provided WXBizMsgCrypt (because the providedRequirements in the decryptMsg function$Encrypt is in Xml format and then extracts the provided method again)

1 $format = "
 
  toUser
  
  %s
  
 ";2 $from_xml = sprintf($format, $encrypt);

You can call the decryptMsg function to decrypt the data.

1 $ msg = ''; 2 3 $ errCode = $ pc-> decryptMsg ($ msg_sign, $ timeStamp, $ nonce, $ from_xml, $ msg ); 4 5 if ($ errCode = 0) {6 // because the returned data is in Xml format, extract the content from the ComponentVerifyTicket node again. 7 $ xml = new DOMDocument (); 8 $ xml-> loadXML ($ msg); 9 $ array_e = $ xml-> getElementsByTagName ('componentverifyticket '); 10 $ component_verify_ticket = $ array_e-> item (0) -> nodeValue; 11 // after obtaining $ component_verify_ticket, you can write data and store 12 echo "success"; 13} else {14 echo $ errCode; 15}

At this point, you have obtained component_verify_ticket.

All code:

Require_once ("wxBizMsgCrypt. php ");
Public function index () {$ timeStamp = $ _ GET ['timestamp']; $ nonce = $ _ GET ['nonce ']; $ encrypt_type = $ _ GET ['encryption _ type']; $ msg_sign = $ _ GET ['MSG _ signature ']; $ encryptMsg = file_get_contents ('php: // input '); $ result = $ this-> getVerify_Ticket ($ timeStamp, $ nonce, $ encrypt_type, $ msg_sign, $ encryptMsg); if ($ result) {echo "success" ;}}// get javaspublic function getVerify_Ticket ($ timeStamp, $ nonce, $ encrypt_type, $ msg_sign, $ encryptMsg) {$ pc = new WXBizMsgCrypt (WxPayConfig:: Token, WxPayConfig: EncodingAesKey, WxPayConfig: open_AppID); $ xml_tree = new DOMDocument (); $ xml_tree-> loadXML ($ encryptMsg ); $ array_e = $ xml_tree-> getElementsByTagName ('encrypt'); $ Encrypt = $ array_e-> item (0)-> nodeValue; $ format =" toUser %s "; $ From_xml = sprintf ($ format, $ encrypt); $ msg =''; $ errCode = $ pc-> decryptMsg ($ msg_sign, $ timeStamp, $ nonce, $ from_xml, $ msg); if ($ errCode = 0) {$ xml = new DOMDocument (); $ xml-> loadXML ($ msg ); $ array_e = $ xml-> getElementsByTagName ('componentverifyticket '); $ component_verify_ticket = $ array_e-> item (0)-> nodeValue; DB: getDB () -> delete ("wechat_verifyticket", 'uptime! = 1'); DB: getDB ()-> insert ("wechat_verifyticket", array ('component _ verify_ticket '=> $ component_verify_ticket, 'uptime' => time (); return true;} else {DB: getDB ()-> delete ("wechat_verifyticket", 'uptime! = 1'); DB: getDB ()-> insert ("wechat_verifyticket", array ('component _ verify_ticket '=> $ errCode, 'uptime' => time (); return false ;}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.