PHP a file to take care of the micro-JSSDK Configuration Example code

Source: Internet
Author: User
PHP a file to fix JSSDK configuration:
including the cache, including HTTPS communications, get access_token, signatures and anything else. However, the preventive programming is relatively small, commercial use, the need to improve the code.
Use Posture

^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),//Open debug mode, The return value of all the API calls will be alert on the client, to see the incoming parameters, can be opened on the PC side, the parameter information will be typed through log, only on the PC side will be printed.   appId: ' ... ',//required, the unique identification of the public number   Timestamp:data.timestamp,//required, time stamp to generate the signature   nonceStr:data.nonceStr,//required, Generate a signed random string   signature:data.signature,//required, signature, see Appendix 1   jsapilist: ["getLocation"]//required, required to use the JS interface list, all JS interface list See Appendix 2  }); }}) Wx.ready (function () {wx.getlocation ({  type: ' WGS84 '),//default is WGS84 GPS coordinates, if you want to return the coordinates of Mars directly to Openlocation, Can pass in ' gcj02 '  success:function (res) {   var latitude = res.latitude;//latitude, floating-point number, range from -90   var longitude = Res. longitude; Longitude, floating point number, RANGE 180 ~-180.   Plus2.storage.setItem ("latitude", latitude);   Plus2.storage.setItem ("Longitude", longitude);  } });});

Service side
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 "; Random string $timestamp = time ();  Timestamp $url = $_get[' url '];  $signature = Getsignature ($jsapi _ticket, $nonceStr, $timestamp, $url); $result = Array ("Jsapi_ticket" + $jsapi _ticket, "noncestr" + $nonceStr, "timestamp" and "$timestamp", "url" =>$  URL, "signature" = $signature); echo Json_encode ($result); } function Getsignature ($jsapi _ticket, $noncestr, $timestamp, $url) {$string 1 = "jsapi_ticket=". $jsapi _ticket. " &noncestr= ". $noncestr." &timestamp= ". $timestamp."  &url= ". $url;  $sha 1 = SHA1 ($string 1); return $sha 1;  } function Getjsapi_ticket () {$cache = new cache (); $cache = new Cache (7000, ' cache/');  You need to create a cache folder to store the cached files. Data from the cache $key from the Read key value $jsapi _ticKet = $cache, get ("Jsapi_ticket");  $access _token = Getaccess_token ();   If there is no cached data 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;  Write the key value $key the data $cache, put ("Jsapi_ticket", $jsapi _ticket); } return $jsapi _ticket;  } function Getaccess_token () {$cache = new cache ();  $cache = new Cache (7000, ' cache/');  The data $key from the cache reading the key value $access _token = $cache, Get ("Access_token");    If there is no cached data if ($access _token = = False) {$url = ' https://api.weixin.qq.com/cgi-bin/token ';    $data = Array (' grant_type ' = ' client_credential ', ' appid ' and ' = ' $APPID, ' secret ' = + $SECRET);   $header = Array ();    $response = Json_decode (Curl_https ($url, $data, $header, 5));  $access _token = $response->access_token; Write the key value $key the data $cache, put ("Access_token", $access _token); } return $access _token; }/** Curl Gets the HTTPS request * @param String $url The requested URL * @param array $data the number to send @param array $header the header sent when the request * @ param int $timeout Time-out, default 30s */function Curl_https ($url, $data =array (), $header =array (), $timeout =30) {$ch = curl_in   It (); curl_setopt ($ch, Curlopt_ssl_verifypeer, false);   Skip certificate Check 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
does not know which source is written-

<?phpclass Cache {Private $cache _path;//path for the cache private $cache _expire;//seconds the cache expires// Cache constructor, optional expiring time and cache path Public function cache ($exp _time = 3600, $path = "cache/") {$thi  S--cache_expire = $exp _time; $this-Cache_path = $path; }//returns the filename for the cache private function filename ($key) {return $this, Cache_path. MD5 ($key);//c Reates new cache files with the given data, $key = = Name of the cache, data the info/values to store public function put ($k  EY, $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 is 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-need to create new}}?>

The above is a PHP file to fix JSSDK configuration of the instance code in detail, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.