国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > PHP > 正文

php一個(gè)文件搞定微信jssdk配置

2020-03-22 18:40:29
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
這篇文章主要為大家詳細(xì)介紹了php如何利用一個(gè)文件搞定微信jssdk配置,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

php一個(gè)文件搞定微信jssdk配置:

包括緩存,包括https通訊,獲取微信access_token,簽名什么的都有。但是防范性編程做得比較少,商業(yè)用的話,需要完善下代碼。

使用姿勢(shì)


^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, // 開啟調(diào)試模式,調(diào)用的所有api的返回值會(huì)在客戶端alert出來(lái),若要查看傳入的參數(shù),可以在pc端打開,參數(shù)信息會(huì)通過(guò)log打出,僅在pc端時(shí)才會(huì)打印。   appId: '……', // 必填,公眾號(hào)的唯一標(biāo)識(shí)   timestamp: data.timestamp, // 必填,生成簽名的時(shí)間戳   nonceStr: data.nonceStr, // 必填,生成簽名的隨機(jī)串   signature: data.signature, // 必填,簽名,見附錄1   jsApiList: ["getLocation"] // 必填,需要使用的JS接口列表,所有JS接口列表見附錄2  }); }})wx.ready(function() { wx.getLocation({  type: 'wgs84', // 默認(rèn)為wgs84的gps坐標(biāo),如果要返回直接給openLocation用的火星坐標(biāo),可傳入'gcj02'  success: function(res) {   var latitude = res.latitude; // 緯度,浮點(diǎn)數(shù),范圍為90 ~ -90   var longitude = res.longitude; // 經(jīng)度,浮點(diǎn)數(shù),范圍為180 ~ -180。   plus2.storage.setItem("latitude", latitude);   plus2.storage.setItem("longitude", longitude);  } });});


服務(wù)端

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"; //隨機(jī)字符串  $timestamp = time(); //時(shí)間戳  $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."&timestamp=".$timestamp."&url=".$url;  $sha1 = sha1($string1);  return $sha1; } function getJsapi_ticket(){  $cache = new Cache();  $cache = new Cache(7000, 'cache/'); //需要?jiǎng)?chuàng)建cache文件夾存儲(chǔ)緩存文件。  //從緩存從讀取鍵值 $key 的數(shù)據(jù)  $jsapi_ticket = $cache -> get("jsapi_ticket");  $access_token = getAccess_token();  //如果沒有緩存數(shù)據(jù)  if ($jsapi_ticket == false) {   $access_token = getAccess_token();   $url = 'http://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 的數(shù)據(jù)   $cache -> put("jsapi_ticket", $jsapi_ticket);  }  return $jsapi_ticket; } function getAccess_token(){  $cache = new Cache();  $cache = new Cache(7000, 'cache/');  //從緩存從讀取鍵值 $key 的數(shù)據(jù)  $access_token = $cache -> get("access_token");  //如果沒有緩存數(shù)據(jù)  if ($access_token == false) {   $url = 'http://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 的數(shù)據(jù)   $cache -> put("access_token", $access_token);  }  return $access_token; } /** curl 獲取 https 請(qǐng)求  * @param String $url 請(qǐng)求的url  * @param Array $data 要發(fā)送的數(shù)據(jù)  * @param Array $header 請(qǐng)求時(shí)發(fā)送的header  * @param int $timeout 超時(shí)時(shí)間,默認(rèn)30s  */  function curl_https($url, $data=array(), $header=array(), $timeout=30){   $ch = curl_init();   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳過(guò)證書檢查   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
不知道哪位寫的源代碼~


<?phphtml' target='_blank'>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 }}?>


以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。


相關(guān)推薦:

php一個(gè)文件搞定微信jssdk配置的實(shí)例代碼詳解

phpmyadmin 裝配 nginx 配置 cookie 配置

php環(huán)境配置及調(diào)試配置的方法


以上就是php一個(gè)文件搞定微信jssdk配置的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注 其它相關(guān)文章!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 曲水县| 平乡县| 锡林郭勒盟| 黔江区| 财经| 县级市| 育儿| 简阳市| 玉龙| 油尖旺区| 铜鼓县| 吴堡县| 伊吾县| 河北区| 太白县| 雷山县| 巴塘县| 临武县| 广元市| 赫章县| 黔江区| 灵石县| 旬阳县| 三江| 福贡县| 新晃| 东城区| 新乡县| 府谷县| 怀集县| 凯里市| 东乌珠穆沁旗| 扎兰屯市| 嫩江县| 丰城市| 平谷区| 绥化市| 昆山市| 绥宁县| 华蓥市| 吉隆县|