讀古今文學網 > 微信公眾平台開發:從零基礎到ThinkPHP5高性能框架實踐 > 25.4.2 微信API接口實現 >

25.4.2 微信API接口實現

在本項目中,微信API接口也是作為一個第三方類庫存在的,其目錄為extend\weixin\Wxapi.php。其實現代碼如下。


  1 <?php
  2 
  3 /*
  4     方倍工作室 http:// www.fangbei.org/
  5     CopyRight 2014 All Rights Reserved
  6 */
  7 namespace weixin;
  8 require_once('config.php');
  9 
 10 class Wxapi
 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         // 3. 本地寫入
 23         $res = file_get_contents('access_token.json');
 24         $result = json_decode($res, true);
 25         $this->expires_time = $result["expires_time"];
 26         $this->access_token = $result["access_token"];
 27 
 28         if (time > ($this->expires_time + 3600)){
 29             $url = "https:// api.weixin.qq.com/cgi-bin/token?grant_type=client_
                credential&appid=".$this->appid."&secret=".$this->appsecret;
 30             $res = $this->http_request($url);
 31             $result = json_decode($res, true);
 32             $this->access_token = $result["access_token"];
 33             $this->expires_time = time;
 34             file_put_contents('access_token.json', '{"access_token": "'.$this->
                access_token.'", "expires_time": '.$this->expires_time.'}');
 35         }
 36     }
 37     
 38     // 獲取用戶信息
 39     public function get_user_info($openid)
 40     {
 41         $url = "https:// api.weixin.qq.com/cgi-bin/user/info?access_token=".$this->
        access_token."&openid=".$openid."&lang=zh_CN";
 42         $res = $this->http_request($url);
 43         return json_decode($res, true);
 44     }
 45     
 46     // 獲取用戶列表
 47     public function get_user_list($next_openid = NULL)
 48     {
 49         $url = "https:// api.weixin.qq.com/cgi-bin/user/get?access_token=".$this->
            access_token."&next_openid=".$next_openid;
 50         $res = $this->http_request($url);
 51         $list = json_decode($res, true);
 52         if ($list["count"] == 10000){
 53             $new = $this->get_user_list($next_openid = $list["next_openid"]);
 54             $list["data"]["openid"] = array_merge_recursive($list["data"]["openid"], 
                $new["data"]["openid"]); // 合併OpenID列表
 55         }
 56         return $list;
 57     }
 58 
 59     // 發送客服消息
 60     public function send_custom_message($touser, $type, $data)
 61     {
 62         $msg = array('touser' =>$touser);
 63         $msg['msgtype'] = $type;
 64         switch($type)
 65         {
 66             case 'text':
 67                 $msg[$type] = array('content'=>urlencode($data));
 68                 break;
 69             case 'news':
 70                 $data2 = array;
 71                 foreach ($data as &$item) {
 72                     $item2 = array;
 73                     foreach ($item as $k => $v) {
 74                         $item2[strtolower($k)] = urlencode($v);
 75                     }
 76                     $data2 = $item2;
 77                 }
 78                 $msg[$type] = array('articles'=>$data2);
 79                 break;
 80             case 'music':
 81             case 'image':
 82             case 'voice':
 83             case 'video':
 84                 $msg[$type] = $data;
 85                 break;
 86             default:
 87                 $msg['text'] = array('content'=>urlencode("不支持的消息類型 ".$type));
 88                 break;
 89         }
 90         $url = "https:// api.weixin.qq.com/cgi-bin/message/custom/send?access_
            token=".$this->access_token;
 91         return $this->http_request($url, urldecode(json_encode($msg)));
 92     }
 93 
 94     // HTTP請求(支持HTTP/HTTPS,支持GET/POST)
 95     protected function http_request($url, $data = null)
 96     {
 97         $curl = curl_init;
 98         curl_setopt($curl, CURLOPT_URL, $url);
 99         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
100         curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
101         if (!empty($data)){
102             curl_setopt($curl, CURLOPT_POST, 1);
103             curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
104         }
105         curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
106         $output = curl_exec($curl);
107         curl_close($curl);
108         return $output;
109     }
110 }
  

上述接口方法中,實現了微信Access Token的獲取、存儲及自動更新,獲取用戶列表及獲取用戶基本信息兩個用戶管理接口,以及客服消息的發送接口。