getOauthUserinfo() public method

获取授权后的用户资料
public getOauthUserinfo ( string $access_token, string $openid ) : array
$access_token string
$openid string
return array {openid,nickname,sex,province,city,country,headimgurl,privilege,[unionid]} 注意:unionid字段 只有在用户将公众号绑定到微信开放平台账号后,才会出现。建议调用前用isset()检测一下
Ejemplo n.º 1
0
 public function wxoauth()
 {
     $scope = 'snsapi_base';
     $code = isset($_GET['code']) ? $_GET['code'] : '';
     $token_time = isset($_SESSION['token_time']) ? $_SESSION['token_time'] : 0;
     if (!$code && isset($_SESSION['open_id']) && isset($_SESSION['user_token']) && $token_time > time() - 3600) {
         if (!$this->wxuser) {
             $this->wxuser = $_SESSION['wxuser'];
         }
         $this->open_id = $_SESSION['open_id'];
         return $this->open_id;
     } else {
         $options = array('token' => $this->options["token"], 'appid' => $this->options["appid"], 'appsecret' => $this->options["appsecret"]);
         $we_obj = new Wechat($options);
         if ($code) {
             $json = $we_obj->getOauthAccessToken();
             if (!$json) {
                 unset($_SESSION['wx_redirect']);
                 die('获取用户授权失败,请重新确认');
             }
             $_SESSION['open_id'] = $this->open_id = $json["openid"];
             $access_token = $json['access_token'];
             $_SESSION['user_token'] = $access_token;
             $_SESSION['token_time'] = time();
             $userinfo = $we_obj->getUserInfo($this->open_id);
             if ($userinfo && !empty($userinfo['nickname'])) {
                 $this->wxuser = array('open_id' => $this->open_id, 'nickname' => $userinfo['nickname'], 'sex' => intval($userinfo['sex']), 'location' => $userinfo['province'] . '-' . $userinfo['city'], 'avatar' => $userinfo['headimgurl']);
             } elseif (strstr($json['scope'], 'snsapi_userinfo') !== false) {
                 $userinfo = $we_obj->getOauthUserinfo($access_token, $this->open_id);
                 if ($userinfo && !empty($userinfo['nickname'])) {
                     $this->wxuser = array('open_id' => $this->open_id, 'nickname' => $userinfo['nickname'], 'sex' => intval($userinfo['sex']), 'location' => $userinfo['province'] . '-' . $userinfo['city'], 'avatar' => $userinfo['headimgurl']);
                 } else {
                     return $this->open_id;
                 }
             }
             if ($this->wxuser) {
                 $_SESSION['wxuser'] = $this->wxuser;
                 $_SESSION['open_id'] = $json["openid"];
                 unset($_SESSION['wx_redirect']);
                 return $this->open_id;
             }
             $scope = 'snsapi_userinfo';
         }
         if ($scope == 'snsapi_base') {
             $url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
             $_SESSION['wx_redirect'] = $url;
         } else {
             $url = $_SESSION['wx_redirect'];
         }
         if (!$url) {
             unset($_SESSION['wx_redirect']);
             die('获取用户授权失败');
         }
         $oauth_url = $we_obj->getOauthRedirect($url, "wxbase", $scope);
         header('Location: ' . $oauth_url);
     }
 }
Ejemplo n.º 2
0
<?php

header("Content-Type: text/html; charset=UTF-8");
include "wechat.class.php";
$options = array('token' => 'vonvon', 'encodingaeskey' => 'encodingaeskey', 'appid' => 'wx519f23f4a45e8c37', 'appsecret' => 'ea8f0b17b3a0882bf5fda7ed27758482');
$weObj = new Wechat($options);
$data = $weObj->getOauthAccessToken();
//$new = $weObj->getOauthRefreshToken($data['refresh_token']);
$info = $weObj->getOauthUserinfo($data['access_token'], $data['openid']);
switch ($info['sex']) {
    case '1':
        $info['sex'] = '男';
        break;
    case '2':
        $info['sex'] = '女';
        break;
    default:
        $info['sex'] = '未知';
}
require_once "js/jssdk.php";
$jssdk = new JSSDK("wx519f23f4a45e8c37", "ea8f0b17b3a0882bf5fda7ed27758482");
$signPackage = $jssdk->GetSignPackage();
?>
<!DOCTYPE html><html lang="zh-CN" class="ua-wk ua-win ua-win-nt6 ua-win-nt63">
<!-- date: 2015-07-17 -->
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1,user-scalable=0" />
<title>哆啦A梦:最后的礼物</title>
<link rel="shortcut icon" type="image/x-icon" href="http://lxfm-s.malmam.com/images/lvxingfm_favicon.ico" />
<link rel="icon" type="image/x-icon" href="http://lxfm-s.malmam.com/images/lvxingfm_favicon.ico" />
Ejemplo n.º 3
0
 /**
  * 微信OAuth操作
  */
 static function do_oauth()
 {
     // 默认公众号信息
     $wxinfo = model('Base')->model->table('wechat')->field('id, token, appid, appsecret, oauth_redirecturi, type, oauth_status')->where('default_wx = 1 and status = 1')->find();
     if (!empty($wxinfo) && $wxinfo['type'] == 2) {
         $config['token'] = $wxinfo['token'];
         $config['appid'] = $wxinfo['appid'];
         $config['appsecret'] = $wxinfo['appsecret'];
         // 微信通验证
         $weObj = new Wechat($config);
         // 微信浏览器浏览
         if (self::is_wechat_browser() && ($_SESSION['user_id'] === 0 || empty($_SESSION['openid'])) && !empty($wxinfo['oauth_status'])) {
             if ($_GET['sale']) {
                 session('sale_id', I('get.sale'));
             }
             if (!isset($_SESSION['redirect_url'])) {
                 session('redirect_url', __HOST__ . $_SERVER['REQUEST_URI']);
             }
             $url = $weObj->getOauthRedirect($wxinfo['oauth_redirecturi'], 1);
             if (isset($_GET['code']) && !empty($_GET['code'])) {
                 $token = $weObj->getOauthAccessToken();
                 if ($token) {
                     $userinfo = $weObj->getOauthUserinfo($token['access_token'], $token['openid']);
                     self::update_weixin_user($userinfo, $wxinfo['id'], $weObj);
                     if (!empty($_SESSION['redirect_url'])) {
                         $redirect_url = session('redirect_url');
                         header('Location:' . $redirect_url, true, 302);
                         exit;
                     }
                 } else {
                     header('Location:' . $url, true, 302);
                     exit;
                 }
             } else {
                 header('Location:' . $url, true, 302);
                 exit;
             }
         }
     }
 }
Ejemplo n.º 4
0
 function getToken($url)
 {
     //引入微信类库
     APP::load(APP_FILE . 'common/class/wechat.class.php');
     $weObj = new Wechat(APP::$config['wechat']);
     // 注意 URL 一定要动态获取,不能 hardcode.
     $protocol = !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443 ? "https://" : "http://";
     $uri = "{$protocol}{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
     $signPackage = $weObj->getJsSign($uri);
     if (!isset($_GET['code'])) {
         header("Location: " . $url);
         exit;
     }
     $data = $weObj->getOauthAccessToken();
     if (!$data) {
         header("Location: " . $url);
         exit;
     }
     $userinfo = $weObj->getOauthUserinfo($data['access_token'], $data['openid']);
     $this->assign('signPackage', $signPackage);
     $_SESSION['openid'] = $data['openid'];
 }