记微信第三方平台通过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); } }