网站首页 博客 微信获取AccessToken和网页签名
类定义如下:
<?php
namespace app\components;
use Yii;
class WxtokenHelper{
private $appId;
private $appSecret;
private $cachetime = 7000; //缓存时间
public function __construct($appId, $appSecret) {
$this->appId = $appId;
$this->appSecret = $appSecret;
}
//获取AccessToken
public function getAccessToken(){
$cachekey = md5("wxapp_". $this->appId ."_accesstoken");
//$cachecon = Yii::$app->cache->d.e.l.e.t.e($cachekey);
$cachecon = Yii::$app->cache->get($cachekey);
if(!empty($cachecon)){ //如果有缓存
$jsonobj = json_decode($cachecon);
return $jsonobj->access_token;
}
$target = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=". $this->appId ."&secret=". $this->appSecret;
$result = file_get_contents($target);
if(empty($result)){
return 'failed';
}
//echo $result;die();
$jsonobj = json_decode($result);
if($jsonobj->errcode!=0){
return 'failed';
}
$cachecon = $result;
Yii::$app->cache->set($cachekey, $cachecon, $this->cachetime);
return $jsonobj->access_token;
}
//获取JsApiTicket
public function getJsApiTicket(){
$cachekey = md5("wxapp_". $this->appId ."_jsapiticket");
//$cachecon = Yii::$app->cache->d.e.l.e.t.e($cachekey);
$cachecon = Yii::$app->cache->get($cachekey);
if(!empty($cachecon)){ //如果有缓存
$jsonobj = json_decode($cachecon);
return $jsonobj->ticket;
}
$access_token = $this->getAccessToken();
$target = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=". $access_token ."&type=jsapi";
$result = file_get_contents($target);
if(empty($result)){
return 'failed';
}
//echo $result;die();
$jsonobj = json_decode($result);
if($jsonobj->errcode!=0){
return 'failed';
}
$cachecon = $result;
Yii::$app->cache->set($cachekey, $cachecon, $this->cachetime);
return $jsonobj->ticket;
}
//获取网页签名
public function getSignPackage($url) {
$jsapiTicket = $this->getJsApiTicket();
$timestamp = time();
$nonceStr = $this->createNonceStr();
// 这里参数的顺序要按照 key 值 ASCII 码升序排序
$string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$url";
$signature = sha1($string);
$signPackage = array(
"appId" => $this->appId,
"noncestr" => $nonceStr,
"timestamp" => $timestamp,
"url" => $url,
"signature" => $signature,
);
return $signPackage;
}
public function createNonceStr($length = 16) {
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$str = "";
for ($i = 0; $i < $length; $i++) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $str;
}
//POST发送数据
public function sendPostData($url, $port, $arrPostInfo){
$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); //CURL_SSLVERSION_TLSv1
}
if(!empty($port) && is_numeric($port)==true){
curl_setopt($ch, CURLOPT_PORT, $port);
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $arrPostInfo);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
//发送Json数据
public function sendJsonData($url, $port, $json){
$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); //CURL_SSLVERSION_TLSv1
}
if(!empty($port) && is_numeric($port)==true){
curl_setopt($ch, CURLOPT_PORT, $port);
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; encoding=utf-8','Content-Length: ' . strlen($json)));
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
}
?>