微信开放平台 获取 component_verify_ticket

<?php
/**
* @author zhaozhuobin
* @date:2016-06=07
*
*/ namespace common\components;use DOMDocument;
use WXBizMsgCrypt;
use yii\base\Action;
use Yii; /**
* 获取微信 component_verify_ticket
*
* <xml>
* <AppId> AppId</AppId>
* <CreateTime> 1413192605 </CreateTime>
* <InfoType> component_verify_ticket </InfoType>
* <ComponentVerifyTicket> 内容 </ComponentVerifyTicket>
* </xml>
*
* Class WechatAction
* @package common\components
*/
class WechatAction extends Action
{ public $openWechat; /**
* @return string|void
*/
public function run()
{
$this->openWechat = Yii::$app->openWechat;
$param = $this->getParam();
$encryptMsg = file_get_contents('php://input');$result = $this->getVerify_Ticket($param['timestamp'],$param['nonce'],$param['msg_signature'],$encryptMsg);
if($result){
return "success";
}
} /**
* 获取参数
* @return array|mixed
*/
public function getParam()
{
$param = Yii::$app->request->get();
$param['timestamp'] = empty($param['timestamp']) ? "" : trim($param['timestamp']);
$param['nonce'] = empty($param['nonce']) ? "" : trim($param['nonce']);
$param['msg_signature'] = empty($param['msg_signature']) ? "" : trim($param['msg_signature']);
$param['signature'] = empty($param['signature']) ? "" : trim($param['signature']);
return $param;
} /**
* 获取 component_verify_ticket
* @param $timeStamp
* @param $nonce
* @param $msg_sign
* @param $encryptMsg
* @return bool
*/
public function getVerify_Ticket($timeStamp, $nonce, $msg_sign, $encryptMsg)
{
@include_once (dirname(__DIR__).'/messageCrypt/wxBizMsgCrypt.php');
$pc = new WXBizMsgCrypt($this->openWechat->token, $this->openWechat->encodingAesKey, $this->openWechat->appId); $xml_tree = new DOMDocument();
$xml_tree->loadXML($encryptMsg);
$array_e = $xml_tree->getElementsByTagName('Encrypt');
$encrypt = $array_e->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) {
$xml = new DOMDocument();
$xml->loadXML($msg);
$array_e = $xml->getElementsByTagName('ComponentVerifyTicket');
$component_verify_ticket = $array_e->item(0)->nodeValue; //=======这里把 component_verify_ticket 保存到数据库========// return true;
}else{
//=======这里记录错误码========//
return false;
}
}
}
上一篇:WindowsclientC/C++编程规范“建议”——前言


下一篇:1027 姓名与ID[二分图匹配(匈牙利)]