Skip to content

Instantly share code, notes, and snippets.

@hubgit
Last active December 16, 2015 19:50
Show Gist options
  • Save hubgit/5487898 to your computer and use it in GitHub Desktop.
Save hubgit/5487898 to your computer and use it in GitHub Desktop.
PHP Twitter client (application-level authentication)
<?php
require 'TwitterClient.php';
define('OUTPUT_DIR', 'output'); // edit this: directory to save the output in
define('SCREEN_NAME', 'ubuweb'); // edit this: username to fetch
mkdir(OUTPUT_DIR);
$client = new TwitterClient();
$params = array(
'screen_name' => SCREEN_NAME,
'count' => 200,
'contributor_details' => true,
'include_user_entities' => true,
);
do {
$data = $client->get('/statuses/user_timeline', $params);
if (!$data) {
break;
}
print_r($data);
foreach ($data as $item) {
$file = OUTPUT_DIR . '/tweet-' . $item['id_str'] . '.json';
file_put_contents($file, json_encode($item));
$params['max_id'] = $item['id'] - 1;
}
} while (true);
<?php
require 'TwitterClient.php';
define('OUTPUT_DIR', 'output'); // edit this: directory to save the output in
define('SCREEN_NAME', 'MediumEditorial'); // edit this: username to fetch
mkdir(OUTPUT_DIR);
$client = new TwitterClient();
$params = array(
'screen_name' => SCREEN_NAME,
'include_user_entities' => true,
);
do {
$data = $client->get('/followers/list', $params);
print_r($data);
if ($data['users']) {
foreach ($data['users'] as $friend) {
$file = OUTPUT_DIR . '/user-' . $friend['id_str'] . '.json';
file_put_contents($file, json_encode($friend));
}
}
$params['cursor'] = $data['next_cursor_str'];
} while ($params['cursor']);
<?php
date_default_timezone_set('UTC');
// Create an application at https://dev.twitter.com/apps/ for keys
class TwitterClient {
/** @var cURL */
public $curl;
private $config = array();
private $headers = array();
public function __construct() {
$this->configure();
$this->curl = curl_init();
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->curl, CURLOPT_VERBOSE, true);
curl_setopt($this->curl, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($this->curl, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $this->config['access_token']));
curl_setopt($this->curl, CURLOPT_HEADERFUNCTION, array($this, 'header'));
}
public function get($path, $params = array()) {
$url = 'https://api.twitter.com/1.1' . $path . '.json';
if ($params) {
$url .= '?' . http_build_query($params);
}
print "$url\n";
curl_setopt($this->curl, CURLOPT_URL, $url);
return $this->exec();
}
protected function configure() {
$configFile = getenv('HOME') . '/.config/twitter.ini';
if (!file_exists($configFile)) {
exit(sprintf("Config file %s does not exist\n", $configFile));
}
$this->config = parse_ini_file($configFile);
if (!isset($this->config['access_token'])) {
foreach (array('consumer_key', 'consumer_secret') as $field) {
if (!isset($this->config[$field])) {
printf("Add %s to %s\n", $field, $configFile);
exit();
}
}
exit(sprintf("Add this line to %s:\naccess_token=%s\n", $configFile, $this->token()));
}
}
protected function exec() {
$this->headers = array();
$result = curl_exec($this->curl);
$code = curl_getinfo($this->curl, CURLINFO_HTTP_CODE);
switch ($code) {
case 200:
return json_decode($result, true);
case 429: // rate limit
$this->delay();
$this->exec();
break;
default:
print "Error $code\n";
print_r($result, true);
return null;
}
}
protected function header($curl, $header) {
list($name, $value) = explode(': ', $header, 2);
$this->headers[strtolower($name)] = $value;
return strlen($header);
}
protected function token() {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://api.twitter.com/oauth2/token');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, 'grant_type=client_credentials');
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Authorization: Basic ' . base64_encode(rawurlencode($this->config['consumer_key']) . ':' . rawurlencode($this->config['consumer_secret']))
));
$result = curl_exec($curl);
$response = json_decode($result, true);
if (!isset($response['access_token'])) {
exit('No access token found:' . print_r($response, true));
}
return $response['access_token'];
}
protected function delay() {
if (!isset($this->headers['x-rate-limit-reset'])) {
exit('No rate limit found');
}
$delay = $this->headers['x-rate-limit-reset'] - time();
if ($delay < 10) {
$delay = 60 * 15; // 15 minute delay if the given delay seems unreasonable
}
printf('Sleeping for %d seconds', $delay);
sleep($delay);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment