You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
1.5 KiB

7 years ago
6 years ago
  1. <?php
  2. include_once "wxBizMsgCrypt.php";
  3. // 第三方发送消息给公众平台
  4. $encodingAesKey = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFG";
  5. $token = "pamtest";
  6. $timeStamp = "1409304348";
  7. $nonce = "xxxxxx";
  8. $appId = "wxb11529c136998cb6";
  9. $text = "<xml><ToUserName><![CDATA[oia2Tj我是中文jewbmiOUlr6X-1crbLOvLw]]></ToUserName><FromUserName><![CDATA[gh_7f083739789a]]></FromUserName><CreateTime>1407743423</CreateTime><MsgType><![CDATA[video]]></MsgType><Video><MediaId><![CDATA[eYJ1MbwPRJtOvIEabaxHs7TX2D-HV71s79GUxqdUkjm6Gs2Ed1KF3ulAOA9H1xG0]]></MediaId><Title><![CDATA[testCallBackReplyVideo]]></Title><Description><![CDATA[testCallBackReplyVideo]]></Description></Video></xml>";
  10. $pc = new WXBizMsgCrypt($token, $encodingAesKey, $appId);
  11. $encryptMsg = '';
  12. $errCode = $pc->encryptMsg($text, $timeStamp, $nonce, $encryptMsg);
  13. if ($errCode == 0) {
  14. print("加密后: " . $encryptMsg . "\n");
  15. } else {
  16. print($errCode . "\n");
  17. }
  18. $xml_tree = new DOMDocument();
  19. $xml_tree->loadXML($encryptMsg);
  20. $array_e = $xml_tree->getElementsByTagName('Encrypt');
  21. $array_s = $xml_tree->getElementsByTagName('MsgSignature');
  22. $encrypt = $array_e->item(0)->nodeValue;
  23. $msg_sign = $array_s->item(0)->nodeValue;
  24. $format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>";
  25. $from_xml = sprintf($format, $encrypt);
  26. // 第三方收到公众号平台发送的消息
  27. $msg = '';
  28. $errCode = $pc->decryptMsg($msg_sign, $timeStamp, $nonce, $from_xml, $msg);
  29. if ($errCode == 0) {
  30. print("解密后: " . $msg . "\n");
  31. } else {
  32. print($errCode . "\n");
  33. }