2017-08-30 78 views
0

我已經設置了twitter API來獲取用戶名爲matthutchings95的推文,並且在這個帳戶上我發佈了10次測試推文。現在頁面正在讀取這些罰款,但我然後做了第二個Twitter帳戶dannybots111,並寫了一條提及第一個帳戶的推文。但是,這並沒有顯示在網頁上,我必須將screen_name更改爲其他內容?我認爲這是從該帳戶中提及的推文和推文。Twitter API沒有被提及的帖子

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

user_timeline只會返回鳴叫發表在screen_name值指定的用戶。您需要閱讀mentions_timeline,並且需要按照提及的用戶進行身份驗證才能看到提及。