Skip to content

Instantly share code, notes, and snippets.

@pete-rai
Created January 10, 2019 09:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pete-rai/6e25cdb662124563fba649d6812d36d0 to your computer and use it in GitHub Desktop.
Save pete-rai/6e25cdb662124563fba649d6812d36d0 to your computer and use it in GitHub Desktop.
A simple PHP wrapper around the Twitter API with full authorization support
<?php
/*
* A simple PHP wrapper around the Twitter API with full authorization support
*
* Released with the karmaware tag - https://pete-rai.github.io/karmaware
*
* Website : http://www.rai.org.uk
* GitHub : https://github.com/pete-rai
* LinkedIn : https://uk.linkedin.com/in/raipete
* NPM : https://www.npmjs.com/~peterai
*
*/
class Twitter
{
// to get your twitter access tokens visit - https://developer.twitter.com/en/docs/basics/authentication/guides/access-tokens.html
protected static $CONSUMER_KEY = '- YOUR KEY HERE ---------';
protected static $CONSUMER_SECRET = '- YOUR SECRET HERE -------------------------------';
protected $bearer = null; // filled on first call and then cached in object instance
// --- calls the api and returns json
protected static function call ($opt)
{
$opt [CURLOPT_RETURNTRANSFER] = true;
$curl = curl_init ();
curl_setopt_array ($curl, $opt);
$data = curl_exec ($curl);
curl_close ($curl);
return json_decode ($data);
}
// --- generates the token needed to access the api
protected static function token ()
{
$token = base64_encode (implode (':', [urlencode (self::$CONSUMER_KEY), urlencode (self::$CONSUMER_SECRET)]));
return "Authorization: Basic $token";
}
// --- helper function to build the public url to a tweet
public static function url ($tweet)
{
return "https://twitter.com/{$tweet->user->screen_name}/status/{$tweet->id}";
}
// --- obtains and object instance caches the bearer token
protected function bearer ()
{
if (!$this->bearer)
{
$opt =
[
CURLOPT_URL => 'https://api.twitter.com/oauth2/token',
CURLOPT_HTTPHEADER => [self::token ()],
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => 'grant_type=client_credentials',
];
$this->bearer = self::call ($opt)->access_token;
}
return "Authorization: Bearer {$this->bearer}";
}
// --- gets a list of tweets from a given users timeline
public function tweets ($user, $count = 8)
{
$opt =
[
CURLOPT_URL => "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=$user&count=$count",
CURLOPT_HTTPHEADER => [$this->bearer ()],
];
return self::call ($opt);
}
}
/*
// --- test code only - leave commented out once working
$twitter = new Twitter ();
$tweets = $twitter->tweets ('RoyalFamily', 4);
foreach ($tweets as $tweet)
{
echo implode (' : ', [date ('Y-m-d H:i', strtotime ($tweet->created_at)), Twitter::url ($tweet), $tweet->text])."\n";
}
$tweets = $twitter->tweets ('POTUS', 4); // this second call will use teh cahced bearer token
foreach ($tweets as $tweet)
{
echo implode (' : ', [date ('Y-m-d H:i', strtotime ($tweet->created_at)), Twitter::url ($tweet), $tweet->text])."\n";
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment