Skip to content

Instantly share code, notes, and snippets.

@bobagold
Created February 1, 2012 11:02
Show Gist options
  • Select an option

  • Save bobagold/1716488 to your computer and use it in GitHub Desktop.

Select an option

Save bobagold/1716488 to your computer and use it in GitHub Desktop.
Simple command-line twitter read-only client in php
<?php
$tmhOAuth = __DIR__.'/tmhOAuth';
if (!is_dir($tmhOAuth)) die("please run git clone https://github.com/themattharris/tmhOAuth.git $tmhOAuth\n");
require $tmhOAuth . '/tmhOAuth.php';
require $tmhOAuth . '/tmhUtilities.php';
$config = array(
'consumer_key' => '',
'consumer_secret' => '',
'user_token' => '',
'user_secret' => '',
);
if (!array_filter($config)) {
$config_file = dirname($argv[0]) . '/' . basename($argv[0], '.php') . '.config.json';
if (!file_exists($config_file)) die("please create config file $config_file with structure ".json_encode($config) . "\n");
$config = json_decode(file_get_contents($config_file), true);
}
$tmhOAuth = new tmhOAuth($config);
$wat = $argv[1] ?: 'home_timeline';
$pages = explode(' ', 'home_timeline mentions public_timeline retweeted_by_me retweeted_to_me retweets_of_me user_timeline retweeted_to_user retweeted_by_user lists lists/statuses');
if (!in_array($wat, $pages)) $wat = '-h';
if ($wat == '-h') die('possible options: ' . implode(' ', $pages)."\n");
$who = $argv[2] ?: null;
$page = $argv[3] ?: 1;
$params = array();
if ($page > 1) $params['page'] = $page;
if ($who && $wat == 'lists/statuses')
list($params['owner_screen_name'], $params['slug']) = explode('/', $who);
else
if ($who) $params['screen_name'] = $who;
if (strpos($wat, 'lists') !== 0) $wat = 'statuses/'.$wat;
$code = $tmhOAuth->request('GET', $tmhOAuth->url('1/'.$wat), $params);
$how = $argv[4] ?: null;
if ($code == 200) {
if ($how == 'json') { echo $tmhOAuth->response['response']; exit; }
$m = json_decode($tmhOAuth->response['response'], true);
if ($wat == 'lists') {
foreach ($m['lists'] as $a)
echo $a["slug"]."\n\t".$a["user"]["screen_name"] . "\t".$a["created_at"]."\n";
exit;
}
foreach ($m as $a)
echo $a["text"]."\n\t".$a["user"]["screen_name"] . "\t".$a["created_at"]."\thttps://twitter.com/#!/{$a["user"]["screen_name"]}/status/$a[id_str]"."\n";
} else {
tmhUtilities::pr($tmhOAuth->response['response']);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment