Stack Exchange
Twitter usertimeline code not working
I have created this test script to get data from the twitter usertimeline which I'm sure was previously working however now it returns nothing. Is there something I am missing here? (To test simply amend the constants at the top)
define('CONSUMER_KEY', '');
define('CONSUMER_SECRET', '');
define('OAUTH_TOKEN','');
define('OAUTH_SECRET','');
define('USER_ID','');

function generateSignature($oauth,$fullurl,$http_method){        

// Take params from url
$main_url = explode('?',$fullurl);        

$urls = explode('&',$main_url[1]);

foreach ($urls as $param){
$bits = explode('=',$param);
if (strlen($bits[0])){
$oauth[$bits[0]] = rawurlencode($bits[1]);
}    
}

ksort($oauth);

$string = http_build_query($oauth);

$new_string = strtoupper($http_method).'&'.urlencode($main_url[0]).'&'.urlencode(urldecode($string));           

$sign_str = CONSUMER_SECRET.'&'.OAUTH_SECRET;

return urlencode(base64_encode(hash_hmac('sha1',$new_string,$sign_str,true)));        
} 

function random($len,$use_chars = false,$numU = false){
$num = range(0,9);
$letu = range('A','Z');
$letl = range('a','z');
$chars = array("!","*","£","$","^","(",")","_","+");

if ($use_chars){
$arr = array_merge($num,$letu,$letl,$chars);
} else {
$arr = array_merge($num,$letu,$letl);
}

// Shuffling - new addition 11/9 to make order actually random!
shuffle($arr);

// Create a number only random string
if ($numU){ $arr = $num; }

$rand = array_rand($arr,$len);
foreach ($rand as $num){
$out[] = $arr[$num];
}

return implode('',$out);
}

$method = 'GET';

// Twitter still uses Oauth1 (which is a pain)
$oauth = array(
'oauth_consumer_key'=>CONSUMER_KEY,
'oauth_nonce'=>random(32),
'oauth_signature_method'=>'HMAC-SHA1',
'oauth_timestamp'=>time(),
'oauth_token'=>OAUTH_TOKEN,
'oauth_version'=>'1.0',
);                    

$url = "https://api.twitter.com/1.1/statuses/user_timeline.json?user_id=".USER_ID;

$oauth['oauth_signature'] = generateSignature($oauth,$url,$method,'');                                

ksort($oauth);

foreach ($oauth as $k=>$v){
$auths[] = $k.'="'.$v.'"';
}

$stream = array('http' =>
array(
'method' => $method,
// ignore_errors should be true
'ignore_errors'=>true, // http://php.net/manual/en/context.http.php - otherwise browser returns error not error content
'follow_location'=>false,
'max_redirects'=>0,
'header'=> array(
'Authorization: OAuth '.implode(', ',$auths),
'Connection: close'
)                                             
)
);                                                                                                                 

echo $url;                                                 
$response = file_get_contents($url, false, stream_context_create($stream));

print'<pre>';print_r($stream);print'</pre>';
print'<pre>[';print_r($reponse);print']</pre>';


Have you checked your server's timezone against that of the API servers? I know in the past I've had a Twitter API script stop working for that reason.

Have you been able to run/test the above code though?

I found this:
// Create a number only random string
if ($numU){ $arr = $num; }

$rand = array_rand($arr,$len);
foreach ($rand as $num){
$out[] = $arr[$num];
}

I am not completly sure, but i think there shouldn't be a } after if ($numU){ $arr = $num;
I think it should be a open bracket. {
I found one more thing:
print'<pre>[';print_r($reponse);print']</pre>';

Here at the bottom of the code, you wrote "$reponse" instead of "$response"
I hope it helped! If it did, give it a upvote and choose as best answer!

Well halmi4 that has to be the easiest 50 points you'll probably earn! It was the $reponse part so congrats and thanks for taking the time to look at it.

np :D Sometimes you can't see the most simple things, in programming.
