根据上述接口的介绍及定义,将微信卡券的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×tamp=$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" => $string114 );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的获取。