docs/examples/Twitter/Twitter-PHP_OAuth.php
<?php
use GuzzleHttp\Client;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware\OpenAuthentication\OAuth10a;
use GuzzleHttp\RequestOptions;
error_reporting(-1);
ini_set('display_errors', 'On');
/**
* Example file: shows how to connect to the Twitter API using OAuth Core 1.0a.
*
* Twitter API uses OAuth Core 1.0a
This example uses the PHP OAuth extension.
* @link http://php.net/manual/en/book.oauth.php
*
This example uses the OAuth Core 1.0a naming conventions.
* @link http://oauth.net/core/1.0a/
*
* This example is adapted from @link http://php.net/manual/en/oauth.examples.fireeagle.php
*
* To read more about Twitter API:
* @link https://dev.twitter.com/rest/public
* @link https://dev.twitter.com/oauth
*
* PRECONDITIONS
* =============
*
* 1. Install PHP OAuth extension on your server
* 2. Create a test app on Twitter:
* 1. Go to https://apps.twitter.com/
* 2. Create your test app
* 1. Set a website (fill in what you want)
* 2. Leave the callback URL blank
*/
require __DIR__ . '/../../../vendor/autoload.php';
/**
* REQUIRED VARIABLES
* ==================
*
* Consumer Credentials are generated by the Service Provider (Twitter) and are unique for the Consumer (this script!).
* The Consumer uses them to make itself recognizable by the Service Provider when the Consumer sends a Resource Owner to it to
* get the authorization to access his Protected Resources and get pair of AccessCredentials.
*
* @var string $consumerKey The consumer key
* @var string $consumerSecret The secret key
*
* "Used by the Consumer to access the Protected Resources on behalf of the User. Access Tokens MAY limit access to
* certain Protected Resources, and MAY have a limited lifetime. Service Providers SHOULD allow Users to revoke Access
* Tokens. Only the Access Token SHALL be used to access the Protect Resources."
* @link http://oauth.net/core/1.0a/#rfc.section.6
*
* @var string $accessTokenKey The key part of the access token credentials
* @var string $accessTokenSecret The shared secret part of the access token credentials
*/
// Consumer Credentials: created at Precondition 2
$consumerKey = 'your-consumer-key';
$consumerSecret = 'your-consumer-secret';
// Set here the access token generated at Precondition 3
$accessTokenKey = 'your-token-key';
$accessTokenSecret = 'your-token-secret';
// The home_timeline endpoint
$resourceUrl = 'https://api.twitter.com/1.1/statuses/home_timeline.json';
try {
$oauthClient = new OAuth($consumerKey, $consumerSecret, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_AUTHORIZATION);
$oauthClient->enableDebug();
$oauthClient->setToken($accessTokenKey, $accessTokenSecret);
$oauthClient->fetch($resourceUrl, [], 'GET', ['Content-Type' => 'application/json']);
$tweetsList = json_decode($oauthClient->getLastResponse(), true);
dump($tweetsList);
dump($oauthClient->debugInfo);
} catch (OAuthException $e) {
dump($e->getMessage());
echo "<br/>";
dump($e->lastResponse);
}
echo '<hr />';
dump($oauthClient);