更新时间:4月 20, 2026 / 创建时间:4月 20, 2026
php codeigniter使用示例代码
<?php
class Oauth extends CI_Controller{
function __construct()
{
parent::__construct();
$this->load->library('session');
$this->load->helper('string');
$this->oauth_server = 'https://admin.jeawin.com/';
// 替换成自己的
$this->client_id = 'test_client';
// 替换成自己的
$this->client_secret = 'test_secret';
// 替换成自己的
$this->redirect_url = 'http://localhost:8080/callback';
}
function index(){
$state = random_string('alnum', 10);
$this->session->set_userdata('test_oauth_state', $state);
$url = $this->oauth_server . 'oauth/authorize?client_id='.$this->client_id.'&response_type=code&redirect_uri='.rawurlencode($this->redirect_url).'&state='.$state.'&scope=eggs-count%20profile';
redirect($url);
}
function client(){
// 获取access_token
try {
$client = new GuzzleHttp\Client(['verify' => FALSE]);
$url = $this->ouauth_server . 'oauth/token';
// var_dump($url);
$response = $client->request('POST', $url, array(
'form_params' => array(
'client_id' => $this->client_id,
'client_secret' => $this->client_secret,
'grant_type' => 'client_credentials',
)
));
// log_message("error", $code);
// var_dump($response->getStatusCode());
$body = $response->getBody()->getContents();
$obj = json_decode($body);
if(!is_null($obj)){
$access_token = $obj->access_token;
// 保存access_token
$this->session->set_userdata('test_access_token', $access_token);
// 通过access_token获取api数据
$response = $client->request('GET', $this->ouauth_server . 'oauth/api', [
'headers' => [
'Authorization' => 'Bearer ' . $access_token
]
]);
// $composed = new Psr7\AppendStream([$response->getBody()]);
$composed = $response->getBody()->getContents();
var_dump($composed);
//
}
}catch(Exception $e){
echo $e->getMessage();
}
}
function callback(){
// echo 'callback';
// 检查state和code
$state = $this->input->get('state');
$test_oauth_state = $this->session->userdata('test_oauth_state');
if($state !== $test_oauth_state){
$this->output->set_output('state请求参数错误');
return;
}
$code = $this->input->get('code');
if(!isset($code)){
$this->output->set_output('code参数错误');
return;
}
// 获取access_token
try {
$client = new GuzzleHttp\Client(['verify' => FALSE]);
$url = $this->ouauth_server . 'oauth/token';
// var_dump($url);
$response = $client->request('POST', $url, array(
'form_params' => array(
'client_id' => $this->client_id,
'client_secret' => $this->client_secret,
'grant_type' => 'authorization_code',
'redirect_uri' => $this->redirect_url,
'code' => $code
)
));
// log_message("error", $code);
// var_dump($response->getStatusCode());
$body = $response->getBody()->getContents();
$obj = json_decode($body);
if(!is_null($obj)){
$access_token = $obj->access_token;
$refresh_token = $obj->refresh_token;
// 保存access_token
$this->session->set_userdata('test_access_token', $access_token);
$this->session->set_userdata('test_refresh_token', $refresh_token);
// 通过access_token获取api数据
$response = $client->request('GET', $this->ouauth_server . 'oauth/api', [
'headers' => [
'Authorization' => 'Bearer ' . $access_token
]
]);
// $composed = new Psr7\AppendStream([$response->getBody()]);
$composed = $response->getBody()->getContents();
var_dump($composed);
//
}
}catch(Exception $e){
echo $e->getMessage();
}
}
function get_api(){
$access_token = $this->session->userdata('test_access_token');
try{
$client = new GuzzleHttp\Client(['verify' => FALSE]);
$response = $client->request('GET', $this->ouauth_server . 'oauth/api', [
'headers' => [
'Authorization' => 'Bearer ' . $access_token
]
]);
// $composed = new Psr7\AppendStream([$response->getBody()]);
$composed = $response->getBody()->getContents();
var_dump($composed);
}catch(Exception $e){
var_dump($e->getMessage());
}
}
function get_userinfo(){
$access_token = $this->session->userdata('test_access_token');
try{
$client = new GuzzleHttp\Client(['verify' => FALSE]);
$response = $client->request('GET', $this->ouauth_server . 'oauth/api/userinfo', [
'headers' => [
'Authorization' => 'Bearer ' . $access_token
]
]);
// $composed = new Psr7\AppendStream([$response->getBody()]);
$composed = $response->getBody()->getContents();
var_dump($composed);
}catch(Exception $e){
var_dump($e->getMessage());
}
}
function get_forms(){
$access_token = $this->session->userdata('test_access_token');
try{
$client = new GuzzleHttp\Client(['verify' => FALSE]);
$response = $client->request('GET', $this->ouauth_server . 'oauth/api/forms', [
'headers' => [
'Authorization' => 'Bearer ' . $access_token
],
'query' => [
'site_id' => 4
]
]);
// $composed = new Psr7\AppendStream([$response->getBody()]);
$composed = $response->getBody()->getContents();
var_dump($composed);
}catch(Exception $e){
var_dump($e->getMessage());
}
}
function get_forms_nodes(){
$form_id = $this->input->get('form_id');
$access_token = $this->session->userdata('test_access_token');
try{
$client = new GuzzleHttp\Client(['verify' => FALSE]);
$response = $client->request('GET', $this->ouauth_server . 'oauth/api/form_nodes', [
'headers' => [
'Authorization' => 'Bearer ' . $access_token
],
'query' => [
'form_id' => $form_id
]
]);
// $composed = new Psr7\AppendStream([$response->getBody()]);
$composed = $response->getBody()->getContents();
var_dump($composed);
}catch(Exception $e){
var_dump($e->getMessage());
}
}
function refresh_token(){
$refresh_token = $this->session->userdata('test_refresh_token');
try{
$client = new GuzzleHttp\Client(['verify' => FALSE]);
$response = $client->request('POST', $this->ouauth_server . 'oauth/token', [
'form_params' => [
'client_id' => $this->client_id,
'client_secret' => $this->client_secret,
'grant_type' => 'refresh_token',
'refresh_token' => $refresh_token
]
]);
$body = $response->getBody()->getContents();
var_dump($body);
$obj = json_decode($body);
if(!is_null($obj)){
$access_token = $obj->access_token;
$refresh_token = $obj->refresh_token;
// 重新保存access_token
$this->session->set_userdata('test_access_token', $access_token);
$this->session->set_userdata('test_refresh_token', $refresh_token);
}
}catch(Exception $e){
var_dump($e->getMessage());
}
}
}
// end this file