zoukankan      html  css  js  c++  java
  • 微信企业号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;
        }
    
    
    }
    
    ?> 
  • 相关阅读:
    共用体类型,结构体类型
    动态内存分配与指向它的指针变量
    向上转型,向下转型
    枚举类型中的构造方法、成员方法
    由setTimeout()里的this引出的this
    eclipse快捷键
    js中运算符的优先级
    关于js闭包杂记
    sublime在Mac osx下安装z-codeing(即emmet)
    利用js得到某个范围内的整数随机数
  • 原文地址:https://www.cnblogs.com/-mrl/p/7146895.html
Copyright © 2011-2022 走看看