微信開放平台擷取component_verify_ticket,cas用戶端擷取ticket_PHP教程

來源:互聯網
上載者:User

開放平台擷取component_verify_ticket,cas用戶端擷取ticket


官方文檔說明:

在公眾號第三方平台建立審核通過後,伺服器會向其“授權事件接收URL”每隔10分鐘定時推送component_verify_ticket。第三方平台方在收到ticket推送後也需進行解密(詳細請見【訊息加解密接入指引】),接收到後必須直接返回字串success。

第一步是執行個體化提供的類 WXBizMsgCrypt ,傳入開發平台的參數。

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

開放平台擷取component_verify_ticket時,除了通過GET擷取 timestamp nonce encrypt_type msg_sign四個參數外 還需要通過 file_get_contents('php://input') 擷取 postdata加密的參數(encryptMsg)。

擷取到的 $encryptMsg 是Xml格式的是資料 需要提取出其中的 Encrypt 節點下的資料 如下:

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

接下來需要將擷取到的密文代入 另一段Xml中通過提供的 WXBizMsgCrypt 中的 decryptMsg 函數進行解密 和 通過sha1計算簽名(因為 提供的 decryptMsg 函數中 要求 $encrypt是 Xml 格式 然後又再次提取 提供的做法

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

這是就可以調用 decryptMsg 函數進行解密了

 1 $msg = ''; 2  3 $errCode = $pc->decryptMsg($msg_sign, $timeStamp, $nonce, $from_xml, $msg); 4  5 if ($errCode == 0) { 6 //由於返回的也是Xml格式的資料 所以這裡再次提取ComponentVerifyTicket節點中的內容 7 $xml = new DOMDocument(); 8 $xml->loadXML($msg); 9 $array_e = $xml->getElementsByTagName('ComponentVerifyTicket');10 $component_verify_ticket = $array_e->item(0)->nodeValue;11 //擷取到了$component_verify_ticket後就可以進行寫入資料存放區了12 echo "success";13 }else{14 echo $errCode;15 }

到此就已經擷取到了component_verify_ticket了。

全部代碼:

require_once ("wxBizMsgCrypt.php");
public function index() { $timeStamp =$_GET['timestamp']; $nonce =$_GET['nonce']; $encrypt_type =$_GET['encrypt_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"; } } //擷取component_verify_ticket public 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; } }

http://www.bkjia.com/PHPjc/1125992.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1125992.htmlTechArticle開放平台擷取component_verify_ticket,cas用戶端擷取ticket 官方文檔說明: 在公眾號第三方平台建立審核通過後,伺服器會向其授權事件...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.