文档地址:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115&token=&lang=zh_CN
开发环境:
Thinkphp3.2.3beta+onethink、yershop
新浪SAE服务器,点此注册
认证微信服务号
jssdk.php——因sae的不可写环境,故需要将accesstoken和JsApiTicket存入数据库。
数据库字段:id(int 3),accesstoken(string 255),lasttime(int 10)
class JSSDK {
private $appId;
private $appSecret;
public function __construct($appId, $appSecret) {
$this->appId = $appId;
$this->appSecret = $appSecret;
}
public function getSignPackage() {
$jsapiTicket = $this->getJsApiTicket();
// 注意 URL 一定要动态获取,不能 hardcode.
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$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,
"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() {
$accessToken = $this -> getAccesstoken();
$lastAT = M('accesstoken')->find(2);//1:token;2:ticket
if(!$lastAT){
M('accesstoken') -> add(array('id'=>2,'accesstoken'=>'null','lasttime'=>0));
$lastAT['lasttime'] = 0;
}
$ticket = $lastAT['accesstoken'];
$now = NOW_TIME;
$timecha = $now - $lastAT['lasttime'];
if($timecha >= 7200){
// $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?' . http_build_query ( $param );
// $content = file_get_contents ( $url );
// $content = json_decode ( $content, true );
// 如果是企业号用以下 URL 获取 ticket
// $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken";
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=".$accessToken;
$res = json_decode($this->httpGet($url));
$ticket = $res->ticket;
$data['accesstoken'] = $ticket;
$data['lasttime'] = NOW_TIME;
M('accesstoken')->where(array('id'=>2))->save($data);
}
return $ticket;
}
//sae无法写入操作,改从数据库获取
private function getAccessToken() {
$param['grant_type'] = 'client_credential';
$param['appid'] = C("APPID");
$param['secret'] = C("SECRET");
$lastAT = M('accesstoken')->find(1);
$accesstoken = $lastAT['accesstoken'];
if(!$lastAT){
M('accesstoken') -> add(array('id'=>1,'accesstoken'=>'null','lasttime'=>0));
$lastAT['lasttime'] = 0;
}
$now = NOW_TIME;
$timecha = $now - $lastAT['lasttime'];
if($timecha >= 7200){
$url = 'https://api.weixin.qq.com/cgi-bin/token?' . http_build_query ( $param );
$content = file_get_contents ( $url );
$content = json_decode ( $content, true );
$data['accesstoken'] = $content['access_token'];
$data['lasttime'] = NOW_TIME;
M('accesstoken')->where(array('id'=>1))->save($data);
$accesstoken = $content['access_token'];
}
$lastAT = M('accesstoken')->find(1);
return $accesstoken;
}
private 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, true);
curl_setopt($curl, CURLOPT_URL, $url);
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
private function get_php_file($filename) {
return trim(substr(file_get_contents($filename), 15));
}
private function set_php_file($filename, $content) {
$fp = fopen($filename, "w");
fwrite($fp, "" . $content);
fclose($fp);
}
}
如果你的服务器是可写环境,可以使用官方提供的包。
可以使用include将此文件包含在需要页面
include('jssdk.php');
$jssdk = new JSSDK(C('APPID'), C('SECRET'));
if(isWeixinBrowser()){
$signPackage = $jssdk->GetSignPackage();
}
?>
出处:blog.csdn.net/afanxingzhou
Q724122005
项目示例:www.kateqi.cn
承接中小型项目开发