<pre id="bbfd9"><del id="bbfd9"><dfn id="bbfd9"></dfn></del></pre>

          <ruby id="bbfd9"></ruby><p id="bbfd9"><mark id="bbfd9"></mark></p>

          <p id="bbfd9"></p>

          <p id="bbfd9"><cite id="bbfd9"></cite></p>

            <th id="bbfd9"><form id="bbfd9"><dl id="bbfd9"></dl></form></th>

            <p id="bbfd9"><cite id="bbfd9"></cite></p><p id="bbfd9"></p>
            <p id="bbfd9"><cite id="bbfd9"><progress id="bbfd9"></progress></cite></p>

            PHP微信jssdk配置

            時間:2025-12-22 07:01:05 php語言

            PHP微信jssdk配置

              PHP微信jssdk配置包括緩存,包括https通訊,獲取微信access_token,簽名什么的都有。但是防范性編程做得比較少,商業用的話,需要完善下代碼。以下是小編為大家搜索整理的PHP微信jssdk配置,希望能給大家帶來幫助!更多經常內容請及時關注我們應屆畢業生考試網!

              用姿勢

              ^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, /pic/p>

              appId: '……', /pic/p>

              timestamp: data.timestamp, /pic/p>

              nonceStr: data.nonceStr, /pic/p>

              signature: data.signature, /pic/p>

              jsApiList: ["getLocation"] /pic/p>

              });

              }

              })

              wx.ready(function() {

              wx.getLocation({

              type: 'wgs84', /pic/p>

              success: function(res) {

              var latitude = res.latitude; /pic/p>

              var longitude = res.longitude; /pic/p>

              plus2.storage.setItem("latitude", latitude);

              plus2.storage.setItem("longitude", longitude);

              }

              });

              });

              服務端

              GetWX.php

              include "lib/Cache.php";

              define($APPID, "……");

              define($SECRET, "……")

              if($_GET['Type'] == "access_token"){

              /pic/p>

              }

              else if($_GET['Type'] == "jsapi_ticket"){

              /pic/p>

              }

              else if($_GET['Type'] == "config"){

              $jsapi_ticket = getJsapi_ticket();

              $nonceStr = "x".rand(10000,100000)."x"; /pic/p>

              $timestamp = time(); /pic/p>

              $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/'); /pic/p>

              /pic/p>

              $jsapi_ticket = $cache -> get("jsapi_ticket");

              $access_token = getAccess_token();

              /pic/p>

              if ($jsapi_ticket == false) {

              $access_token = getAccess_token();

              $url = '/pic/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;

              /pic/p>

              $cache -> put("jsapi_ticket", $jsapi_ticket);

              }

              return $jsapi_ticket;

              }

              function getAccess_token(){

              $cache = new Cache();

              $cache = new Cache(7000, 'cache/');

              /pic/p>

              $access_token = $cache -> get("access_token");

              /pic/p>

              if ($access_token == false) {

              $url = '/pic/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;

              /pic/p>

              $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); /pic/p>

              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

              不知道哪位寫的源代碼~

              class Cache {

              private $cache_path;

              /pic/p>

              private $cache_expire;

              /pic/p>

              /pic/p>

              public function Cache($exp_time = 3600, $path = "cache/") {

              $this -> cache_expire = $exp_time;

              $this -> cache_path = $path;

              }

              /pic/p>

              private function fileName($key) {

              return $this -> cache_path . md5($key);

              }

              /pic/values to store

              public function put($key, $data) {

              $values = serialize($data);

              $filename = $this -> fileName($key);

              $file = fopen($filename, 'w');

              if ($file) {/pic/p>

              fwrite($file, $values);

              fclose($file);

              } else

              return false;

              }

              /pic/p>

              public function get($key) {

              $filename = $this -> fileName($key);

              if (!file_exists($filename) || !is_readable($filename)) {/pic/p>

              return false;

              }

              if (time() < (filemtime($filename) + $this -> cache_expire)) {/pic/p>

              $file = fopen($filename, "r");

              /pic/p>

              if ($file) {/pic/p>

              $data = fread($file, filesize($filename));

              fclose($file);

              return unserialize($data);

              /pic/p>

              } else

              return false;

              } else

              return false;

              /pic/p>

              }

              }

              ?>

            【PHP微信jssdk配置】相關文章:

            php一個文件搞定微信jssdk配置02-27

            php學習之php配置03-11

            PHP基礎配置08-05

            PHP安裝與配置03-28

            如何配置php環境05-12

            PHP socket的配置及實例03-21

            php環境怎么配置03-25

            如何配置PHP開發環境04-23

            PHP環境搭建與配置的方法01-25

                    <pre id="bbfd9"><del id="bbfd9"><dfn id="bbfd9"></dfn></del></pre>

                    <ruby id="bbfd9"></ruby><p id="bbfd9"><mark id="bbfd9"></mark></p>

                    <p id="bbfd9"></p>

                    <p id="bbfd9"><cite id="bbfd9"></cite></p>

                      <th id="bbfd9"><form id="bbfd9"><dl id="bbfd9"></dl></form></th>

                      <p id="bbfd9"><cite id="bbfd9"></cite></p><p id="bbfd9"></p>
                      <p id="bbfd9"><cite id="bbfd9"><progress id="bbfd9"></progress></cite></p>
                      飘沙影院