Twitter API screen_name和名称不仅仅是文本

时间:2017-08-31 01:11:17

标签: php api twitter

我使用twitter API来获取标签的推文信息。我能够使用数组中的[text]来获取推文,但是当我使用[screen_name]获取用户名和[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


        $request = array(
            'screen_name'       => 'matthutchings95',
            'count'             => '10'
        );

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


        $oauth = array_merge($oauth, $request);


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


        $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 "Name: " .$tweet[0]["name"];
    echo '<br><br>';
    echo "Screen Name: " .$tweet[0]["screen_name"];
    echo '<br><br>';
    echo "Text: " .$tweet[0]["text"];
    echo '<br><br>';
    echo "Name: " .$tweet[1]["name"];
    echo '<br><br>';
    echo "Screen Name: " .$tweet[1]["screen_name"];
    echo '<br><br>';
    echo "Text: " .$tweet[1]["text"];
    echo '<br><br>';
    echo "Name: " .$tweet[2]["name"];
    echo '<br><br>';
    echo "Screen Name: " .$tweet[2]["screen_name"];
    echo '<br><br>';
    echo "Text: " .$tweet[2]["text"];
    echo '<br><br>';
    echo '<br><br>';
    echo '<br><br>';
    echo '<br><br>';
    print_r(returnTweet());

?>

1 个答案:

答案 0 :(得分:2)

试试这个:

echo "Screen Name: " .$tweet[0]["user"]["screen_name"];
echo "Screen Name: " .$tweet[0]["user"]["name"];

这是您需要的doc