public function actionTicket()
{
//开启session
session_start();
//appId
$appId = 'wx73d0c47a64aa5315';
//secret
$appSecret = 'aba2793c10623350f6aeee5a728099d3';
if (!isset($_SESSION['ticket'])){
//获取微信公众号全局唯一接口调用凭据access_token
$result = $this->getAccessToken($appId, $appSecret);
$accessToken = $result['access_token'];
//获取jsapi_ticket
$getTicket = $this->getTicket($accessToken);
$ticket = $getTicket['ticket'];
$_SESSION['ticket'] = $ticket;
}
//jsapi_ticket(公众号用于调用微信JS接口的临时票据)
$ticket = $_SESSION['ticket'];
//当前时间戳
$timestamp = time();
//随机字符串
$noncestr = $this->getRandCode();
//当前url
$url = $this->getUrl();
$params = [
'jsapi_ticket' => $ticket,
'timestamp' => $timestamp,
'noncestr' => $noncestr,
'url' => $url,
];
$options = $this->urlParams($params);
//获取签名
$signature = sha1($options);
//将appId,timestamp,noncestr,signature渲染到页面
return $this->render('ticket', [
'appId' => $appId,
'timestamp' => $timestamp,
'noncestr' => $noncestr,
'signature' => $signature,
]);
}
/*
* 获取jsapi_ticket
*/
public function getTicket($accessToken)
{
$params = [
'access_token' => $accessToken,
'type' => 'jsapi',
];
$urlParams = $this->urlParams($params);
$ticketUrl = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?' . $urlParams;
$result = $this->http_curl($ticketUrl);
return json_decode($result, true);
}
/*
* 获取微信公众号的全局唯一接口调用凭据access_token
*/
public function getAccessToken($appId, $appSecret)
{
$params = [
'grant_type' => 'client_credential',
'appid' => $appId,
'secret' => $appSecret,
];
$urlParams = $this->urlParams($params);
$accessUrl = 'https://api.weixin.qq.com/cgi-bin/token?' . $urlParams;
$result = $this->http_curl($accessUrl);
return json_decode($result, true);
}
/*
* 获取随机码
*/
function getRandCode($num = 16){
$array = array(
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','v','z',
'1','2','3','4','5','6','7','8','9','0',
);
$tmpstr='';
$max = count($array);
for($i=1; $i<=$num; $i++){
$key = rand(0,$max-1); //'A' -> $array[0]
$tmpstr .= $array[$key];
}
return $tmpstr;
}
/*
* curl接口调用
*/
public function http_curl($url, $data=null) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
/*
* 字符串拼接
*/
public function urlParams($params)
{
ksort($params);
reset($params);
$options = '';
foreach ($params as $key => $value) {
$options .= $key . '=' . $value .'&';
}
$options = rtrim($options, '&');
return $options;
}
/*
* 获取当前url
*/
public function getUrl()
{
//获取协议类型
$protocalPort = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
//获取当前执行脚本的url
$phpSelf = $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
$pathInfo = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
$queryString = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : '';
$relateUrl = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $phpSelf . (!empty($queryString) ? '?' . $queryString : $pathInfo);
$url = $protocalPort . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '') . $relateUrl;
return $url;
}
|