php一个文件搞定微信jssdk配置
这篇文章主要为大家详细介绍了php如何利用一个文件搞定微信jssdk配置,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
php一个文件搞定微信jssdk配置:
包括缓存,包括https通讯,获取微信access_token,签名什么的都有。但是防范性编程做得比较少,商业用的话,需要完善下代码。
使用姿势
^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
<?php include "lib/Cache.php"; define($APPID, "……"); define($SECRET, "……") if($_GET['Type'] == "access_token"){ // echo getAccess_token(); } else if($_GET['Type'] == "jsapi_ticket"){ // echo getJsapi_ticket(); } else if($_GET['Type'] == "config"){ $jsapi_ticket = getJsapi_ticket(); $nonceStr = "x".rand(10000,100000)."x"; //随机字符串 $timestamp = time(); //时间戳 $url = $_GET['url']; $signature = getSignature($jsapi_ticket,$nonceStr, $timestamp, $url); $result = array("jsapi_ticket"=>$jsapi_ticket, "nonceStr"=>$nonceStr,"timestamp"=>$timestamp,"url"=>$url,"signature"=>$signature); echo json_encode($result); } function getSignature($jsapi_ticket,$noncestr, $timestamp, $url){ $string1 = "jsapi_ticket=".$jsapi_ticket."&noncestr=".$noncestr."×tamp=".$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, $data, $header, 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, true); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); $response = curl_exec($ch); if($error=curl_error($ch)){ die($error); } curl_close($ch); return $response; } ?>
Cache.php
不知道哪位写的源代码~
<?php class Cache { private $cache_path; //path for the cache private $cache_expire; //seconds that the cache expires //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 } } ?>
以上就是本文的全部内容,希望对大家的学习有所帮助。
相关推荐:
phpmyadmin 装配 nginx 配置 cookie 配置
以上是php一个文件搞定微信jssdk配置的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

JWT是一种基于JSON的开放标准,用于在各方之间安全地传输信息,主要用于身份验证和信息交换。1.JWT由Header、Payload和Signature三部分组成。2.JWT的工作原理包括生成JWT、验证JWT和解析Payload三个步骤。3.在PHP中使用JWT进行身份验证时,可以生成和验证JWT,并在高级用法中包含用户角色和权限信息。4.常见错误包括签名验证失败、令牌过期和Payload过大,调试技巧包括使用调试工具和日志记录。5.性能优化和最佳实践包括使用合适的签名算法、合理设置有效期、

静态绑定(static::)在PHP中实现晚期静态绑定(LSB),允许在静态上下文中引用调用类而非定义类。1)解析过程在运行时进行,2)在继承关系中向上查找调用类,3)可能带来性能开销。

PHP的魔法方法有哪些?PHP的魔法方法包括:1.\_\_construct,用于初始化对象;2.\_\_destruct,用于清理资源;3.\_\_call,处理不存在的方法调用;4.\_\_get,实现动态属性访问;5.\_\_set,实现动态属性设置。这些方法在特定情况下自动调用,提升代码的灵活性和效率。

本文提供国内安全下载欧易OKX App的详细指南。由于国内应用商店限制,建议用户通过欧易OKX官方网站下载App,或使用官网提供的二维码扫描下载。下载过程中,务必核实官网地址,检查应用权限,安装后进行安全扫描,并启用双重验证。 使用过程中,请遵守当地法律法规,使用安全网络环境,保护账户安全,警惕诈骗,理性投资。 本文仅供参考,不构成投资建议,数字资产交易风险自负。

在PHP8 中,match表达式是一种新的控制结构,用于根据表达式的值返回不同的结果。1)它类似于switch语句,但返回值而非执行语句块。2)match表达式使用严格比较(===),提升了安全性。3)它避免了switch语句中可能的break遗漏问题,增强了代码的简洁性和可读性。

公司安全软件与应用兼容性问题及排查方法许多企业为了保障内网安全,会安装安全软件。然而,安全软件有时...

本文提供2025年更新的币安虚拟货币买卖简明指南,详细讲解了在币安平台上进行虚拟货币交易的操作步骤。指南涵盖了法币购买USDT、币币交易购买其他币种(如BTC)以及卖出操作,包括市价交易和限价交易两种方式。 此外,指南还特别提示了法币交易的支付安全和网络选择等关键风险,帮助用户安全、高效地进行币安交易。 通过本文,您可以快速掌握在币安平台上买卖虚拟货币的技巧,降低交易风险。

在PHP中可以通过使用不可预测的令牌来有效防范CSRF攻击。具体方法包括:1.生成并在表单中嵌入CSRF令牌;2.在处理请求时验证令牌的有效性。
