Twitter get status fails with additional parameters
Edit: I answered my own question. I do not know the proper etiquette for indicating this in the original question or just answering and accepting it myself.
How do I add additional parameters to filter out retweets and replies?
I tried submitting a question to the twitter dev forums but I think I will get better results also asking it here.
I have used sample code from this answer to implement a working retrieval of statuses. I want to try and filter out the retweets and replies by using the parameters I saw on the twitter dev api and update the url from
$url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
to
$url = "https://api.twitter.com/1.1/statuses/user_timeline.json?include_rts=false";
which returns an error 23 "Could not authenticate you".
My "guess" is that I shouldn't be including the additional parameters in the base url, but as additional parameters into the oauth array where the sample code was commented out.
// Make Requests
$header = array(buildAuthorizationHeader($oauth), 'Expect:');
$options = array( CURLOPT_HTTPHEADER => $header,
//CURLOPT_POSTFIELDS => $postfields,
CURLOPT_HEADER => false,
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false);
Full Sample code with removed tokens.
<?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));
}
function buildAuthorizationHeader($oauth) {
$r = 'Authorization: OAuth ';
$values = array();
foreach($oauth as $key=>$value)
$values[] = "$key="" . rawurlencode($value) . """;
$r .= implode(', ', $values);
return $r;
}
//$url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
$url = "https://api.twitter.com/1.1/statuses/user_timeline.json?include_rts=false";
$oauth_access_token = "removed";
$oauth_access_token_secret = "removed";
$consumer_key = "removed";
$consumer_secret = "removed";
$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;
// Make Requests
$header = array(buildAuthorizationHeader($oauth), 'Expect:');
$options = array( CURLOPT_HTTPHEADER => $header,
//CURLOPT_POSTFIELDS => $postfields,
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);
foreach ($twitter_data as $tweet)
{
$text = $tweet->text;
echo $text . "</br></br>";
}
?>
So the change is pretty simple. The params don't belong in the main url. The url is just the base url, then the params are added to the oauth array, and the params are additionally added to the url in the $options array.
Shown with two parameters exclude retweets and replies.
<?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));
}
function buildAuthorizationHeader($oauth) {
$r = 'Authorization: OAuth ';
$values = array();
foreach($oauth as $key=>$value)
$values[] = "$key="" . rawurlencode($value) . """;
$r .= implode(', ', $values);
return $r;
}
$url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
$oauth_access_token = "removed";
$oauth_access_token_secret = "removed";
$consumer_key = "removed";
$consumer_secret = "removed";
$oauth = array( 'exclude_replies' => 'true',
'include_rts' => 'false',
'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;
// Make Requests
$header = array(buildAuthorizationHeader($oauth), 'Expect:');
$options = array( CURLOPT_HTTPHEADER => $header,
//CURLOPT_POSTFIELDS => $postfields,
CURLOPT_HEADER => false,
CURLOPT_URL => $url . '?exclude_replies=true&include_rts=false',
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);
foreach ($twitter_data as $tweet)
{
$text = $tweet->text;
echo $text . "</br></br>";
}
?>
链接地址: http://www.djcxy.com/p/11336.html
上一篇: 为什么不能找到在`pip search`结果中列出的软件包?
下一篇: Twitter获取状态失败并带有其他参数