php 微信授權認證獲取使用者基本資訊
阿新 • • 發佈:2018-12-26
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; }