Twitter usertimeline代码不起作用

时间:2014-12-08 10:16:24

标签: php twitter

我已经创建了这个测试脚本来从twitter usertimeline获取数据,我确信它以前工作过,但现在它什么也没有返回。这里有什么我想念的吗? (测试只是修改顶部的常量)

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>';

1 个答案:

答案 0 :(得分:0)

我发现了这个:

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

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

我不完全确定,但我认为在if ($numU){ $arr = $num;之后不应该有}} 我认为它应该是一个开放的支架。 {

我发现了另外一件事:

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

在代码底部,您编写了“$ reponse”而不是“$ response”

我希望它有所帮助!如果确实如此,请给它一个upvote并选择最佳答案!