【问题标题】:PHP Website - Twitter API + Abraham Oauth (Unable to find x_rate_limit_remaining )PHP 网站 - Twitter API + Abraham Oauth(无法找到 x_rate_limit_remaining )
【发布时间】:2016-09-15 18:50:56
【问题描述】:

我正在开发一个简单的函数来获取我的推特追随者的 ID。我可以使用 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

现在我可以得到 response(D3) 但是,为什么我的 headers(D2) 数组返回为 null ?我错过了什么吗? (问题也发布在https://github.com/abraham/twitteroauth/issues/469

【问题讨论】:

    标签: php api twitter oauth twitter-oauth


    【解决方案1】:

    您犯了一个很小的错误,即在发出请求之前尝试获取标头。试试这段代码(我正在从代码中删除所有不需要的东西)

    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-&gt;get('followers/ids'); 之后拨打$connection-&gt;getLastXHeaders(); 即可。

    【讨论】:

    • 怪胎!!这东西就像一个魅力..!!在进行 http 调用之前,我试图打印 http 标头。真是大错特错。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-26
    • 1970-01-01
    • 2012-03-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多