微信授权的获取用户信息的几种方法
1 <?php 2 3 class wxcard 4 5 { 6 7 private $wxinfo = ''; 8 9 //public $error = array(); 10 11 12 13 function __construct($wxinfo) { 14 15 $this->wxinfo = $wxinfo; 16 17 18 19 } 20 21 //demo 获取用户微信 信息 22 23 /* public function get_user_openid() 24 25 { 26 27 //判断是不是微信is_weixin() is_mobile() 28 29 $wx_user['appid'] = ''; 30 31 $wx_user['appSecret'] = ''; 32 33 $wxCardPack = new Wxcard($wx_user); 34 35 36 37 $url = "https://lepay.51ao.com/merchants.php?m=Index&c=index&a=addbonus&mid=$b"; 38 39 $info_arr = $wxCardPack->auth_openid($url); 40 41 //$this->fanSave($info_arr,$b); 42 43 //header("Location:".WUYI_PATH."merchants.php?m=Index&c=index&a=bonus&mid=$b");die; 44 45 }*/ 46 47 public function one_openid($url) 48 49 { 50 51 52 53 if (empty($_GET['code'])){ 54 55 //echo '2';die; 56 57 $_SESSION['weixinstate'] = md5(uniqid()); 58 59 $oauthUrl = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . $this->wxinfo['appid'] . '&redirect_uri=' . urlencode($url) . '&response_type=code&scope=snsapi_base&state=1#wechat_redirect'; 60 61 header('Location: ' . $oauthUrl); 62 63 exit; 64 65 } else if (isset($_GET['code'])) { 66 67 //echo $_GET['code'];die; 68 69 $jsonrt = $this->post('https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $this->wxinfo['appid'] . '&secret=' . $this->wxinfo['appSecret'] . '&code=' . $_GET['code'] . '&grant_type=authorization_code'); 70 71 $jsonrt = json_decode($jsonrt,true); 72 73 if ($jsonrt['errcode'] || empty($jsonrt['openid'])) { 74 75 return array('error' => 1, 'msg' => '授权发生错误:' . $jsonrt['errcode']); 76 77 } 78 79 if ($jsonrt['openid']) { 80 81 $openid = $jsonrt['openid']; 82 83 return array('openid' => $openid);die; 84 85 } 86 87 } else { 88 89 return array('error' => 2); 90 91 } 92 93 94 95 } 96 97 98 99 100 101 public function auth_openid($url) 102 103 { 104 105 106 107 if (empty($_GET['code'])){ 108 109 //echo '2';die; 110 111 $_SESSION['weixinstate'] = md5(uniqid()); 112 113 $oauthUrl = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . $this->wxinfo['appid'] . '&redirect_uri=' . urlencode($url) . '&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect'; 114 115 header('Location: ' . $oauthUrl); 116 117 exit; 118 119 } else if (isset($_GET['code'])) { 120 121 //echo $_GET['code'];die; 122 123 $jsonrt = $this->post('https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $this->wxinfo['appid'] . '&secret=' . $this->wxinfo['appSecret'] . '&code=' . $_GET['code'] . '&grant_type=authorization_code'); 124 125 $jsonrt = json_decode($jsonrt,true); 126 127 if ($jsonrt['errcode'] || empty($jsonrt['openid'])) { 128 129 return array('error' => 1, 'msg' => '授权发生错误:' . $jsonrt['errcode']); 130 131 } 132 133 if ($jsonrt['openid']) { 134 135 //$_SESSION['openid'] = $jsonrt['openid']; 136 137 //var_dump($jsonrt);die; 138 139 $access_token=$jsonrt['access_token']; 140 141 $openid = $jsonrt['openid']; 142 143 144 145 //$userinfo_url="https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$openid&lang=zh_CN"; 146 147 $userinfo_url="https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid"; 148 149 $userinfo_json=$this->post($userinfo_url); 150 151 $userinfo_array=json_decode($userinfo_json,true); 152 153 return $userinfo_array; 154 155 //var_dump($userinfo_array);die; 156 157 } 158 159 } else { 160 161 return array('error' => 2); 162 163 } 164 165 166 167 } 168 169 public function GetwxUserInfoByOpenid($wxAccessToken,$openid){ 170 $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" . $wxAccessToken.'&openid='.$openid.'&lang=zh_CN'; 171 $result = $this->post($url); 172 $result = json_decode($result,true); 173 return $result; 174 } 175 176 public function getToken() 177 { 178 //getToken 179 $filename = PIGCMS_PATH.'/upload/token/accesstoken'; 180 // echo $filename;die; 181 if(!file_exists($filename) || (file_exists($filename) && (time()-filemtime($filename)) > 5000)){ 182 //1.url地址 183 $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->wxinfo['appid'].'&secret='.$this->wxinfo['appSecret']; 184 //2.判断是否为post请求 185 //3.发送请求 186 $content = $this->post($url); 187 //4.处理返回值 188 //返回数据格式为json,php不可以直接操作json格式,需要json_decode转化一下 189 $content = json_decode($content); 190 $access_token = $content->access_token; 191 //把access_token保存到文件 192 file_put_contents($filename, $access_token); 193 } 194 //如果没有过期,那么就去读取缓存文件里的access_token 195 else{ 196 $access_token = file_get_contents($filename); 197 } 198 //把access_token返回 199 return $access_token; 200 } 201 202 203 204 public function post($url) 205 206 { 207 208 // 创建curl对象 209 210 $ch = curl_init (); 211 212 // 配置这个对象 213 214 curl_setopt ($ch, CURLOPT_URL, $url); // 请求的URL地址 215 216 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // 对认证证书来源的检查 217 218 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // 从证书中检查SSL加密算法是否存在 219 220 221 222 curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); // 返回接口的结果,而不是输出 223 224 225 226 // 发出请求 227 228 $data = curl_exec ( $ch ); 229 230 if(curl_errno($ch)) 231 232 { 233 234 return 'error'.curl_error($ch); 235 236 } 237 238 // 关闭对象 239 240 curl_close ( $ch ); 241 242 // 返回数据 243 244 return $data; 245 246 } 247 248 }