獲取公衆號全部用戶的信息
- <?php
-
- $appid = "wxXXXXXXXXXXXXXXXXXX";
- $appsecret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
- $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";
-
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- $output = curl_exec($ch);
- curl_close($ch);
- $jsoninfo = json_decode($output, true);
- $access_token = $jsoninfo["access_token"];
-
- $url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=$access_token";
- $result = https_request($url);
- $jsoninfo = json_decode($result); // 默認false,爲Object,如果True,爲Array
-
- $data = $jsoninfo -> data;
- $arr = $data -> openid; // 得到全部用戶的Openid
-
- $temp = 0;
- while ($temp < count($arr)) {
- $openid = $arr[$temp];
- getInfo($access_token,$openid);
- $temp++;
- }
-
- // 根據Openid獲取單個用戶信息,如nickname
- function getInfo($access_token,$openid){
- $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$openid&lang=zh_CN";
- $output = https_request($url);
- $jsoninfo = json_decode($output);
- //echo $jsoninfo -> headimgurl;
- echo $jsoninfo -> nickname;
- echo "<br>";
- }
-
- function https_request($url)
- {
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- $data = curl_exec($curl);
- if (curl_errno($curl)) {return 'ERROR '.curl_error($curl);}
- curl_close($curl);
- return $data;
- }
- ?>
歡迎關注本站公眾號,獲取更多信息