【问题标题】:cant fetch recent tweets of a user无法获取用户最近的推文
【发布时间】:2017-03-15 20:34:23
【问题描述】:

当我打印 $tweets 时,它给了我一个错误的数组

[body] => {"errors":[{"message":"抱歉,该页面不存在","code":34}]}

private function fetch_tweets($tweet_count,$username){
    $tweets = wp_remote_get("http://twitter.com/statuses/user_timeline/$username.json");

    print_r($tweets);

    if(isset($tweet->error) ) return false;

    foreach($tweets as $tweet){
        if($tweet_count-- === 0) break;
        echo $tweet->text;
    }
}

【问题讨论】:

  • 您似乎在尝试获取不正确的 URL,而不是向正确的 API 端点发送适当的参数。我可以建议参考Twitter API documentation for GET statuses/user_timeline
  • 这不是你代码中的拼写错误吗? usern_timeline(用户中的 n)。
  • 对不起,我的错..这是错字..

标签: api twitter tweets


【解决方案1】:

正如 ardavey 所说,您需要阅读API Documentation

示例请求

GET https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=twitterapi&count=2

所以,对于你的 PHP:

$tweets = wp_remote_get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=$username");

另外,您不需要验证您的请求:

请注意,API 要求对请求进行身份验证(查看身份验证和授权文档...

请参阅Auth Overview 以开始使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-13
    • 1970-01-01
    • 2014-05-03
    • 2016-08-28
    • 2021-02-25
    • 2017-04-11
    • 2015-08-27
    • 2013-06-03
    相关资源
    最近更新 更多