php微信授权认证获取用户基本信息 拿到code值 public function snsapi_userinfo(){ $appid = $this->config->item('wechat_appid','app/config'); $redirect_uri = urlencode ( 'http://tp.ihouser.cn/weixin/M_wechat/getOauthAccessToken' );//将字符串以 URL 编码。 $url ="https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect"; header("Location:".$url); } 用code值获取openid及用户基本信息 public function getOauthAccessToken(){ $appid = $this->config->item('wechat_appid','app/config'); $secret = $this->config->item('wechat_appsecret','app/config'); $code = isset($_GET['code'])?$_GET['code']:''; if (!$code) return false; $result = file_get_contents('https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code'); if ($result) { $json = json_decode($result,true); if (!$json || !empty($json['errcode'])) { $this->errCode = $json['errcode']; $this->errMsg = $json['errmsg']; return false; } $this->user_token = $json['access_token']; $token = $json['access_token']; $openid = $json['openid']; $user = file_get_contents('https://api.weixin.qq.com/sns/userinfo?access_token='.$token.'&openid='.$openid.'&lang=zh_CN'); $user1 = json_decode($user,true); $openid = $user1['openid']; $username = $user1['username']; header("Location:".'http://tp.ihouser.cn/weixin/C_vote/index_list?openid='.$openid.'&username='.$username); } return false; }
php 微信授权认证获取用户基本信息
猜你喜欢
转载自blog.csdn.net/qq_39870660/article/details/85122615
今日推荐
周排行