giberti/ext-oauth-shim

PHP中pecl OAuth扩展的实现,适用于不提供此扩展的共享主机

v2.0.2 2023-12-09 20:11 UTC

This package is auto-updated.

Last update: 2024-09-09 21:37:52 UTC


README

旨在提供对OAuthOAuthExceptionOAuthProvider的支持,并满足需要它的composer包的ext-oauth要求。这是一个正在进行中的工作。

警告:如果可能,请使用PECL扩展。它会更快!

质量

Build and Test

贡献

这是一个正在进行中的工作,但是OAuthClient可以作为缺少支持的系统的替代品使用。在某些情况下,它可能更受欢迎,因为它不会在一些边缘情况下导致PECL客户端那样的致命错误。

OAuthProvider尚未完成。请考虑帮助完善OAuthProvider类或测试或两者。当尝试贡献时,这份使用Docker进行测试的指南可能有所帮助。欢迎提交拉取请求!

安装

此库需要PHP 7.3或更高版本才能使用,包括8.x版本直到8.3。

composer require giberti/ext-oauth-shim

用法

客户端

大多数时候,您会使用此OAuth客户端来请求数据。您需要先创建客户端实例,并传入您的访问令牌和密钥。

// Replace with your values
$consumer       = 'consumer';
$consumerSecret = 'secret';
$token          = 'token';
$tokenSecret    = 'secret';

// Create the client
$client = new OAuth($consumer, $consumerSecret);

// Set the access credentials
$client->setToken($token, $tokenSecret);

现在您已经配置了客户端,可以开始发送请求。要对URI发出一个简单的GET请求,您可以调用fetch()函数并传入URL。

// GET a protected resource
$response = $client->fetch('https://example.com/user/me');

另一个常见用例是发送数据,例如,发布一条推文。

// POST data to a protected resource
$postData = [
    'status' => 'Hello Twitter!'
];
$response = $client->fetch('https://api.twitter.com/1.1/statuses/update.json', $postData, 'POST');

这也可以用于更复杂的有效载荷,例如JSON。

JSON正文

$data = [
    'Name' => 'Jane Doe',
    'Age'  => '30',
];
$json = json_encode($data);

$headers = [
    'Content-type' => 'application/json',
];

$response = $client->fetch('https://example.com/user/janedoe', $json, 'POST', $headers);

二进制正文

将GIF发布到接受二进制图像数据的端点的示例。

$image = file_get_contents('funny.gif');
$headers = [
    'Content-type' => 'image/gif',
];

$response = $client->fetch('https://example.com/image/', $image, 'POST', $headers);

获取访问令牌

某些提供者不会自动颁发访问令牌,如果您交互的API没有提供此令牌,您将需要创建一个。

// Replace with your values
$consumer       = 'consumer';
$consumerSecret = 'secret';

// Create the client
$client = new OAuth($consumer, $consumerSecret);

// Set this to the callback page on your site
$callbackUrl = 'https://yoursite.com/oauth/finish';

// These two Urls are provided to you by the API provider
$requestTokenUrl  = 'https://example.com/oauth/request-token';
$authorizationUrl = 'https://example.com/oauth/authorize';

// Fetch a request token and store it in the session
$requestToken = $client->getRequestToken();

// Store the request token and secret for later
$_SESSION['requestToken'] = $requestToken;

// Redirect the browser to the authorization page
header('Location: ' . $authorizationUrl . '?' . urlencode($requestToken['oauth_token]));

用户的浏览器将被重定向到服务,在那里他们将授予应用程序权限。一旦完成此步骤,浏览器将重定向回您在第一个代码示例中提供的回调URL。

// Replace with your values
$consumer       = 'consumer';
$consumerSecret = 'secret';

$requestToken = $_SESSION['requestToken'];

// Create the client
$client = new OAuth($consumer, $consumerSecret);
$client->setToken($requestToken['oauth_token'], $requestToken['oauth_token_secret']);

$accessTokenUrl = 'https://example.com/oauth/access-token';

// Exchange the request token for a permanent access token
$accessToken = $client->getAccessToken($accessTokenUrl, null, $_REQUEST['oauth_verifier']);
$_SESSION['accessToken'] = $accessToken;
unset($_SESSION['requestToken']);

对于所有未来的请求,您将使用访问令牌与API进行交互。

// Replace with your values
$consumer       = 'consumer';
$consumerSecret = 'secret';

$accessToken = $_SESSION['accessToken'];

// Create the client
$client = new OAuth($consumer, $consumerSecret);
$client->setToken($accessToken['oauth_token'], $accessToken['oauth_token_secret']);

// Fetch a resource
$response = $client->fetch('https://example.com/user/friends');