在正式讲解开发内容之前,需要先将常用功能函数写入类中,以便在后面的章节中调用。以下是方倍工作室开发的用于微信小店的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获取订单详情和根据订单状态/创建时间获取订单详情两个方法,这两个方法的使用方法在后面的章节有详细介绍。