【问题标题】:TwitterAPIExchange suddenly stops workingTwitterAPIExchange 突然停止工作
【发布时间】:2015-08-23 20:59:56
【问题描述】:

所以我正在制作一个 Twitter 获取网站,其中来自特定主题标签的所有推文都将使用 ajax 实时获取。我想,一切都很好,直到突然停止工作。如果我再等 3 分钟左右,它就会重新开始工作。 PHP 给了我诸如

之类的错误

注意:正在尝试获取非对象的属性...

我正在运行的代码:

require_once '../lib/TwitterAPIExchange.php';

$settings = array(
    'oauth_access_token' => "...",
    'oauth_access_token_secret' => "...",
    'consumer_key' => "...",
    'consumer_secret' => "..."
);

$url = 'https://api.twitter.com/1.1/search/tweets.json';
$getfield = '?q=#hashtag&result_type=recent';
$requestMethod = 'GET';

$twitter = new TwitterAPIExchange($settings);
$response = $twitter->setGetfield($getfield)
                             ->buildOauth($url, $requestMethod)
                             ->performRequest();

foreach(json_decode($response) as $result){
    foreach($result as $post){
        echo '<div class="post"><div class="header"><img src="'.$post->user->profile_image_url.'"><p class="user">'.$post->user->name.'</p></div><div class="text">'.$post->text.'</div></div>';
    }
    break;
}

【问题讨论】:

  • 也许您在彼此之后提出请求太快了?所以也许你正在经历 DOS 保护。
  • 你检查过 PHP 试图解析的值吗?这可能是来自 Twitter 的错误消息。

标签: php ajax json twitter


【解决方案1】:

我在@Uwe Allner 的帮助下想通了。原来 Twitter API 有一个叫做 API Rate Limits 的东西。因此,正如您可能猜到的那样,它们限制了每 x 时间的请求数量。在“搜索”下,我注意到他们设置了每 15 分钟 180 个查询的限制。所以我可以每 5 秒查询一次。

180/15 = 12
60/12 = 5

【讨论】:

    猜你喜欢
    • 2017-08-22
    • 2021-02-01
    • 2016-12-29
    • 2018-05-09
    • 2015-12-07
    • 2014-02-05
    • 2013-09-06
    • 1970-01-01
    相关资源
    最近更新 更多