【php】记微信第三方平台通过ticket 并解密 然后授权相关接口/获取令牌 获取component_access_token

<?php
namespace app\api\controller;
use think\Controller;
use think\facade\Request;
use think\Db;
use think\facade\Cache;


class WxopenController extends Controller{
    
    /*第三方平台 appid secret*/
    private $component_appid = '';
    private $component_appsecret = '';
    private $encodingAESKey = '';

    public function getTicket(){
        if (!$xml = file_get_contents('php://input')) {
           error_log("\msg:无参数\n",   3,   "./wxNotify.log");
        }
        
        $data = $this->fromXml($xml); 
        Cache::set('component_verify_ticket',$data['Encrypt'],600);
        $jsdata = json_encode($data);
        
        echo 'success';
        error_log(date('Y-m-d H:i:s')."--Ticket: ".$jsdata."\r\n",   3,   "./Ticket.log");
        // echo 1;
    }    
    

    /*接口文档 /授权相关接口 /获取令牌*/
    /*https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/ThirdParty/token/component_access_token.html*/
    public function get_component_access_token(){
        $url = 'https://api.weixin.qq.com/cgi-bin/component/api_component_token';
        $parmas = array();
        $parmas['component_appid'] = $this->component_appid;
        $parmas['component_appsecret'] = $this->component_appsecret;
        
        $ticket = Cache::get('component_verify_ticket');
        // $ticket = 'ticket@@@aOYUqeoDKqQokXTflbh2MweS_plRfEQqiQ1kuoxkeRyAgPuzqsT93EMkWFCF3EH6_TWXJL0ZObjf9Dbl5B7gaA';
        $sa  = $this->decryptMsg($ticket);
        $saArr = $this->fromXml($sa); 
        $ticket = $saArr['ComponentVerifyTicket'];
        
        
        $parmas['component_verify_ticket'] = $ticket;
        // dump($parmas);
        $result = curl_send($url,json_encode($parmas));
        dump($result);
    }
    
    /*解密ticket*/
    private function decryptMsg($msg_encrypt)
    {

        $EncodingAESKey = $this->encodingAESKey;

        $AESKey = base64_decode($EncodingAESKey.'=');

        $iv = substr($AESKey, 0, 16);

        $msg_decode = base64_decode($msg_encrypt);

        $msg = openssl_decrypt($msg_decode, 'AES-256-CBC', $AESKey, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv);

        $msg_len = unpack('N', substr($msg, 16, 4));

        $len = $msg_len[1];

        $xml = substr($msg, 20, $len);



        return $xml;

    }
    protected function curlPost($url, $post_data, $timeout = 5)
    {
        $ch = curl_init();

        if (stripos($url, 'https://') !== false) {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($ch, CURLOPT_SSLVERSION, 1);
        }

        $header = empty($header) ? '' : $header;

        if (is_string($post_data)) {
            $strPOST = $post_data;
        }
        else {
            $aPOST = array();

            foreach ($post_data as $key => $val) {
                $aPOST[] = $key . '=' . urlencode($val);
            }

            $strPOST = join('&', $aPOST);
        }

        curl_setopt($ch, CURLOPT_HTTP_VERSION, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $strPOST);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        $result = curl_exec($ch);
        $aStatus = curl_getinfo($ch);
        curl_close($ch);

        if (intval($aStatus['http_code']) == 200) {
            return $result;
        }
        else {
            return false;
        }
    }
    /**
     * 将xml转为array
     * @param $xml
     * @return mixed
     */
    private function fromXml($xml)
    {
        // 禁止引用外部xml实体
        libxml_disable_entity_loader(true);
        return json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
    }
    
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值