365体育备用网址报道25.4.2微信API接口实现_365体育备用网址官网资讯

在本项目中,微信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的猎取存储及主动更新,猎取用户列表及猎取用户基本信息两个用户治理接口,以及客服消息的发送接口。

------分隔线----------------------------

公布评论 (139人察看0条评论)
请自觉遵守互联网相关的政策法规,严禁公布色情、暴力、反动的言论。
用户名: 验证码: 点击我更换博亿老虎机手机版
最新评论