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

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

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

使用姿势

rush:PHP;"> ^ajax(Common.ServerUrl + "GetWX.PHP",{ data: { Type: "config",url: location.href.split('#')[0] },dataType: 'json',type: 'get',timeout: 5000,success: function(data) { wx.config({ debug: true,// 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。 appId: '……',// 必填,公众号的唯一标识 timestamp: data.timestamp,// 必填,生成签名的时间戳 nonceStr: data.nonceStr,// 必填,生成签名的随机串 signature: data.signature,// 必填,签名,见附录1 jsApiList: ["getLocation"] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2 }); } }) wx.ready(function() { wx.getLocation({ type: 'wgs84',// 认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02' success: function(res) { var latitude = res.latitude; // 纬度,浮点数,范围为90 ~ -90 var longitude = res.longitude; // 经度,浮点数,范围为180 ~ -180。 plus2.storage.setItem("latitude",latitude); plus2.storage.setItem("longitude",longitude); } }); });

服务端

GetWX.PHP

rush: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 不知道哪位写的源代码~

rush: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
}

}
?>

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


服务器优化必备:深入了解PHP8底层开发原理
Golang的网络编程:如何快速构建高性能的网络应用?
Golang和其他编程语言的对比:为什么它的开发效率更高?
PHP8底层开发原理揭秘:如何利用新特性创建出色的Web应用
将字符重新排列以形成回文(如果可能)在C++中
掌握PHP8底层开发原理和新特性:创建高效可扩展的应用程序
服务器性能优化必学:掌握PHP8底层开发原理
PHP8新特性和底层开发原理详解:优化应用性能的终极指南
将 C/C++ 代码转换为汇编语言
深入研究PHP8底层开发原理:创建高效可扩展的应用程序
C++程序查找法向量和迹
PHP8底层开发原理实战指南:提升服务器效能
重排数组,使得当 i 为偶数时,arr[i] >= arr[j],当 i 为奇数时,arr[i] <= arr[j],其中 j < i,使用 C++ 语言实现
Golang的垃圾回收:为什么它可以减少开发人员的负担?
C++程序:将一个数组的所有元素复制到另一个数组中
Golang:构建智能系统的基石
为什么AI开发者应该关注Golang?
在C和C++中,逗号(comma)的用法是用来分隔表达式或语句
PHP8底层开发原理解析及新特性应用实例
利用PHP8底层开发原理解析新特性:如何构建出色的Web应用