private function savePostData() { $xml = ''; if ($this->encrypted) { $errCode = $this->msgCryptor->decryptMsg($_GET['msg_signature'], $_GET['timestamp'], $_GET['nonce'], $GLOBALS['HTTP_RAW_POST_DATA'], $xml); if ($errCode != 0) { exit($errCode); } } else { $xml = $GLOBALS['HTTP_RAW_POST_DATA']; } $xml = (array) simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA); $this->request = array_change_key_case($xml, CASE_LOWER); // 将数组键名转换为小写,提高健壮性,减少因大小写不同而出现的问题 }
public function decodeMsg($msg) { import("@.ORG.aes.WXBizMsgCrypt"); $sReqMsgSig = $_GET['msg_signature']; $sReqTimeStamp = $_GET['timestamp']; // $sReqNonce = HttpUtils.ParseUrl("nonce"); $sReqNonce = $_GET['nonce']; // post请求的密文数据 // $sReqData = HttpUtils.PostData(); $sReqData = $msg; $sMsg = ""; // 解析之后的明文 $pc = new WXBizMsgCrypt($this->wxuser['pigsecret'], $this->wxuser['aeskey'], $this->wxuser['appid']); $errCode = $pc->decryptMsg($sReqMsgSig, $sReqTimeStamp, $sReqNonce, $sReqData, $sMsg); if ($errCode == 0) { /* $xml = new DOMDocument(); $xml->loadXML($sMsg); $content = $xml->getElementsByTagName('Content')->item(0)->nodeValue; */ $data = array(); $xml = new SimpleXMLElement($sMsg); $xml || exit; foreach ($xml as $key => $value) { $data[$key] = strval($value); } return $data; } else { return $errCode; } }
public function decodeMsg($msg) { import('@.ORG.aes.WXBizMsgCrypt'); $sReqMsgSig = $_GET['msg_signature']; $sReqTimeStamp = $_GET['timestamp']; $sReqNonce = $_GET['nonce']; $sReqData = $msg; $sMsg = ''; $pc = new WXBizMsgCrypt($this->wxuser['pigsecret'], $this->wxuser['aeskey'], $this->wxuser['appid']); $errCode = $pc->decryptMsg($sReqMsgSig, $sReqTimeStamp, $sReqNonce, $sReqData, $sMsg); if ($errCode == 0) { $data = array(); $xml = new SimpleXMLElement($sMsg); $xml || exit(); foreach ($xml as $key => $value) { $data[$key] = strval($value); } return $data; } else { return $errCode; } }
function setTicket() { include_once ONETHINK_ADDON_PATH . 'PublicBind/wxBizMsgCrypt.php'; // 第三方发送消息给公众平台 $encodingAesKey = 'DfEqNBRvzbg8MJdRQCSGyaMp6iLcGOldKFT0r8I6Tnp'; $token = 'weiphp'; $appId = D('Addons://PublicBind/PublicBind')->component_appid; $timeStamp = empty($_GET['timestamp']) ? "" : trim($_GET['timestamp']); $nonce = empty($_GET['nonce']) ? "" : trim($_GET['nonce']); $msg_sign = empty($_GET['msg_signature']) ? "" : trim($_GET['msg_signature']); $encryptMsg = file_get_contents('php://input'); $pc = new \WXBizMsgCrypt($token, $encodingAesKey, $appId); // 第三方收到公众号平台发送的消息 $msg = ''; $errCode = $pc->decryptMsg($msg_sign, $timeStamp, $nonce, $encryptMsg, $msg); if ($errCode == 0) { $data = $this->_xmlToArr($msg); $map['name'] = 'PublicBind'; $config = M('addons')->where($map)->getField('config'); $config = (array) json_decode($config, true); $config['ComponentVerifyTicket'] = $data['ComponentVerifyTicket']; $save['config'] = json_encode($config); M('addons')->where($map)->save($save); echo 'success'; } else { addWeixinLog('解密后失败:' . $errCode, 'setTicket_error'); } }
private function savePostData() { $xml = ''; $data = file_get_contents('php://input'); if ($this->encrypted) { $errCode = $this->msgCryptor->decryptMsg($_GET['msg_signature'], $_GET['timestamp'], $_GET['nonce'], $data, $xml); if ($errCode != 0) { exit($errCode); } } else { $xml = $data; } $xml = (array) simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA); $this->request = array_change_key_case($xml, CASE_LOWER); // 将数组键名转换为小写,提高健壮性,减少因大小写不同而出现的问题 }
public function responseMsg() { $wxMsgCrypt = new WXBizMsgCrypt($this->token, $this->enaeskey, $this->appid); $Common = new Common(); $WxModel = new WxModel(); $postStr = file_get_contents("php://input"); $WxModel->recordLog($postStr); if (!empty($postStr)) { $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $WxModel->recordLog('signature=' . $signature); $WxModel->recordLog('timestamp=' . $timestamp); $WxModel->recordLog('nonce=' . $nonce); $msg = ''; $errCode = $wxMsgCrypt->decryptMsg($signature, $timestamp, $nonce, $postStr, $msg); if ($errCode == 0) { $WxModel = new WxModel(); $WxModel->recordLog($msg); libxml_disable_entity_loader(true); $postObj = simplexml_load_string($msg, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $MsgType = $postObj->MsgType; $Event = $postObj->Event; $WxModel->recordLog('MsgType=' . $MsgType . ' Event=' . $Event); $keyword = trim($postObj->Content); $resultStr = $WxModel->fix($MsgType, $Event, $keyword, $fromUsername, $toUsername, $timestamp); $WxModel->recordLog($resultStr); if ($resultStr) { $encryptMsg = ''; $errCode = $wxMsgCrypt->encryptMsg($resultStr, $timestamp, $nonce, $encryptMsg); if ($errCode == 0) { echo $encryptMsg; } else { $WxModel->recordLog('encryptMsg:errCode=' . $errCode); } } } else { $WxModel->recordLog('decryptMsg:errCode=' . $errCode); } } else { echo ""; exit; } }
public function decodeMsg($msg) { import("@.ORG.aes.WXBizMsgCrypt"); $pc = new WXBizMsgCrypt($this->_config["wechat_token"], $this->_config["wechat_encodingaeskey"], $this->_config["wechat_appid"]); $sMsg = ""; $errCode = $pc->decryptMsg($this->msg_signature, $this->sTimeStamp, $this->nonce, $msg, $sMsg); if ($errCode == 0) { $data = array(); $xml = new SimpleXMLElement($sMsg); $xml || exit; foreach ($xml as $key => $value) { $data[$key] = strval($value); } return $data; } else { return $errCode; } }
public function decodeMsg($msg) { import('source.class.aes.WXBizMsgCrypt'); $pc = new WXBizMsgCrypt($this->_config['wx_token'], $this->_config['wx_encodingaeskey'], $this->_config['wx_appid']); $sMsg = ''; $errCode = $pc->decryptMsg($this->msg_signature, $this->sTimeStamp, $this->nonce, $msg, $sMsg); if ($errCode == 0) { $data = array(); $xml = new SimpleXMLElement($sMsg); $xml || exit; foreach ($xml as $key => $value) { $data[$key] = strval($value); } return $data; } else { return $errCode; } }
private function _wxlogic() { //接口认证 if (IS_GET && $_GET['signature'] && $_GET['timestamp'] && $_GET['nonce'] && $_GET['echostr']) { $this->_auth(); exit; } $signature = $_REQUEST['signature']; $msg_signature = $_REQUEST['msg_signature']; $timestamp = $_REQUEST['timestamp']; $nonce = $_REQUEST['nonce']; $postdata = file_get_contents("php://input"); //处理微信消息 if ($signature && $timestamp && $nonce) { include VENDOR_PATH . 'Weixin/wxBizMsgCrypt.php'; $Token = C('WX.Token'); $EncodingAESKey = C('WX.EncodingAESKey'); $AppID = C('WX.AppID'); $Weixin = new \WXBizMsgCrypt($Token, $EncodingAESKey, $AppID); $xmldata = ''; $errCode = $Weixin->decryptMsg($msg_signature, $timestamp, $nonce, $postdata, $xmldata); if ($errCode != 0) { echo $errCode; exit; } else { $XMLDom = new \DOMDocument(); $XMLDom->loadXML($xmldata); $MsgType = $XMLDom->getElementsByTagName('MsgType')->item(0)->nodeValue; $Event = $XMLDom->getElementsByTagName('Event')->item(0)->nodeValue; //记录地理位置 if ($MsgType == 'event' && $Event == 'LOCATION') { $FromUserName = $XMLDom->getElementsByTagName('FromUserName')->item(0)->nodeValue; $Latitude = $XMLDom->getElementsByTagName('Latitude')->item(0)->nodeValue; $Longitude = $XMLDom->getElementsByTagName('Longitude')->item(0)->nodeValue; D('User')->saveWXUserLatLng($FromUserName, $Latitude, $Longitude); } echo true; exit; } } }
// 第三方发送消息给公众平台 $encodingAesKey = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFG"; $token = "pamtest"; $timeStamp = "1409304348"; $nonce = "xxxxxx"; $appId = "wxb11529c136998cb6"; $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>"; $pc = new WXBizMsgCrypt($token, $encodingAesKey, $appId); $encryptMsg = ''; $errCode = $pc->encryptMsg($text, $timeStamp, $nonce, $encryptMsg); if ($errCode == 0) { print "加密后: " . $encryptMsg . "\n"; } else { print $errCode . "\n"; } $xml_tree = new DOMDocument(); $xml_tree->loadXML($encryptMsg); $array_e = $xml_tree->getElementsByTagName('Encrypt'); $array_s = $xml_tree->getElementsByTagName('MsgSignature'); $encrypt = $array_e->item(0)->nodeValue; $msg_sign = $array_s->item(0)->nodeValue; $format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>"; $from_xml = sprintf($format, $encrypt); // 第三方收到公众号平台发送的消息 $msg = ''; $errCode = $pc->decryptMsg($msg_sign, $timeStamp, $nonce, $from_xml, $msg); if ($errCode == 0) { print "解密后: " . $msg . "\n"; } else { print $errCode . "\n"; }
function msgHandler() { include_once ONETHINK_ADDON_PATH . 'PublicBind/wxBizMsgCrypt.php'; // 第三方发送消息给公众平台 $encodingAesKey = 'Wry3C6IG6yzFR55cnje0W7CWd2jqlqQrAIK5ilOIfo3'; $token = 'youkang120'; $appId = D('Addons://PublicBind/PublicBind')->component_appid; $timeStamp = empty($_GET['timestamp']) ? "" : trim($_GET['timestamp']); $nonce = empty($_GET['nonce']) ? "" : trim($_GET['nonce']); $msg_sign = empty($_GET['msg_signature']) ? "" : trim($_GET['msg_signature']); $encryptMsg = file_get_contents('php://input'); $pc = new \WXBizMsgCrypt($token, $encodingAesKey, $appId); // 第三方收到公众号平台发送的消息 $msg = ''; $errCode = $pc->decryptMsg($msg_sign, $timeStamp, $nonce, $encryptMsg, $msg); if ($errCode == 0) { $data = $this->_xmlToArr($msg); error_log("公众号消息:" . $msg); $map['name'] = 'PublicBind'; $config = M('addons')->where($map)->getField('config'); $config = (array) json_decode($config, true); $config['ComponentVerifyTicket'] = $data['ComponentVerifyTicket']; $save['config'] = json_encode($config); M('addons')->where($map)->save($save); echo 'success'; } else { addWeixinLog('解密后失败:' . $errCode, 'setTicket_error'); } }
private function route() { $nonce = isset($_REQUEST['nonce']) ? $_REQUEST['nonce'] : ''; $sTimeStamp = isset($_REQUEST['timestamp']) ? $_REQUEST['timestamp'] : time(); $msg_signature = isset($_REQUEST['msg_signature']) ? $_REQUEST['msg_signature'] : ''; $xml = $GLOBALS["HTTP_RAW_POST_DATA"]; import("@.ORG.aes.WXBizMsgCrypt"); $pc = new WXBizMsgCrypt($this->config['wx_token'], $this->config['wx_encodingaeskey'], $this->config['wx_appid']); $sMsg = ""; $pc->decryptMsg($msg_signature, $sTimeStamp, $nonce, $xml, $sMsg); $data = $this->api_notice_increment('http://we-cdn.net', $sMsg); $data = str_replace('<?xml version="1.0"?>', '', $data); $encryptMsg = ""; $pc->encryptMsg($data, $sTimeStamp, $nonce, $encryptMsg); return $encryptMsg; }
public function responseMsg() { $postStr = ''; if (isset($_GET['encrypt_type']) && $_GET['encrypt_type'] == 'aes') { $pc = new WXBizMsgCrypt(TOKEN, Yii::app()->params['encodingAesKey'], APP_ID); $errCode = $pc->decryptMsg($_GET['msg_signature'], $_GET['timestamp'], $_GET['nonce'], $GLOBALS["HTTP_RAW_POST_DATA"], $postStr); if ($errCode != 0) { //如果第一次解密失败,那么再试一次上一个串 $pc = new WXBizMsgCrypt(TOKEN, Yii::app()->params['encodingAesKey2'], APP_ID); $errCode = $pc->decryptMsg($_GET['msg_signature'], $_GET['timestamp'], $_GET['nonce'], $GLOBALS["HTTP_RAW_POST_DATA"], $postStr); if ($errCode > 0) { Yii::log($errCode, 'error', 'WX_MESSAGE_AES_ERROR'); Yii::app()->end(); } $this->encodingAesKey = Encoding_Aes_Key; } else { $this->encodingAesKey = Encoding_Aes_Key2; } $this->restype = $_GET['encrypt_type']; Yii::log(CVarDumper::dumpAsString($postStr), 'trace', 'WX_MESSAGE_AES_RESPONSE'); } else { $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; } if (!empty($postStr)) { $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $messagetype = $postObj->MsgType; $time = time(); if ($messagetype == 'text') { $this->returnText('I love you', $postObj); } else { if ($messagetype == 'event') { $this->messageEvent($postObj); } } if ($messagetype != 'event') { $this->messageLog($postObj); } else { $this->eventLog($postObj); } } else { echo ""; exit; } }
/** * 公众平台解密 * For weixin server validation * @param bool $return 是否返回 */ public function platform_DecryptMsg() { // $_REQUEST=array ( // 'ctl' => 'weixin', // 'act' => 'accept', // 'signature' => '7a883244cd389ce5e7a068554973bd031ae0a3e7', // 'timestamp' => '1435132193', // 'nonce' => '14138352', // 'encrypt_type' => 'aes', // 'msg_signature' => '7dc88fe1d54181065553a15ca4178c3ef7c11d6e', // ); $msg_sign = $_REQUEST['msg_signature']; $timeStamp = $_REQUEST['timestamp']; $nonce = $_REQUEST['nonce']; $from_xml = file_get_contents("php://input"); //$this->log($from_xml); $msg = ''; $return = array('status' => 1, 'info' => ''); $pc = new WXBizMsgCrypt($this->platform_token, $this->platform_encodingAesKey, $this->platform_appid); $errCode = $pc->decryptMsg($msg_sign, $timeStamp, $nonce, $from_xml, $msg); if ($errCode == 0) { $return['info'] = (array) simplexml_load_string($msg, 'SimpleXMLElement', LIBXML_NOCDATA); $this->_receive = $return['info']; $this->addRecord(); } else { $return['status'] = 0; $return['info'] = $errCode; } return $return; }