1. Go to this page and download the library: Download printy6/oauth1 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/ */
printy6 / oauth1 example snippets
// Includes the Composer autoload file.
eate an instance of Risan\OAuth1\OAuth1 class.
$oauth1 = Risan\OAuth1\OAuth1Factory::create([
'client_credentials_identifier' => 'YOUR_TWITTER_API_KEY',
'client_credentials_secret' => 'YOUR_TWITTER_API_SECRET',
'temporary_credentials_uri' => 'https://api.twitter.com/oauth/request_token',
'authorization_uri' => 'https://api.twitter.com/oauth/authorize',
'token_credentials_uri' => 'https://api.twitter.com/oauth/access_token',
'callback_uri' => 'YOUR_CALLBACK_URI',
]);
if (isset($_SESSION['token_credentials'])) {
// Get back the previosuly obtain token credentials (step 3).
$tokenCredentials = unserialize($_SESSION['token_credentials']);
$oauth1->setTokenCredentials($tokenCredentials);
// STEP 4: Retrieve the user's tweets.
// It will return the Psr\Http\Message\ResponseInterface instance.
$response = $oauth1->request('GET', 'https://api.twitter.com/1.1/statuses/user_timeline.json');
// Convert the response to array and display it.
var_dump(json_decode($response->getBody()->getContents(), true));
} elseif (isset($_GET['oauth_token']) && isset($_GET['oauth_verifier'])) {
// Get back the previosuly generated temporary credentials (step 1).
$temporaryCredentials = unserialize($_SESSION['temporary_credentials']);
unset($_SESSION['temporary_credentials']);
// STEP 3: Obtain the token credentials (also known as access token).
$tokenCredentials = $oauth1->requestTokenCredentials($temporaryCredentials, $_GET['oauth_token'], $_GET['oauth_verifier']);
// Store the token credentials in session for later use.
$_SESSION['token_credentials'] = serialize($tokenCredentials);
// this basically just redirecting to the current page so that the query string is removed.
header('Location: ' . (string) $oauth1->getConfig()->getCallbackUri());
exit();
} else {
// STEP 1: Obtain a temporary credentials (also known as the request token)
$temporaryCredentials = $oauth1->requestTemporaryCredentials();
// Store the temporary credentials in session so we can use it on step 3.
$_SESSION['temporary_credentials'] = serialize($temporaryCredentials);
// STEP 2: Generate and redirect user to authorization URI.
$authorizationUri = $oauth1->buildAuthorizationUri($temporaryCredentials);
header("Location: {$authorizationUri}");
exit();
}
$plainTextSigner = new Risan\OAuth1\Signature\PlainTextSigner();
$oauth1 = Risan\OAuth1\OAuth1Factory::create($config, $plainTextSigner);
$authorizationUri = $oauth1->buildAuthorizationUri($temporaryCredentials);
// Redirect user to the authorization URI.
header("Location: {$authorizationUri}");
exit();
// Set the previously obtained token credentials.
$oauth1->setTokenCredentials($tokenCredentials);
// Make a request to the protected resource.
$response = $oauth1->request('GET', 'https://api.twitter.com/1.1/statuses/user_timeline.json');
// GET method
$oauth1->get($uri, $options);
// POST method
$oauth1->post($uri, $options);
// PUT method
$oauth1->put($uri, $options);
// PATCH method
$oauth1->patch($uri, $options);
// DELETE method
$oauth1->delete($uri, $options);
echo $response->getStatusCode();
// Get all of the headers
$headers = $response->getHeaders();
// Or get a specific header
$header = $response->getHeader('X-Foo');
$bodyStream = $response->getBody();
// Get the string representation of the stream
$bodyStream = $bodyStream->getContents();
// Or simply cast it
$bodyString = (string) $bodyStream;