2016-05-19 72 views
3

我正在開發一個簡單的函數來獲取我的推特追隨者的ID。我可以使用oauth登錄並獲取結果,但是我無法獲取標題信息,因此我可以使用標題中的x_rate_limit_remaining值。 功能是:PHP網站 - Twitter的API +亞伯拉罕Oauth(無法找到x_rate_limit_remaining)

function get_id_of_followers() { 
     $access_token = $_SESSION['access_token']; 
     $connection = new TwitterOauth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']); 
     $data0 = get_class_methods($connection); 
     echo "<pre> D0 ", print_r($data0, true), "</pre>"; 
     $data1 = $connection->getLastXHeaders(); 
     echo "<pre> D1 ", print_r($data1, true), "</pre>"; 
     $data2 = $connection->getLastXHeaders()["x_rate_limit_remaining"]; 
// x_rate_limit_remaining is an entity in the headers that I am able to see with my python scripts in headers output. 
     echo "<pre> D2 ", $data2, "</pre>"; 
     $data3 = $connection->get('followers/ids'); 
     echo "<pre> D3 ", print_r($data3, true), "</pre>"; 
    } 

從函數的輸出如下:

D0 
Array 
(
    [0] => __construct 
    [1] => setOauthToken 
    [2] => getLastApiPath 
    [3] => getLastHttpCode 
    [4] => getLastXHeaders 
    [5] => getLastBody 
    [6] => resetLastResponse 
    [7] => url 
    [8] => oauth 
    [9] => oauth2 
    [10] => get 
    [11] => post 
    [12] => delete 
    [13] => put 
    [14] => upload 
    [15] => setTimeouts 
    [16] => setDecodeJsonAsArray 
    [17] => setUserAgent 
    [18] => setProxy 
    [19] => setGzipEncoding 
) 


D1 
Array 
(
) 


D2 

D3 
stdClass Object 
(
    [ids] => Array 
     (
      [0] => 730017479360045056 
      [1] => 4716372642 
      [2] => 709411090980020224 
      [3] => 4905437529 
      [4] => 709964576868200449 
      [5] => 228648311 
      [6] => 3190604329 
      [7] => 4298659035 
      [8] => 378112124 
      [9] => 4554579372 
      [10] => 3845238492 
      [11] => 3009354738 
records retrived upto 5000 

現在我能得到的迴應(D3),但是,爲什麼我的頭(D2)陣列返回爲空?我錯過了什麼嗎? (問題也發佈在https://github.com/abraham/twitteroauth/issues/469

回答

3

您在提出請求之前嘗試獲取標頭時犯了一個很小的錯誤。試試這個代碼(我從代碼中刪除所有不必要的東西)

function get_id_of_followers() { 
     $access_token = $_SESSION['access_token']; 
     $connection = new TwitterOauth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']); 
     $Body = $connection->get('followers/ids'); 
     $Header = $connection->getLastXHeaders(); 
     echo "<pre> Body: ", print_r($Body, true), "</pre>"; 
     echo "<pre> Header: ", print_r($Header, true), "</pre>"; 
     echo "<pre> x_rate_limit_remaining: ", $Header[x_rate_limit_remaining], "</pre>"; 
} 

所有你所要做的就是打電話給$connection->getLastXHeaders();$connection->get('followers/ids');電話後的事情工作。

+0

怪胎!這件事情就像一個魅力..! 我正在嘗試在進行http調用之前打印http標題。什麼是絕對的錯誤。 –

相關問題