這篇文章主要為大家詳細介紹了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中文網其他相關文章!

PHP和Python各有優勢,選擇應基於項目需求。 1.PHP適合web開發,語法簡單,執行效率高。 2.Python適用於數據科學和機器學習,語法簡潔,庫豐富。

PHP不是在消亡,而是在不斷適應和進化。 1)PHP從1994年起經歷多次版本迭代,適應新技術趨勢。 2)目前廣泛應用於電子商務、內容管理系統等領域。 3)PHP8引入JIT編譯器等功能,提升性能和現代化。 4)使用OPcache和遵循PSR-12標準可優化性能和代碼質量。

PHP的未來將通過適應新技術趨勢和引入創新特性來實現:1)適應云計算、容器化和微服務架構,支持Docker和Kubernetes;2)引入JIT編譯器和枚舉類型,提升性能和數據處理效率;3)持續優化性能和推廣最佳實踐。

在PHP中,trait適用於需要方法復用但不適合使用繼承的情況。 1)trait允許在類中復用方法,避免多重繼承複雜性。 2)使用trait時需注意方法衝突,可通過insteadof和as關鍵字解決。 3)應避免過度使用trait,保持其單一職責,以優化性能和提高代碼可維護性。

依賴注入容器(DIC)是一種管理和提供對象依賴關係的工具,用於PHP項目中。 DIC的主要好處包括:1.解耦,使組件獨立,代碼易維護和測試;2.靈活性,易替換或修改依賴關係;3.可測試性,方便注入mock對象進行單元測試。

SplFixedArray在PHP中是一種固定大小的數組,適用於需要高性能和低內存使用量的場景。 1)它在創建時需指定大小,避免動態調整帶來的開銷。 2)基於C語言數組,直接操作內存,訪問速度快。 3)適合大規模數據處理和內存敏感環境,但需謹慎使用,因其大小固定。

PHP通過$\_FILES變量處理文件上傳,確保安全性的方法包括:1.檢查上傳錯誤,2.驗證文件類型和大小,3.防止文件覆蓋,4.移動文件到永久存儲位置。

JavaScript中處理空值可以使用NullCoalescingOperator(??)和NullCoalescingAssignmentOperator(??=)。 1.??返回第一個非null或非undefined的操作數。 2.??=將變量賦值為右操作數的值,但前提是該變量為null或undefined。這些操作符簡化了代碼邏輯,提高了可讀性和性能。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

禪工作室 13.0.1
強大的PHP整合開發環境

Atom編輯器mac版下載
最受歡迎的的開源編輯器

Dreamweaver CS6
視覺化網頁開發工具

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能