讀古今文學網 > 微信公眾平台開發:從零基礎到ThinkPHP5高性能框架實踐 > 15.2 案例實踐:獲取門店ID列表 >

15.2 案例實踐:獲取門店ID列表

微信門店接口在其他多個業務場景中都需要用到,如微信卡券、微信連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節的結果一致。