加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

php一个文件搞定微信jssdk配置

发布时间:2020-05-23 05:02:56 所属栏目:PHP 来源:互联网
导读:这篇文章主要为大家详细介绍了php如何利用一个文件搞定微信jssdk配置,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

php一个文件搞定微信jssdk配置:

包括缓存,包括https通讯,获取微信access_token,签名什么的都有。但是防范性编程做得比较少,商业用的话,需要完善下代码。

使用姿势

服务端

GetWX.php

$result = array("jsapi_ticket"=>$jsapi_ticket,"nonceStr"=>$nonceStr,"timestamp"=>$timestamp,"url"=>$url,"signature"=>$signature);
echo json_encode($result);
}

function getSignature($jsapi_ticket,$noncestr,$url){
$string1 = "jsapi_ticket=".$jsapi_ticket."&noncestr=".$noncestr."&timestamp=".$timestamp."&url=".$url;
$sha1 = sha1($string1);
return $sha1;
}

function getJsapi_ticket(){
$cache = new Cache();
$cache = new Cache(7000,'cache/'); //需要创建cache文件夹存储缓存文件。
//从缓存从读取键值 $key 的数据
$jsapi_ticket = $cache -> get("jsapi_ticket");
$access_token = getAccess_token();
//如果没有缓存数据
if ($jsapi_ticket == false) {
$access_token = getAccess_token();
$url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket';
$data = array('type'=>'jsapi','access_token'=>$access_token);
$header = array();
$response = json_decode(curl_https($url,$data,$header,5));
$jsapi_ticket = $response->ticket;
//写入键值 $key 的数据
$cache -> put("jsapi_ticket",$jsapi_ticket);
}
return $jsapi_ticket;
}

function getAccess_token(){
$cache = new Cache();
$cache = new Cache(7000,'cache/');
//从缓存从读取键值 $key 的数据
$access_token = $cache -> get("access_token");

//如果没有缓存数据
if ($access_token == false) {
$url = 'https://api.weixin.qq.com/cgi-bin/token';
$data = array('grant_type'=>'client_credential','appid'=>$APPID,'secret'=>$SECRET);
$header = array();

$response = json_decode(curl_https($url,5));
$access_token = $response->access_token;
//写入键值 $key 的数据
$cache -> put("access_token",$access_token);
}
return $access_token;
}

/** curl 获取 https 请求

  • @param String $url 请求的url
  • @param Array $data 要發送的數據
  • @param Array $header 请求时发送的header
  • @param int $timeout 超时时间,默认30s
    */
    function curl_https($url,$data=array(),$header=array(),$timeout=30){
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false); // 跳过证书检查
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
    curl_setopt($ch,CURLOPT_POST,true);
    curl_setopt($ch,CURLOPT_POSTFIELDS,http_build_query($data));
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,CURLOPT_TIMEOUT,$timeout);

$response = curl_exec($ch);

if($error=curl_error($ch)){
die($error);
}

curl_close($ch);

return $response;

}
?>

Cache.php 不知道哪位写的源代码~

//cache constructor,optional expiring time and cache path
public function Cache($exp_time = 3600,$path = "cache/") {
$this -> cache_expire = $exp_time;
$this -> cache_path = $path;
}

//returns the filename for the cache
private function fileName($key) {
return $this -> cache_path . md5($key);
}

//creates new cache files with the given data,$key== name of the cache,data the info/values to store
public function put($key,$data) {
$values = serialize($data);
$filename = $this -> fileName($key);
$file = fopen($filename,'w');
if ($file) {//able to create the file
fwrite($file,$values);
fclose($file);
} else
return false;
}

//returns cache for the given key
public function get($key) {
$filename = $this -> fileName($key);
if (!file_exists($filename) || !is_readable($filename)) {//can't read the cache
return false;
}
if (time() < (filemtime($filename) + $this -> cache_expire)) {//cache for the key not expired
$file = fopen($filename,"r");
// read data file
if ($file) {//able to open the file
$data = fread($file,filesize($filename));
fclose($file);
return unserialize($data);
//return the values
} else
return false;
} else
return false;
//was expired you need to create new
}

}
?>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读