Skip to content

Instantly share code, notes, and snippets.

@pingpoli
Last active November 9, 2020 14:44
Show Gist options
  • Save pingpoli/f6692a46255fc5846b4299e00f3b43fc to your computer and use it in GitHub Desktop.
Save pingpoli/f6692a46255fc5846b4299e00f3b43fc to your computer and use it in GitHub Desktop.
<?php
// based on https://stackoverflow.com/questions/12684765/twitter-api-returns-error-215-bad-authentication-data
define( 'ACCESS_TOKEN' , 'YOUR_ACCESS_TOKEN' );
define( 'ACCESS_TOKEN_SECRET' , 'YOUR_ACCESS_TOKEN_SECRET' );
define( 'CONSUMER_KEY' , 'YOUR_CONSUMER_KEY' );
define( 'CONSUMER_KEY_SECRET' , 'YOUR_CONSUMER_KEY_SECRET' );
// get the twitter ids of all followers of your account
$query = array(
'screen_name' => 'YOUR_TWITTER_USERNAME',
'stringify_ids' => true
);
$json = json_decode( twitterRequest( "https://api.twitter.com/1.1/followers/ids.json" , "GET" , $query ) );
var_dump( $json );
function twitterRequest( $url , $method , $query )
{
// oauth parameters
$oauth = array(
'oauth_consumer_key' => CONSUMER_KEY,
'oauth_token' => ACCESS_TOKEN,
'oauth_nonce' => (string)mt_rand(),
'oauth_timestamp' => time(),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_version' => '1.0'
);
// must be encoded before sorting
$oauth = array_map( "rawurlencode" , $oauth );
$query = array_map( "rawurlencode" , $query );
$arr = array_merge( $oauth , $query ); // combine the values then sort
asort( $arr ); // secondary sort (value)
ksort( $arr ); // primary sort (key)
// http_build_query automatically encodes, but the parameters are already encoded, and must be by this point, so we undo the encoding step
$querystring = urldecode( http_build_query( $arr , '' , '&' ) );
// mash everything together for the text to hash
$base_string = $method."&".rawurlencode($url)."&".rawurlencode($querystring);
// same with the key
$key = rawurlencode(CONSUMER_KEY_SECRET)."&".rawurlencode(ACCESS_TOKEN_SECRET);
// generate the hash, which is the oauth signature
$oauth['oauth_signature'] = rawurlencode( base64_encode( hash_hmac( 'sha1' , $base_string , $key , true ) ) );
// some query parameters include commas and they don't work when the parameter is urlencoded above, so decode it here and use it raw, WARNING: this may be a little hacky
if ( $method == "GET" )
{
// build the GET parameters
$url .= "?";
while ( $element = current($query) )
{
$url .= key($query)."=".urldecode($element)."&";
next($query);
}
$url = rtrim( $url , "&" );
}
else
{
// build the POST parameters
$postParameters = "";
while ( $element = current($query) )
{
$postParameters .= key($query)."=".urldecode($element)."&";
next($query);
}
$postParameters = rtrim( $postParameters , "&" );
}
// this is the full value of the authorization line
$auth = "OAuth ".urldecode( http_build_query( $oauth , '' , ', ' ) );
// curl options
$options = array( CURLOPT_HTTPHEADER => array("Authorization: $auth"),
CURLOPT_HEADER => false,
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false);
// add the POST parameters
if ( $method == "POST" )
{
$options[CURLOPT_POSTFIELDS] = $postParameters;
}
// curl
$curl = curl_init();
curl_setopt_array( $curl , $options );
$result = curl_exec( $curl );
curl_close( $curl );
return $result;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment