PHP code example of cocolait / sys_oauth

1. Go to this page and download the library: Download cocolait/sys_oauth library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cocolait / sys_oauth example snippets


composer 

// 加载composer
入口文件


return [
    //腾讯QQ登录配置
    'SYA_AUTH_QQ'      => [
        'APP_KEY'       => '',  //应用注册成功后分配的 APP ID
        'APP_SECRET'    => '',  //应用注册成功后分配的KEY
        'CALLBACK'      => '',  // 应用回调地址
    ],
    //新浪微博配置
    'SYA_AUTH_SINA'    => [
        'APP_KEY'       => '', //应用注册成功后分配的 APP ID
        'APP_SECRET'    => '', //应用注册成功后分配的KEY
        'CALLBACK'      => '', // 应用回调地址
    ],
    //微信登录
    'SYA_AUTH_WEIXIN' => [
        'APP_KEY' => '',     //应用注册成功后分配的 APP ID
        'APP_SECRET' => '',  //应用注册成功后分配的KEY
        'CALLBACK' => "",    //应用回调地址
    ]
);

namespace Home\Controller;
use Think\Controller;
class OauthController extends Controller {
	//登录地址
	//目前type参数 只支持 [qq,sina,weixin]
	public function login($type = null){
		empty($type) && $this->error('参数错误');
		$_SESSION['login_http_referer']=$_SERVER["HTTP_REFERER"];
		$sns  = \Cp\Sys\Oauth::getInstance($type);
		//跳转到授权页面
		redirect($sns->getRequestCodeURL());
	}

	//授权回调地址
	public function callback($type = null, $code = null){
		(empty($type)) && $this->error('参数错误');

		if(empty($code)){
			redirect(__ROOT__."/");
		}

		$sns  = \Cp\Sys\Oauth::getInstance($type);
		$extend = null;
        // 获取TOKEN
		$token = $sns->getAccessToken($code , $extend);

		//获取当前第三方登录用户信息
		if(is_array($token)){
			  $user_info = \Cp\Sys\GetInfo::getInstance($type,$token);
			  var_dump($user_info);
		}else{
              echo "获取基本信息失败";
		}
	}
}