Twitter API没有提到帖子

时间:2017-08-30 19:49:00

标签: php api twitter

我已经设置了Twitter API,用于从用户名为matthutchings95获取推文,在此帐户中,我已经发布了10次测试推文。现在页面正在阅读这些,但我然后制作了第二个推特帐户dannybots111并写了一条提到第一个帐户的推文。但是,我不得不将screen_name更改为其他内容?我认为这提到了该帐户的提及和推文。

<?
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;
}

function returnTweet(){
    $oauth_access_token         = "1140776708-Z03sg5mVXqc4il9ff1EfBcbE5aycwlfslDni2Lt";
    $oauth_access_token_secret  = "gr6RbsvIq9kUoREjVkwlOgHSwJJU7do91NpCz87PKNMOz";
    $consumer_key               = "Ub3R51niGzjJIFQtCDrb7tvjJ";
    $consumer_secret            = "T74d8SJYDET9n8T4PDI7aobNwo9M6fkmQI0hHhWd9ntzY9y5hL";

    $twitter_timeline           = "user_timeline";  //  mentions_timeline / user_timeline / home_timeline / retweets_of_me

    //  create request
        $request = array(
            'screen_name'       => 'matthutchings95',
            'count'             => '3'
        );

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

    //  merge request and oauth to one array
        $oauth = array_merge($oauth, $request);

    //  do some magic
        $base_info              = buildBaseString("https://api.twitter.com/1.1/statuses/$twitter_timeline.json", '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 request
        $header = array(buildAuthorizationHeader($oauth), 'Expect:');
        $options = array( CURLOPT_HTTPHEADER => $header,
                          CURLOPT_HEADER => false,
                          CURLOPT_URL => "https://api.twitter.com/1.1/statuses/$twitter_timeline.json?". http_build_query($request),
                          CURLOPT_RETURNTRANSFER => true,
                          CURLOPT_SSL_VERIFYPEER => false);

        $feed = curl_init();
        curl_setopt_array($feed, $options);
        $json = curl_exec($feed);
        curl_close($feed);

    return json_decode($json, true); 
}
    $tweet = returnTweet();

    echo "Tweet No.1: " .$tweet[0]["text"];
    echo '<br><br>';
    echo "Tweet No.2: " .$tweet[1]["text"];
    echo '<br><br>';
    echo "Tweet No.3: " .$tweet[2]["text"];
    echo '<br><br>';
    echo '<br><br>';

    print_r(returnTweet());


?>

1 个答案:

答案 0 :(得分:1)

user_timeline只会返回screen_name值中指定的用户发布的推文。您需要阅读mentions_timeline,并且需要按照上述用户进行身份验证,才能看到提及。