微信门店接口在其他多个业务场景中都需要用到,如微信卡券、微信连WiFi等。在这些业务场景中,需要确定使用业务的门店有哪些,因此事先获取门店列表并保存有助于后续业务的开发。
微信门店的PHP SDK实现代码如下。
1 class class_wxpoint 2 { 3 var $appid = APPID; 4 var $appsecret = APPSECRET; 5 6 // 构造函数,获取Access Token 7 public function __construct($appid = NULL, $appsecret = NULL) 8 { 9 if($appid && $appsecret){ 10 $this->appid = $appid; 11 $this->appsecret = $appsecret; 12 } 13 $res = file_get_contents('token.json'); 14 $result = json_decode($res, true); 15 $this->expires_time = $result["expires_time"]; 16 $this->access_token = $result["access_token"]; 17 18 if (time > ($this->expires_time + 3600)){ 19 $url = "https:// api.weixin.qq.com/cgi-bin/token?grant_type=client_ credential&appid=".$this->appid."&secret=".$this->appsecret; 20 $res = $this->http_request($url); 21 $result = json_decode($res, true); 22 $this->access_token = $result["access_token"]; 23 $this->expires_time = time; 24 file_put_contents('token.json', '{"access_token": "'.$this->access_ token.'", "expires_time": '.$this->expires_time.'}'); 25 } 26 } 27 28 // 查询门店列表 29 public function get_poi_list($begin = 0, $limit = 20) 30 { 31 $msg = array('begin' => $begin, 'limit' => $limit); 32 $url = "https:// api.weixin.qq.com/cgi-bin/poi/getpoilist?access_token=". $this->access_token; 33 $res = $this->http_request($url, urldecode(json_encode($msg))); 34 return json_decode($res, true); 35 } 36 37 // HTTP请求(支持HTTP/HTTPS,支持GET/POST) 38 protected function http_request($url, $data = null) 39 { 40 $curl = curl_init; 41 curl_setopt($curl, CURLOPT_URL, $url); 42 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); 43 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); 44 if (!empty($data)){ 45 curl_setopt($curl, CURLOPT_POST, 1); 46 curl_setopt($curl, CURLOPT_POSTFIELDS, $data); 47 } 48 curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); 49 $output = curl_exec($curl); 50 curl_close($curl); 51 return $output; 52 } 53 }
调用获取门店ID列表的实现代码如下。
define('APPID', "wxfd3fd09f8b7c8f84"); define('APPSECRET', "b45c1dd5152b35cfb3c7c70514decd04"); $weixin = new class_wxpoint; $result = $weixin->get_poi_list; var_dump($result);
上述程序执行后的结果和15.1.4节的结果一致。