讀古今文學網 > 微信公眾平台開發:從零基礎到ThinkPHP5高性能框架實踐 > 14.1.4 SDK實現 >

14.1.4 SDK實現

根據上述接口的介紹及定義,將微信卡券的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->ac
        cess_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=js
                api&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=$ti
            mestamp&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的獲取。