• 微信开放平台 获取 component_verify_ticket


    <?php
    /**
     * @author zhaozhuobin
     * @date:2016-06=07
     *
     */
    
    namespace commoncomponents;use DOMDocument;
    use WXBizMsgCrypt;
    use yiiaseAction;
    use Yii;
    
    /**
     *  获取微信 component_verify_ticket
     *
     * <xml>
     *   <AppId> AppId</AppId>
     *   <CreateTime> 1413192605 </CreateTime>
     *   <InfoType> component_verify_ticket </InfoType>
     *   <ComponentVerifyTicket> 内容 </ComponentVerifyTicket>
     * </xml>
     *
     * Class WechatAction
     * @package commoncomponents
     */
    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;
            }
        }
    }
  • 相关阅读:
    Java基础--线程创建方式
    Java基础--static关键字
    Java基础--异常处理
    mybatis的#{}和${}的区别以及order by注入问题
    前后端分离结构中使用shiro进行权限控制
    Java FTP下载文件
    10个经典智力推理题!据说答对7道,智力在140!
    Java面试题总结之数据结构、算法和计算机基础(刘小牛和丝音的爱情故事1)
    Java面试题总结之JDBC 和Hibernate
    Java面试题总结之数据库与SQL语句
  • 原文地址:https://www.cnblogs.com/boystar/p/5595420.html
Copyright © 2020-2023  润新知