Because of the Twitter API 1.0 retirement as of June 11th 2013, the script below does not work anymore.
// Create curl resource
$ch = curl_init();
// Set u
Here's a brief one for getting a specified number of tweets from your timeline. It basically does the same thing as the other examples, only with less code.
Just fill in the keys and adjust $count
to your liking:
$url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$count = '10';
$oauth = array('count' => $count,
'oauth_consumer_key' => '[CONSUMER KEY]',
'oauth_nonce' => md5(mt_rand()),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_timestamp' => time(),
'oauth_token' => '[ACCESS TOKEN]',
'oauth_version' => '1.0');
$oauth['oauth_signature'] = base64_encode(hash_hmac('sha1', 'GET&' . rawurlencode($url) . '&' . rawurlencode(implode('&', array_map(function ($v, $k) { return $k . '=' . $v; }, $oauth, array_keys($oauth)))), '[CONSUMER SECRET]&[ACCESS TOKEN SECRET]', true));
$twitterData = json_decode(file_get_contents($url . '?count=' . $count, false, stream_context_create(array('http' => array('method' => 'GET',
'header' => 'Authorization: OAuth '
. implode(', ', array_map(function ($v, $k) { return $k . '="' . rawurlencode($v) . '"'; }, $oauth, array_keys($oauth))))))));
This one uses anonymous functions and file_get_contents
instead of the cURL library. Note the use of an MD5 hashed nonce. Everyone seems to be going along with the time()
nonce, however, most examples on the web concerning OAuth use some kind of encrypted string (like this one: http://www.sitepoint.com/understanding-oauth-1/). This makes more sense to me too.
Further note: you need PHP 5.3+ for the anonymous functions (in case your server/computer is in some cold war cave and you can't upgrade it).