Skip to content

Instantly share code, notes, and snippets.

@hubgit
Created May 20, 2010 17:58
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hubgit/407862 to your computer and use it in GitHub Desktop.
Save hubgit/407862 to your computer and use it in GitHub Desktop.
Command line Twitter OAuth authentication in PHP
<?php
// Register an app: http://dev.twitter.com/apps
define('CONSUMER_KEY', 'YOUR CONSUMER KEY');
define('CONSUMER_SECRET', 'YOUR CONSUMER SECRET');
define('TWITTER_TOKEN', '');
define('TWITTER_TOKEN_SECRET', '');
$oauth = new OAuth(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_URI);
if (!(TWITTER_TOKEN && TWITTER_TOKEN_SECRET)){
$request_token = $oauth->getRequestToken('https://api.twitter.com/oauth/request_token');
$oauth->setToken($request_token['oauth_token'], $request_token['oauth_token_secret']);
$url = 'https://api.twitter.com/oauth/authorize?' . http_build_query(array('oauth_token' => $request_token['oauth_token']));
//system('open ' . escapeshellarg($url));
print "Authorize:\n$url\nEnter the PIN: ";
$access_token = $oauth->getAccessToken('https://api.twitter.com/oauth/access_token', NULL, trim(fgets(STDIN)));
printf("define('TWITTER_TOKEN', '%s');\ndefine('TWITTER_TOKEN_SECRET', '%s');\n", $access_token['oauth_token'], $access_token['oauth_token_secret']);
exit();
}
$oauth->setToken(TWITTER_TOKEN, TWITTER_TOKEN_SECRET);
$oauth->fetch('http://twitter.com/account/verify_credentials.json');
print_r(json_decode($oauth->getLastResponse()));
@fireproofsocks
Copy link

It would be helpful to know which OAuth class was being used here...

@tvlooy
Copy link

tvlooy commented Dec 19, 2016

the repo name is "oauth-pecl" so https://pecl.php.net/package/oauth

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment