微信企业号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;
    }


}

?> 
复制代码

 

posted @   一个人的孤独自白  阅读(1814)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程
点击右上角即可分享
微信分享提示