Skip to content

Instantly share code, notes, and snippets.

@kayalshri
Created September 10, 2014 05:58
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 kayalshri/2df326117df16c7b2c5d to your computer and use it in GitHub Desktop.
Save kayalshri/2df326117df16c7b2c5d to your computer and use it in GitHub Desktop.
Simple twitter connector
<?php
function buildBaseString($baseURI, $method, $params)
{
$r = array();
ksort($params);
foreach($params as $key=>$value){
$r[] = "$key=" . rawurlencode($value);
}
return $method."&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $r)); //return complete base string
}
function buildAuthorizationHeader($oauth)
{
$r = 'Authorization: OAuth ';
$values = array();
foreach($oauth as $key=>$value)
$values[] = "$key=\"" . rawurlencode($value) . "\"";
$r .= implode(', ', $values);
return $r;
}
//https://api.twitter.com/1.1/account/verify_credentials.json
//https://api.twitter.com/1.1/search/tweets.json?q=%23freebandnames
$url = "https://api.twitter.com/1.1/statuses/home_timeline.json";
$oauth_access_token = "xxxxxxxxxxxxxxxxxxxxxx";
$oauth_access_token_secret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$consumer_key = "XXXXXXXXXXXXXXXXXXXXXXX";
$consumer_secret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$oauth = array( 'oauth_consumer_key' => $consumer_key,
'oauth_nonce' => time(),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_token' => $oauth_access_token,
'oauth_timestamp' => time(),
'oauth_version' => '1.0');
$base_info = buildBaseString($url, 'GET', $oauth);
$composite_key = rawurlencode($consumer_secret) . '&' . rawurlencode($oauth_access_token_secret);
$oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
$oauth['oauth_signature'] = $oauth_signature;
$header = array(buildAuthorizationHeader($oauth), 'Expect:');
$options = array( CURLOPT_HTTPHEADER => $header,
CURLOPT_HEADER => false,
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false);
$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);
$twitter_data = json_decode($json,true);
echo "<pre>";
print_r($twitter_data);
echo "</pre>";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment