微信分享

/**
* 获取签名 微信分享
*/
function getSignPackage() {
$AppID=‘wx0492be96499b8b5c‘;
$AppSecret =‘8218182e99f47db899c884b690f2d142‘;

$protocol = (!empty($_SERVER[‘HTTPS‘]) && $_SERVER[‘HTTPS‘] !== ‘off‘ || $_SERVER[‘SERVER_PORT‘] == 443) ? "https://" : "http://";
$url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; //获取服务器地址 获取当前url路径地址

$jsapiTicket = $this->getJsApiTicket($AppID,$AppSecret);

$timestamp = time();
$nonceStr = $this->createNonceStr(); //获取随机数

// 这里参数的顺序要按照 key 值 ASCII 码升序排序
$string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";
$signature = sha1($string); //签名 二进制或者是16进制 加密

$signPackage = array(
"appId" => $AppID,
"nonceStr" => $nonceStr,
"timestamp" => $timestamp,
"url" => $url,
"signature" => $signature,

);
return $signPackage;
}

function getJsApiTicket($appid,$appsecret) { //获取ticket
$accessToken = $this->getAccessToken($appid,$appsecret);
$access_token = json_decode($accessToken);
$access_token = $access_token->access_token;
$jsapi_ticket_data = db(‘jsapi_ticket‘)->find();
if(empty($jsapi_ticket_data)){
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$access_token";
$res = json_decode($this->httpGet($url));
$ticket = $res->ticket;
db(‘jsapi_ticket‘)->insert([‘jsapi_ticket‘=>$ticket,‘time‘=>time()]);
}else{
$time = time()-$jsapi_ticket_data[‘time‘];
if($time>7100){
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$access_token";
$res = json_decode($this->httpGet($url));
$ticket = $res->ticket;
db(‘jsapi_ticket‘)->where([‘id‘=>$jsapi_ticket_data[‘id‘]])->update([‘jsapi_ticket‘=>$ticket]);

}else{
$ticket = $jsapi_ticket_data[‘jsapi_ticket‘];
}
}

$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$access_token";
$res = json_decode($this->httpGet($url));
$ticket = $res->ticket;
return $ticket;
}

function getAccessToken($appid,$appsecret) { //获取access_token
$access_token_data = db(‘access_token‘)->find();

if(empty($access_token_data)){
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";
$res = json_decode($this->httpGet($url));
$access_token = $res->access_token;
db(‘access_token‘)->insert([‘access_token‘=>$access_token,‘time‘=>time()]);
return json_encode(array("access_token"=>$access_token));
}else{
$time = time()-$access_token_data[‘time‘];
if($time>7100){
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";
$res = json_decode($this->httpGet($url));
// dump($res);exit;
$access_token = $res->access_token;
db(‘access_token‘)->where([‘id‘=>$access_token_data[‘id‘]])->update([‘access_token‘=>$access_token,‘time‘=>time()]);
return json_encode(array("access_token"=>$access_token));
}else{
$access_token = $access_token_data[‘access_token‘];
return json_encode(array("access_token"=>$access_token));
}
}
}

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;
}

function httpGet($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
// 为保证第三方服务器与微信服务器之间数据传输的安全性,所有微信接口采用https方式调用,必须使用下面2行代码打开ssl安全校验。
// 如果在部署过程中代码在此处验证失败,请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_URL, $url);
$res = curl_exec($curl);
curl_close($curl);
return $res;
}

微信分享

上一篇:Java与微信相关


下一篇:mpvue搭建微信小程序