- <?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;
- }
- ?>
转载于:https://my.oschina.net/u/588516/blog/639991