365体育备用网址报道14.1.4sdk实现_365体育备用网址官网资讯

依据上述接口介绍及定义,将微信卡券的PHP SDK实现如下:

  1 <?php
  2 
  3 /*
  4     方倍工作室 http://www.fangbei.org/
  5     CopyRight 2014 All Rights Reserved
  6 */
  7 define('APPID',         "wx1b7559b818e3c23e"); 
  8 define('APPSECRET',     "wx1b7559b818e3c23ewx1b7559b818e3c23e");
  9 
 10 class class_weixin
 11 {
 12     var $appid = APPID;
 13     var $appsecret = APPSECRET;
 14 
 15     //构造函数,猎取Access Token
 16     public function __construct($appid = NULL, $appsecret = NULL)
 17     {
 18         if($appid && $appsecret){
 19             $this->appid = $appid;
 20             $this->appsecret = $appsecret;
 21         }
 22         $res = file_get_contents('access_token.json');
 23         $result = json_decode($res, true);
 24         $this->expires_time = $result["expires_time"];
 25         $this->access_token = $result["access_token"];
 26 
 27         if (time() > ($this->expires_time + 3600)){
 28             $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appid."&secret=".$this->appsecret;
 29             $res = $this->http_request($url);
 30             $result = json_decode($res, true);
 31             $this->access_token = $result["access_token"];
 32             $this->expires_time = time();
 33             file_put_contents('access_token.json', '{"access_token": "'.$this->access_token.'", "expires_time": '.$this->expires_time.'}');
 34         }
 35     }
 36 
 37     //生成长度16的随机字符串
 38     public function createNonceStr($length = 16) {
 39         $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
 40         $str = "";
 41         for ($i = 0; $i < $length; $i++) {
 42             $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
 43         }
 44         return $str;
 45     }
 46     
 47     //获得微信卡券api_ticket
 48     public function getCardApiTicket()
 49     {
 50         $res = file_get_contents('cardapi_ticket.json');
 51         $result = json_decode($res, true);
 52         $this->cardapi_ticket = $result["cardapi_ticket"];
 53         $this->cardapi_expire = $result["cardapi_expire"];
 54         if (time() > ($this->cardapi_expire + 3600)){
 55             $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=wx_card&access_token=".$this->access_token;
 56             $res = $this->http_request($url);
 57             $result = json_decode($res, true);
 58             $this->cardapi_ticket = $result["ticket"];
 59             $this->cardapi_expire = time();
 60             file_put_contents('cardapi_ticket.json', '{"cardapi_ticket": "'.$this->cardapi_ticket.'", "cardapi_expire": '.$this->cardapi_expire.'}');
 61         }
 62         return $this->cardapi_ticket;
 63     }
 64     
 65     //cardSign卡券签名
 66     public function get_cardsign($bizObj)
 67     {
 68         //字典序排序
 69         asort($bizObj);
 70         //URL键值对拼成字符串
 71         $buff = "";
 72         foreach ($bizObj as $k => $v){
 73             $buff .= $v;
 74         }
 75         //sha1签名
 76         return sha1($buff);
 77     }
 78     
 79     //获得JS API的ticket
 80     private function getJsApiTicket() 
 81     {
 82         $res = file_get_contents('jsapi_ticket.json');
 83         $result = json_decode($res, true);
 84         $this->jsapi_ticket = $result["jsapi_ticket"];
 85         $this->jsapi_expire = $result["jsapi_expire"];
 86 
 87         if (time() > ($this->jsapi_expire + 3600)){
 88             $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=".$this->access_token;
 89             $res = $this->http_request($url);
 90             $result = json_decode($res, true);
 91             $this->jsapi_ticket = $result["ticket"];
 92             $this->jsapi_expire = time();
 93             file_put_contents('jsapi_ticket.json', '{"jsapi_ticket": "'.$this->jsapi_ticket.'", "jsapi_expire": '.$this->jsapi_expire.'}');
 94         }
 95         return $this->jsapi_ticket;
 96     }
 97 
 98     //获得签名包
 99     public function getSignPackage() {
100         $jsapiTicket = $this->getJsApiTicket();
101         $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
102         $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
103         $timestamp = time();
104         $nonceStr = $this->createNonceStr();
105         $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";
106         $signature = sha1($string);
107         $signPackage = array(
108                             "appId"     => $this->appid,
109                             "nonceStr"  => $nonceStr,
110                             "timestamp" => $timestamp,
111                             "url"       => $url,
112                             "signature" => $signature,
113                             "rawString" => $string
114                             );
115         return $signPackage;
116     }
117     
118     //HTTP要求(撑腰HTTP/HTTPS,撑腰GET/POST)
119     protected function http_request($url, $data = null)
120     {
121         $curl = curl_init();
122         curl_setopt($curl, CURLOPT_URL, $url);
123         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
124         curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
125         if (!empty($data)){
126             curl_setopt($curl, CURLOPT_POST, 1);
127             curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
128         }
129         curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
130         $output = curl_exec($curl);
131         curl_close($curl);
132         return $output;
133     }
134 }

上述代码简要解读如下:
第7行~第8行:定义开拓者参数
第15行~第35行:定义构造函数,在构造函数中同时初始化获得Access Token
第37行~第45行:用于生成默认长度16的随机字符串
第47行~第63行:获得微信卡券api_ticket,并使用文件进行缓存。
第65行~第77行:定义cardSign卡券签名函数。
第79行~第96行:获得JS API的ticket,并使用文件进行缓存。
第98行~第116行:获得JS SDK的签名。
第118行~第133行:定义HTTP要求函数。
上述SDK包括了JS API的ticket的猎取,也包括了卡券API的ticket的猎取。

------分隔线----------------------------

公布评论 (72人察看0条评论)
请自觉遵守互联网相关的政策法规,严禁公布色情、暴力、反动的言论。
用户名: 验证码: 点击我更换在线娱乐城 官网
最新评论