微信企業號OAuth2.0驗證接口來獲取成員的身份信息

<?php

$appid = "請輸入您企業的appid";
$secret = "請輸入您企業的secreat";

if (!isset($_GET['code'])) {
    $this->redirect($this->qychat->getOauthRedirect($this->getUrl(), 'STATE', 'snsapi_userinfo', '1000003'));
      
}

  
$code = $_GET["code"];  //獲取登陸進來的用戶code  

header('content-type:text/html;charset=utf-8');//必定要加上這段代碼,必定必定切記切記 
$get_token_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' . $appid . '&corpsecret=' . $secret . '';
$access_token = json_decode(file_get_contents($get_token_url));
$access_tokens = $access_token->access_token;

//print_r( $access_tokens );

$get_Userid = 'https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=' . $access_tokens . '&code=' . $code . '&agentid=1000003';
$userIds = json_decode(file_get_contents($get_Userid));
$users = $userIds->UserId;

if ($userIds->UserId) {
    //print_r($users ); 
    $url = "http://www.baidu.com/";
    echo '<script language="JavaScript">window.location.href="跳轉的頁面index.html?userId=' . $users . '";</script>';
} else if ($userIds->OpenId) {
    //print_r("error");
    echo '<script language="JavaScript">alter("您當前未受權,請聯繫管理員!");</script>';
}

/*
$openid = $access_token->openid;  
$get_user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_tokens.'&openid='.$openid.'&lang=zh_CN'; //獲取用戶信息的url  
$user_info = json_decode(file_get_contents($get_user_info_url));  

//print_r($user_info);

$_SESSION['user'] = $user_info;   
echo $user_info->nickname;//獲得用戶信息  
echo $user_info->headimgurl;  
*/


public
function getOauthRedirect($callback, $state = 'STATE', $scope = 'snsapi_base', $agentid = '')
{
    if ($agentid == '') {
        $agentid = $this->agentid;
    }
    $url = self::OAUTH_PREFIX . self::OAUTH_AUTHORIZE_URL . 'appid=' . $this->appid . '&redirect_uri=' . urlencode($callback) . '&response_type=code&scope=' . $scope . '&agentid=' . $agentid . '&state=' . $state . '#wechat_redirect';

    return $url;

    /**
     * 跳轉url
     */
    public
    function redirect($url = '/')
    {
        Header('Location: ' . $url);
        exit;
    }


}

?> 
相關文章
相關標籤/搜索