Last active
August 29, 2015 14:01
-
-
Save tecnom1k3/9b6db6144a52fcc250c5 to your computer and use it in GitHub Desktop.
Guzzle oauth
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
session_start(); | |
require 'vendor/autoload.php'; | |
use GuzzleHttp\Client; | |
use GuzzleHttp\Subscriber\Oauth\Oauth1; | |
use GuzzleHttp\Subscriber\Log\LogSubscriber; | |
use Monolog\Logger; | |
use Monolog\Handler\StreamHandler; | |
use GuzzleHttp\Subscriber\Log\Formatter; | |
date_default_timezone_set('America/Phoenix'); | |
$authToken = $_GET['oauth_token']; | |
$authVerifier = $_GET['oauth_verifier']; | |
$log = new Logger('guzzle'); | |
$log->pushHandler(new StreamHandler('guzzle.log')); | |
$client = new Client(['base_url' => 'https://api.twitter.com', 'defaults' => ['auth' => 'oauth']]); | |
$subscriber = new LogSubscriber($log, Formatter::SHORT); | |
$client->getEmitter()->attach($subscriber); | |
$oauth = new Oauth1([ | |
'consumer_key' => '[your_api_key]', | |
'consumer_secret' => '[your_api_secret]', | |
'token' => $_SESSION['oauth_token'] | |
]); | |
$client->getEmitter()->attach($oauth); | |
if ($authToken == $_SESSION['oauth_token']) | |
{ | |
$res = $client->post('oauth/access_token', ['body' => ['oauth_verifier' => $authVerifier]]); | |
$params = (string)$res->getBody(); | |
parse_str($params); | |
$_SESSION['oauth_token'] = $oauth_token; | |
$_SESSION['oauth_token_secret'] = $oauth_token_secret; | |
$_SESSION['userId'] = $user_id; | |
$_SESSION['screenName'] = $screen_name; | |
header("Location: timeline.php"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "johndoe/guzzle-twitter", | |
"description": "PoC for Sitepoint article", | |
"authors": [ | |
{ | |
"name": "John Doe", | |
"email": "john.doe@gmail.tst" | |
} | |
], | |
"minimum-stability": "dev", | |
"require": { | |
"guzzlehttp/guzzle": "4.*", | |
"guzzlehttp/log-subscriber": "1.*", | |
"monolog/monolog": "*", | |
"guzzlehttp/oauth-subscriber": "*", | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
session_start(); | |
require 'vendor/autoload.php'; | |
use GuzzleHttp\Client; | |
use GuzzleHttp\Subscriber\Oauth\Oauth1; | |
use GuzzleHttp\Subscriber\Log\LogSubscriber; | |
use Monolog\Logger; | |
use Monolog\Handler\StreamHandler; | |
use GuzzleHttp\Subscriber\Log\Formatter; | |
date_default_timezone_set('America/Phoenix'); | |
$log = new Logger('guzzle'); | |
$log->pushHandler(new StreamHandler('guzzle.log')); | |
$client = new Client(['base_url' => 'https://api.twitter.com', 'defaults' => ['auth' => 'oauth']]); | |
$subscriber = new LogSubscriber($log, Formatter::SHORT); | |
$client->getEmitter()->attach($subscriber); | |
$oauth = new Oauth1([ | |
'consumer_key' => '[your_api_key]', | |
'consumer_secret' => '[your_api_secret]' | |
]); | |
$client->getEmitter()->attach($oauth); | |
// Set the "auth" request option to "oauth" to sign using oauth | |
$res = $client->post('oauth/request_token', ['body' => ['oauth_callback' => 'http://[yourawesomehost]/callback.php']]); | |
$params = (string)$res->getBody(); | |
parse_str($params); | |
$_SESSION['oauth_token'] = $oauth_token; | |
$_SESSION['oauth_token_secret'] = $oauth_token_secret; | |
header("Location: https://api.twitter.com/oauth/authenticate?oauth_token={$oauth_token}"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
session_start(); | |
require 'vendor/autoload.php'; | |
use GuzzleHttp\Client; | |
use GuzzleHttp\Subscriber\Oauth\Oauth1; | |
use GuzzleHttp\Subscriber\Log\LogSubscriber; | |
use GuzzleHttp\Subscriber\Log\Formatter; | |
use Monolog\Logger; | |
use Monolog\Handler\StreamHandler; | |
date_default_timezone_set('America/Phoenix'); //Set to a proper timezone | |
/* | |
* Setting up the logging backend, | |
*/ | |
$log = new Logger('guzzle'); | |
$log->pushHandler(new StreamHandler('guzzle.log')); // Log will be found at a file named guzzle.log | |
$subscriber = new LogSubscriber($log, Formatter::SHORT); //To see full details, you can use Formatter::DEBUG | |
/* | |
* Creating the Guzzle client, we are setting oauth as the default authentication method | |
*/ | |
$client = new Client(['base_url' => 'https://api.twitter.com', 'defaults' => ['auth' => 'oauth']]); | |
/* | |
* Setting up the oauth subscriber parameters. Parameter values | |
* have to be generated at the Twitter site | |
*/ | |
$oauth = new Oauth1([ | |
'consumer_key' => '[your_api_key]', | |
'consumer_secret' => '[your_api_secret]', | |
'token' => $_SESSION['oauth_token'], | |
'token_secret' => $_SESSION['oauth_token_secret'] | |
]); | |
/* | |
* Attaching the oauth and the log subscriber to the client | |
*/ | |
$client->getEmitter()->attach($oauth); | |
$client->getEmitter()->attach($subscriber); | |
/* | |
* Executing a GET request on the timeline service, pass the result to the json parser | |
*/ | |
$res = $client->get('1.1/statuses/home_timeline.json')->json(); | |
echo '<pre>'; | |
echo 'Timeline for user ' . $_SESSION['screenName'] . ':' . PHP_EOL . '<hr>'; | |
foreach ($res as $tweet) | |
{ | |
echo 'From: ' . $tweet['user']['name'] . ' (@' . $tweet['user']['screen_name'] . ')' . PHP_EOL; | |
echo ' ' . htmlentities($tweet['text']) . PHP_EOL . '<hr>'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment