在正式講解開發內容之前,需要先將常用功能函數寫入類中,以便在後面的章節中調用。以下是方倍工作室開發的用於微信小店的SDK代碼。
1 <?php 2 3 /* 4 方倍工作室 5 CopyRight 2014 All Rights Reserved 6 */ 7 require_once('config.php'); // 引用配置 8 9 class class_weixin 10 { 11 var $appid = APPID; 12 var $appsecret = APPSECRET; 13 14 // 構造函數,獲取Access Token 15 public function __construct($appid = NULL, $appsecret = NULL) 16 { 17 if($appid && $appsecret){ 18 $this->appid = $appid; 19 $this->appsecret = $appsecret; 20 } 21 22 $url = "https:// api.weixin.qq.com/cgi-bin/token?grant_type=client_cred ential&appid=".$this->appid."&secret=".$this->appsecret; 23 $res = $this->http_request($url); 24 $result = json_decode($res, true); 25 // save to Database or Memcache 26 $this->access_token = $result["access_token"]; 27 $this->lasttime = time; 28 } 29 30 // 創建菜單 31 public function create_menu($data) 32 { 33 $url = "https:// api.weixin.qq.com/cgi-bin/menu/create?access_token=".$ this->access_token; 34 $res = $this->http_request($url, $data); 35 return json_decode($res, true); 36 } 37 38 // 根據訂單ID獲取訂單詳情 39 public function get_detail_by_order_id($id) 40 { 41 $data = array('order_id' =>$id); 42 $url = "https:// api.weixin.qq.com/merchant/order/getbyid?access_token=". $this->access_token; 43 $res = $this->http_request($url, json_encode($data)); 44 return json_decode($res, true); 45 } 46 47 // 根據訂單狀態/創建時間獲取訂單詳情 48 public function get_detail_by_filter($data = null) 49 { 50 $url = "https:// api.weixin.qq.com/merchant/order/getbyfilter?access_token=". $this->access_token; 51 $res = $this->http_request($url, $data); 52 return json_decode($res, true); 53 } 54 55 // 發送客服消息,已實現發送文本,其他類型可擴展 56 public function send_custom_message($touser, $type, $data) 57 { 58 $msg = array('touser' =>$touser); 59 $msg['msgtype'] = $type; 60 switch($type) 61 { 62 case 'text': 63 $msg[$type] = array('content'=>urlencode($data)); 64 break; 65 case 'news': 66 $msg[$type] = array('articles'=>$data); 67 break; 68 default: 69 $msg['text'] = array('content'=>urlencode("不支持的消息類型 ".$type)); 70 break; 71 } 72 $url = "https:// api.weixin.qq.com/cgi-bin/message/custom/send?access_token=". $this->access_token; 73 return $this->http_request($url, urldecode(json_encode($msg))); 74 } 75 76 // 發送模板消息 77 public function send_template_message($data) 78 { 79 $url = "https:// api.weixin.qq.com/cgi-bin/message/template/send?access_ token=".$this->access_token; 80 $res = $this->http_request($url, $data); 81 return json_decode($res, true); 82 } 83 84 // HTTPS請求(支持GET和POST) 85 protected function http_request($url, $data = null) 86 { 87 $curl = curl_init; 88 curl_setopt($curl, CURLOPT_URL, $url); 89 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); 90 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); 91 if (!empty($data)){ 92 curl_setopt($curl, CURLOPT_POST, 1); 93 curl_setopt($curl, CURLOPT_POSTFIELDS, $data); 94 } 95 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 96 $output = curl_exec($curl); 97 curl_close($curl); 98 return $output; 99 } 100 }
上述代碼定義了微信小店的類,在類中定義了本章開發實現需要用到的方法,包括前面章節提到的創建自定義菜單、客服消息及模板消息等功能,以及需要用到的根據訂單ID獲取訂單詳情和根據訂單狀態/創建時間獲取訂單詳情兩個方法,這兩個方法的使用方法在後面的章節有詳細介紹。