想说的话
这些代码如果你是新手可能第一眼有懵,但是耐心点,就会很清楚,(高手肯定一看就会),里面有些 代码也是网上来的,微信这个东西我也是被坑了好多回。实在想不出什么说的了
PHP端代码
直接上代码 代码片
.
<?php
function curlHttp($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt($curl, CURLOPT_TIMEOUT, 500 );
curl_setopt($curl, CURLOPT_URL, $url );
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,false);
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
function comfunction(){
//appid!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!更改处1
$appid="**********";
//appsecret!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!更改处2
$appsecret="************";
//url
$url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appsecret}";
$result=curlHttp($url);
//把获取回来的json格式拆分出来
$json=json_decode($result);
//把数组中access_token字段赋值给$token
$token=$json->access_token;
//实例化redis数据库对象
$redis=new Redis();
//链接数据库
$redis->connect('127.0.0.1',6379);
//定义一个数组当做键添加进数据库
$redis_token=['token'=>$token,'expirtime'=>(time()+7000)];
//把数组当做一个值添加进数据库
$redis->hMset('token',$redis_token);
//把缓存数据添加进数据库
$redis->save();
}
//获取access_token
function getaccess_token(){
//echo $token;
//实例化redis数据库对象
$redis=new Redis();
//链接数据库
$redis->connect('127.0.0.1',6379);
//查看服务是否运行
// echo "Connection to server successfully";
$array=["token","expirtime"];
$array_token=$redis->hMget('token',$array);
//第一步判断$array_token['token']是否有值
if(!empty($array_token['token'])){
//如果有值那么判断上次获取的access_token时间戳是否超过了两个小时如果超过就刷新如果没有就调用现有的
if($array_token['expirtime']<=time()){
comfunction();
// echo"超过时间刷新的";
return $array_token['token'];
}else{
//上次获取access_token的时间没有超过或等于两个小时就用原来的access_token
//echo "没刷新的";
return $array_token['token'];
}
}else{
echo"没有时刷新的";
comfunction();
}
}
//getaccess_token();
//获取jsapi_ticket
function getjs_tik(){
$accessToken=getaccess_token();
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=$accessToken&&type=jsapi";
// 微信返回的信息
$returnData = json_decode(curlHttp($url));
//把josn数据复制给页面
// $resData['errcode'] = $returnData->errcode;
$resData = $returnData->ticket;
//$resData['expiresIn'] = $returnData ->expires_in;
// $resData['time'] = date("Y-m-d H:i",time());
return $resData;
}
//getjs_tik();
//生成随机数
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 get_jssdk(){
// 获取token
$token = getaccess_token();
// 获取ticket
$ticket = $resData['ticket']=getjs_tik();
// 该url为调用jssdk接口的url(就是哪个页面要用就写哪个页面,这个参数可以通过前台ajax提交过来)!!!!!!!!!!!!!!!!!!!!!!!!!更改处3
$url = $_POST['url'];
// 生成时间戳
$timestamp = time();
// 生成随机字符串
$nonceStr = createNoncestr();
// 这里参数的顺序要按照 key 值 ASCII 码升序排序 j -> n -> t -> u
$string = "jsapi_ticket=$ticket&noncestr=$nonceStr×tamp=$timestamp&url=$url";
$signature = sha1($string);
$signPackage = array (
"appId" => '*********',//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!更改4
"nonceStr" => $nonceStr,
"timestamp" => $timestamp,
"url" => $url,
"signature" => $signature,
"rawString" => $string,
"ticket" => $ticket,
"token" => $token
);
// 返回数据给前端
echo json_encode($signPackage);
}
//执行获取jssdk接口签名方法
get_jssdk();
//请求只需请求jssdk
//以上就是获取微信jssdk接口签名的全部了,希望你一次成功,出错也是难免的,毕竟我也被他搞了好久
?>
PS:
第一次写博客,不喜勿喷,上述代码有些也是网上找的,希望对看到这儿的你有所帮助,有不足之处还希望留言,大家一起交流。