phpÒ»¸öÎļþ¸ã¶¨Î¢ÐÅjssdkÅäÖÃ

5年以前  |  阅读数:972 次  |  编程语言:PHP 

phpÒ»¸oÎļþ¸a¶¨Î¢ÐÅjssdkÅaÖãº

°uÀ¨»º´ae£¬°uÀ¨httpsͨѶ£¬»ñȡ΢ÐÅaccess_token£¬Ç(C)ÃuʲôµÄ¶¼ÓС£µ«ÊÇÀ¶ÐÔ±a³Ì×oµÃ±È½ÏÉÙ£¬ÉÌÒµÓõĻ°£¬ÐeÒªÍeÉÆÏ´uÂe¡£

ʹÓÃ×ËÊÆ


    ^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, // ¿ªÆoµ÷ÊÔģʽ,µ÷ÓõÄËuÓÐapiµÄ*µ»ØÖµ»aÔÚ¿Í»§¶Ëalert³oÀ´£¬ÈoÒª²e¿´´«ÈeµÄ²ÎÊý£¬¿ÉÒÔÔÚpc¶Ë´o¿ª£¬²ÎÊýÐÅÏ¢»aͨ¹ýlog´o³o£¬½oÔÚpc¶Ëʱ²Å»a´oÓ¡¡£
       appId: '¡­¡­', // ±ØÌi£¬¹«ÖںŵÄΨһ±eʶ
       timestamp: data.timestamp, // ±ØÌi£¬Éu³ÉÇ(C)ÃuµÄʱ¼a´Á
       nonceStr: data.nonceStr, // ±ØÌi£¬Éu³ÉÇ(C)ÃuµÄËae»u´®
       signature: data.signature, // ±ØÌi£¬Ç(C)Ãu£¬¼u¸½Â¼1
       jsApiList: ["getLocation"] // ±ØÌi£¬ÐeҪʹÓõÄJS½Ó¿ÚÁбi£¬ËuÓÐJS½Ó¿ÚÁбi¼u¸½Â¼2
      });
     }
    })
    wx.ready(function() {
     wx.getLocation({
      type: 'wgs84', // ĬÈÏΪwgs84µÄgps×ø±e£¬Èç¹uÒª*µ»ØÖ±½Ó¸øopenLocationÓõĻðÐÇ×ø±e£¬¿É´«Èe'gcj02'
      success: function(res) {
       var latitude = res.latitude; // γ¶È£¬¸¡µaÊý£¬*¶Î§Îª90 ~ -90
       var longitude = res.longitude; // ¾­¶È£¬¸¡µaÊý£¬*¶Î§Îª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"; //Ëae»u×Ö*u´®
      $timestamp = time(); //ʱ¼a´Á
      $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/'); //ÐeÒª´´½¨cacheÎļþ¼Ð´ae´¢»º´aeÎļþ¡£
      //´Ó»º´ae´Ó¶ÁÈ¡¼uÖµ $key µÄÊý¾Ý
      $jsapi_ticket = $cache -> get("jsapi_ticket");
      $access_token = getAccess_token();
      //Èç¹uûÓлº´aeÊý¾Ý
      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;
       //дÈe¼uÖµ $key µÄÊý¾Ý
       $cache -> put("jsapi_ticket", $jsapi_ticket);
      }
      return $jsapi_ticket;
     }

     function getAccess_token(){
      $cache = new Cache();
      $cache = new Cache(7000, 'cache/');
      //´Ó»º´ae´Ó¶ÁÈ¡¼uÖµ $key µÄÊý¾Ý
      $access_token = $cache -> get("access_token");

      //Èç¹uûÓлº´aeÊý¾Ý
      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;
       //дÈe¼uÖµ $key µÄÊý¾Ý
       $cache -> put("access_token", $access_token);
      }
      return $access_token;
     }

     /** curl »ñÈ¡ https ÇeÇo 
     * @param String $url ÇeÇoµÄurl 
     * @param Array $data Òª°lË͵Ä"µ"þ 
     * @param Array $header ÇeÇoʱ*¢Ë͵Äheader 
     * @param int $timeout ³¬Ê±Ê±¼a£¬Ä¬ÈÏ30s 
     */ 
     function curl_https($url, $data=array(), $header=array(), $timeout=30){ 
      $ch = curl_init(); 
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Ìø¹ýÖ¤Êe¼i²e 
      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
²»ÖªµÀÄÄλдµÄÔ´´uÂe~


    <?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
     }

    }
    ?>

ÒÔÉϾÍÊDZ¾ÎĵÄÈ«²¿ÄÚÈÝ£¬Ï£Íu¶Ô´o¼ÒµÄѧϰÓÐËu°iÖu£¬Ò²Ï£Íu´o¼Ò¶a¶aÖ§³Ö½Å±¾Ö®¼Ò¡£

 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例