Ở hocdekhangdinhminh.com cài plugin OAuth Server :)
Client ID: NquBsEdmLk23ByRt3U5LUjLNX628aELPTsTTmm3K
Client Secret: qw69GVPAXnVcSH3uE0fStoYjNgI6lyvolTzzxn52
Lấy mã nguồn ở đây :)
Ví dụ 1: Xác thực ở hanam.work
public_html\test.php (hanam.work)
<?php
require 'vendor/autoload.php';
const CLIENT_ID = 'NquBsEdmLk23ByRt3U5LUjLNX628aELPTsTTmm3K';
const CLIENT_SECRET = 'qw69GVPAXnVcSH3uE0fStoYjNgI6lyvolTzzxn52';
const REDIRECT_URI = 'https://hanam.work/test.php';
const AUTHORIZATION_ENDPOINT = 'https://hocdekhangdinhminh.com/oauth/authorize';
const TOKEN_ENDPOINT = 'https://hocdekhangdinhminh.com/oauth/token';
$client = new OAuth2\Client(CLIENT_ID, CLIENT_SECRET);
if (!isset($_GET['code'])) {
$auth_url = $client->getAuthenticationUrl(AUTHORIZATION_ENDPOINT, REDIRECT_URI);
header('Location: ' . $auth_url);
die('Redirect');
} else {
$params = array(
'code' => $_GET['code'],
'redirect_uri' => REDIRECT_URI,
);
$response = $client->getAccessToken(TOKEN_ENDPOINT, 'authorization_code', $params);
echo '<pre>';
print_r($response);
echo '</pre>';
}
?>
Truy cập đường dẫn này https://hanam.work/test.php nó tự chuyển về dạng https://hanam.work/test.php?code=utmznu4sowxst6fdk2d7fpkkfml7wgwycp57jwjp&iframe=break
Cấu trúc thư mục :)
Ví dụ 2: Xác thực ở hocdekhangdinhminh.com
public_html\oauth\test.php (hocdekhangdinhminh.com)
<?php
require 'vendor/autoload.php';
const CLIENT_ID = 'NquBsEdmLk23ByRt3U5LUjLNX628aELPTsTTmm3K';
const CLIENT_SECRET = 'qw69GVPAXnVcSH3uE0fStoYjNgI6lyvolTzzxn52';
const REDIRECT_URI = 'https://hocdekhangdinhminh.com/oauth/test.php';
const AUTHORIZATION_ENDPOINT = 'https://hocdekhangdinhminh.com/oauth/authorize';
const TOKEN_ENDPOINT = 'https://hocdekhangdinhminh.com/oauth/token';
$client = new OAuth2\Client(CLIENT_ID, CLIENT_SECRET);
if (!isset($_GET['code'])) {
$auth_url = $client->getAuthenticationUrl(AUTHORIZATION_ENDPOINT, REDIRECT_URI);
header('Location: ' . $auth_url);
die('Redirect');
} else {
$params = array(
'code' => $_GET['code'],
'redirect_uri' => REDIRECT_URI,
);
$response = $client->getAccessToken(TOKEN_ENDPOINT, 'authorization_code', $params);
echo '<pre>';
print_r($response);
echo '</pre>';
}
?>
Truy cập https://hocdekhangdinhminh.com/oauth/test.php nó tự chuyển về dạng https://hocdekhangdinhminh.com/oauth/test.php?code=c13pzprgitytumfatkqv7wr3xggzx8zfmsuwz7ll&iframe=break
Cấu trúc thư mục như sau để cho dễ phát triển
Ví dụ 3: Hoàn thành ví dụ 1, ví dụ 2 sử dụng OAuth để lấy bài viết "revisions"
public_html\test.php (hanam.work)
<?php
require 'vendor/autoload.php';
const CLIENT_ID = 'NquBsEdmLk23ByRt3U5LUjLNX628aELPTsTTmm3K';
const CLIENT_SECRET = 'qw69GVPAXnVcSH3uE0fStoYjNgI6lyvolTzzxn52';
const REDIRECT_URI = 'https://hanam.work/test.php';
const AUTHORIZATION_ENDPOINT = 'https://hocdekhangdinhminh.com/oauth/authorize';
const TOKEN_ENDPOINT = 'https://hocdekhangdinhminh.com/oauth/token';
$client = new OAuth2\Client(CLIENT_ID, CLIENT_SECRET);
if (!isset($_GET['code'])) {
$auth_url = $client->getAuthenticationUrl(AUTHORIZATION_ENDPOINT, REDIRECT_URI);
header('Location: ' . $auth_url);
die('Redirect');
} else {
$params = array(
'code' => $_GET['code'],
'redirect_uri' => REDIRECT_URI,
);
$response = $client->getAccessToken(TOKEN_ENDPOINT, 'authorization_code', $params);
$client->setAccessToken($response['result']['access_token']);
$data = $client->fetch("https://hocdekhangdinhminh.com/wp-json/wp/v2/posts/1/revisions");
echo '<pre>';
var_export($data);
echo '</pre>';
}
?>
Giờ thì lấy được giá trị như sau :)