The OAuth1Client allows you to connect to any OAuth1 server. Just follow the procedure described in the example below.
composer require webiik/oauth1client
// Twitter Example
// Prepare dependencies
$chc = new \Webiik\CurlHttpClient\CurlHttpClient();
$token = new \Webiik\Token\Token();
// Instantiate OAuth1 client
$oAuth1Client = new \Webiik\OAuth1Client\OAuth1Client($chc, $token);
// Set your callback URL
// OAuth1 server redirects users to this URL, after user verification
$oAuth1Client->setCallbackUrl('https://127.0.0.1/webiik/');
// Set OAuth1 server endpoints
$oAuth1Client->setAuthorizeUrl('https://api.twitter.com/oauth/authenticate');
$oAuth1Client->setRequestTokenUrl('https://api.twitter.com/oauth/request_token');
$oAuth1Client->setAccessTokenUrl('https://api.twitter.com/oauth/access_token');
// Set OAuth1 server access credentials (create yours at https://developer.twitter.com/en/apps)
$oAuth1Client->setConsumerKey('your-api-key');
$oAuth1Client->setConsumerSecret('your-api-secret');
// Make API calls...
// Notice: It's a good idea to separate below steps to individual routes.
if (!isset($_GET['oauth_verifier'])) {
// 1. Prepare Twitter login link
echo '<a href="' . $oAuth1Client->getAuthorizeUrl() . '" target="_blank">Authorize with Twitter</a><br/>';
}
if (isset($_GET['oauth_verifier'])) {
// 2. Verify oauth_token
$accessToken = $oAuth1Client->getAccessToken();
}
if (isset($accessToken, $accessToken['oauth_token'], $accessToken['oauth_token_sercret'])) {
// 3. oauth_token is valid, user is authorized by Twitter
// Access protected resources...
$urlParameters = [
'skip_status' => 'true',
];
$res = $oAuth1Client->get('https://api.twitter.com/1.1/account/verify_credentials.json', $accessToken['oauth_token'], $accessToken['oauth_token_secret'], $urlParameters);
header('Content-Type: application/json');
echo $res;
}
Before you can connect to any OAuth1 server, you have to properly configure access credentials and endpoints.
setConsumerKey(string $key): void
setConsumerKey() sets consumer key.
$oAuth1Client->setConsumerKey('your-api-key');
setConsumerSecret(string $secret): void
setConsumerSecret() sets consumer secret.
$oAuth1Client->setConsumerSecret('your-api-secret');
setSignatureSecret(string $secret): void
setSignatureSecret() sets signature secret. Usually it't optional or not required.
$oAuth1Client->setSignatureSecret('your-api-signature-secret');
setRequestTokenUrl(string $url): void
setRequestTokenUrl() sets URL to obtain a request token.
$oAuth1Client->setRequestTokenUrl('https://api.twitter.com/oauth/request_token');
setAuthorizeUrl(string $url): void
setAuthorizeUrl() sets URL to authorize a request token by user at OAuth1 server.
$oAuth1Client->setAuthorizeUrl('https://api.twitter.com/oauth/authenticate');
setCallbackUrl(string $url): void
setCallbackUrl() sets URL to redirect a user after authorization.
$oAuth1Client->setCallbackUrl('https://127.0.0.1/webiik/');
setAccessTokenUrl(string $url): void
setAccessTokenUrl() sets URL to obtain a access token.
$oAuth1Client->setAccessTokenUrl('https://api.twitter.com/oauth/access_token');
getAuthorizeUrl($requestToken): string
getAuthorizeUrl() makes HTTP POST request to a URL set by setRequestTokenUrl() and prepares authorized link to a URL set by setAuthorizeUrl().
$link = $oAuth1Client->getAuthorizeUrl();
getAccessToken()
getAccessToken() makes HTTP POST request to URL set by setAccessTokenUrl() and returns an array with 'oauth_token' on success and a string with cURL error message on error.
$accessToken = $oAuth1Client->getAccessToken();
get(string $url, string $oauth_token, string $oauth_token_secret, array $params = []): string
get() makes authorized HTTP GET request to OAuth1 API endpoint.
$urlParameters = [
'skip_status' => 'true',
];
$response = $oAuth1Client->get('https://api.twitter.com/1.1/account/verify_credentials.json', $accessToken['oauth_token'], $accessToken['oauth_token_secret'], $urlParameters);
post(string $url, string $oauth_token, string $oauth_token_secret, array $params = [], array $postData = []): string
post() makes authorized HTTP POST request to OAuth1 API endpoint.
$urlParameters = [
'include_entities' => 'true',
];
$postData = [
'status' => 'Hello Ladies + Gentlemen, a signed OAuth request!',
];
$response = $oAuth1Client->post('https://api.twitter.com/1.1/statuses/update.json', $accessToken['oauth_token'], $accessToken['oauth_token_secret'], $urlParameters, $postData);