微信分享s d k

作者: adm 分类: php 发布时间: 2023-04-09

php

appId = $appId;
$this->appSecret = $appSecret;
}

public function getSignPackage($url) {
$jsapiTicket = $this->getJsApiTicket();
//$url = “http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]”;
$timestamp = time();
$nonceStr = $this->createNonceStr();

// 这里参数的顺序要按照 key 值 ASCII 码升序排序
$string = “jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url”;

$signature = sha1($string);

$signPackage = array(
“appId” => $this->appId,
“nonceStr” => $nonceStr,
“timestamp” => $timestamp,
“url” => $url,
“signature” => $signature,
“rawString” => $string
);
return $signPackage;
}

private 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; } private function getJsApiTicket() { $data = Cache::get("wx_jsapi_ticket"); if ($data) { $ticket = $data; } else { $accessToken = $this->getAccessToken();
$url = “https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken”;
$res = json_decode($this->httpGet($url),true);
$ticket = $res[‘ticket’];
if ($ticket) {
Cache::set(“wx_jsapi_ticket”, $ticket, 7000);
}
}

return $ticket;
}

private function getAccessToken() {
$data = Cache::get(“wx_access_token”);
if ($data) {
$access_token = $data;
} else {
$url = “https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret”;
$res = json_decode($this->httpGet($url),true);
$access_token = $res[‘access_token’];
if ($access_token) {
Cache::set(“wx_access_token”, $access_token, 7000);
}
}
return $access_token;
}

private function httpGet($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
curl_setopt($curl, CURLOPT_URL, $url);

$res = curl_exec($curl);
curl_close($curl);

return $res;
}
}

function sign(){
$jssdk = new JSSDK($appid, $secret);
//返回签名基本信息
$url=input(“url”);
$signPackage = $jssdk->getSignPackage($url);
return msg(1,”, $signPackage);
}

Javascript Demo
//页面接受PHP传过来的签名参数 $signPackage