php – Twitter获取状态失败并附加参数

前端之家收集整理的这篇文章主要介绍了php – Twitter获取状态失败并附加参数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
编辑:
我回答了自己的问题.我不知道在原始问题中表明这一点的恰当礼仪,或者仅仅回答和接受它.

如何添加其他参数以过滤转发和回复

我尝试向twitter开发论坛提交一个问题,但我想我会在这里得到更好的结果.

我使用了answer中的示例代码来实现状态的工作检索.我想通过使用我在twitter dev api上看到的参数来尝试过滤掉转推和回复,并更新来自

$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),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,$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,CURLOPT_URL => $url . '?exclude_replies=true&include_rts=false',$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>";
}

?>
原文链接:https://www.f2er.com/php/134387.html

猜你在找的PHP相关文章