zoukankan      html  css  js  c++  java
  • 微信开放平台 获取 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;
            }
        }
    }
  • 相关阅读:
    1046 Shortest Distance (20 分)(模拟)
    1004. Counting Leaves (30)PAT甲级真题(bfs,dfs,树的遍历,层序遍历)
    1041 Be Unique (20 分)(hash散列)
    1036 Boys vs Girls (25 分)(查找元素)
    1035 Password (20 分)(字符串处理)
    1044 Shopping in Mars (25 分)(二分查找)
    onenote使用小Tip总结^_^(不断更新中...)
    1048 Find Coins (25 分)(hash)
    三个故事
    领导者的举止
  • 原文地址:https://www.cnblogs.com/boystar/p/5595420.html
Copyright © 2011-2022 走看看