微信公众号查询粉丝列表
生活随笔
收集整理的這篇文章主要介紹了
微信公众号查询粉丝列表
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
公眾號通過接口查詢所有粉絲的openid信息列表。
一 . 查詢用戶列表:
?? 1. 獲取access_token。
?? 2. 獲取open_id。
?? 2. 查詢用戶列表。(get方式訪問)
class UsersService extends Service {private $appid = ''; //自己appidprivate $appsecret = ''; //自己appsecretprivate $access_token; //tokenprivate $open_id;public $error;//$begin 代表從哪個用戶開始,類似于order bypublic function userList($begin = ''){if(empty($this->access_token)){$this->getToken();}$begin = empty($begin) ? '' : 'OPENID'.$begin;$url = 'https://api.weixin.qq.com/cgi-bin/user/get?access_token='.$this->access_token.'&next_openid='.$begin;$result = $this->requestUrl($url);$result = json_decode($result);$data = $result->data->openid;foreach($data as $k => $v){$info_url = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token='.$this->access_token.'&openid='.$v.'&lang=zh_CN';$data_info = $this->requestUrl($info_url);$user_info[] = json_decode($data_info);}return $user_info;}/** 獲取普通通用token*/public function getToken(){if (empty($_SESSION['accessToken'])) {$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $this->appid . '&secret=' . $this->appsecret;$result = json_decode($this->requestUrl($url), true);if (empty($result['access_token'])) {$this->error = $result['errorMsg'];return false;}$_SESSION['accessToken'] = $result['access_token'];}$this->access_token = $_SESSION['accessToken'];}/*** curl 請求*/private function requestUrl($url, $data = ''){$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //禁止 cURL 驗證對等證書curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // 為SSL不檢查名稱curl_setopt($ch, CURLOPT_HEADER, 0);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);// curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);if (!empty($data)) {curl_setopt($ch, CURLOPT_POST, true);curl_setopt($ch, CURLOPT_POSTFIELDS, $data);}$result = curl_exec($ch);curl_close($ch);return $result;}二 . 控制器邊調用方法查詢:
/*** 微信獲取用戶列表* @writer dan* @date 2022-07-14*/public function actionUserList(){$begin = 0;$users = $this->users()->userList($begin);if(empty($users)){return '暫無人關注';}return $users;}更多詳情,可以看微信官方文檔:微信開放文檔,微信開放文檔
總結
以上是生活随笔為你收集整理的微信公众号查询粉丝列表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 英语------------单词复数形式
- 下一篇: 几台WEB经常宕机,求分析原因