Twitter获取状态失败并带有其他参数
编辑:我回答了我自己的问题。 我不知道在原始问题中表明这一点的正确礼仪,或者只是自己回答和接受礼仪。
如何添加额外的参数来过滤掉转推和回复?
我尝试向twitter开发者论坛提交问题,但我想我会在这里得到更好的结果。
我已经使用这个答案的示例代码来实现状态的工作检索。 我想尝试使用我在twitter dev api上看到的参数筛选转发和回复,并更新URL
$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";
这会返回错误23“无法验证你”。
我的“猜测”是我不应该在基本url中包含附加参数,而是作为示例代码被注释掉的oauth数组中的附加参数。
// 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);
带有删除令牌的完整示例代码。
<?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>";
}
?>
所以这个改变非常简单。 这些参数不属于主要网址。 url只是基础url,然后params被添加到oauth数组中,并且params被另外添加到$ options数组中的url中。
显示两个参数不包括推特和回复。
<?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/11335.html