【问题标题】:Got error using Twitter Search in PHP在 PHP 中使用 Twitter 搜索时出错
【发布时间】:2012-05-02 05:19:13
【问题描述】:

在搜索 2 或 3 后使用 PHP 在 Twitter 上搜索时,我收到了您的错误是速率限制。增强您在 PHP 中的冷静

 function twitter_class()
{
    $this->realNamePattern = '/\((.*?)\)/';


$this->intervalNames   = array('second', 'minute', 'hour', 'day', 'week', 'month', 'year');
$this->intervalSeconds = array( 1,        60,       3600,   86400, 604800, 2630880, 31570560);

    $this->badWords = array('somebadword', 'anotherbadword');
}
function searchURL($language, $query) {
    $func_args=func_get_args();
    $vars = array_map('urlencode', $func_args + array('', ''));        
    return vsprintf('http://search.twitter.com/search.atom?lang=%s&q=%s', $vars);
}

function getTweets($q,$lang, $limit=15)
{
    $output = '';

    // get the seach result

   $ch = curl_init($this->searchURL($lang, $q));


    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);

    $response = curl_exec($ch);

    if ($response !== FALSE)
    {
        $xml = simplexml_load_string($response);

        $output = '';
        $tweets = 0;

        for($i=0; $i<count($xml->entry); $i++)
        {
            $crtEntry = $xml->entry[$i];
            $account  = $crtEntry->author->uri;
            $image    = $crtEntry->link[1]->attributes()->href;
            $tweet    = $crtEntry->content;

            // skip tweets containing banned words
            $foundBadWord = false;
            foreach ($this->badWords as $badWord)
            {
                if(stristr($tweet, $badWord) !== FALSE)
                {
                    $foundBadWord = true;
                    break;
                }
            }

            $tweet = str_replace('<a href=', '<a target="_blank" href=', $tweet);

            // skip this tweet containing a banned word
            if ($foundBadWord)
                continue;

            // don't process any more tweets if at the limit
            if ($tweets==$limit)
                break;
            $tweets++;

            // name is in this format "acountname (Real Name)"
            preg_match($this->realNamePattern, $crtEntry->author->name, $matches);
            $name = $matches[1];

            // get the time passed between now and the time of tweet, don't allow for negative
            // (future) values that may have occured if server time is wrong
            $time = 'just now';
            $secondsPassed = time() - strtotime($crtEntry->published);

            if ($secondsPassed>0)
            {
                // see what interval are we in
                for($j = count($this->intervalSeconds)-1; ($j >= 0); $j--)
                {
                    $crtIntervalName = $this->intervalNames[$j];
                    $crtInterval = $this->intervalSeconds[$j];

                    if ($secondsPassed >= $crtInterval)
                    {
                        $value = floor($secondsPassed / $crtInterval);
                        if ($value > 1)
                            $crtIntervalName .= 's';

                        $time = $value . ' ' . $crtIntervalName . ' ago';

                        break;
                    }
                }
            }

这是我的代码,请查看它并找到我滞后的确切解决方案

【问题讨论】:

    标签: php twitter


    【解决方案1】:

    使用 Twitter API 没有限制。 如果您在共享服务器上测试该代码(意味着您的 ip 是否被使用 twitter API 的其他人使用),则可能导致此问题。 或者你的要求太多了……

    https://dev.twitter.com/docs/rate-limiting#search

    搜索 API 速率限制

    对托管在 search.twitter.com 上的 Search API 的请求不计算在内 达到 REST API 限制。但是,所有来自 IP 的请求 地址应用于搜索速率限制。搜索率限制 不公开以阻止不必要的搜索使用和滥用, 但它高于 REST 速率限制。我们感觉到搜索率 限制对于大多数应用程序来说既宽松又足够,并且知道 许多应用程序供应商发现它适合他们的需求。

    搜索 API 的要求

    搜索 API 的使用要求应用程序包含唯一且 识别用户代理字符串。 HTTP 引荐来源网址是预期的,但不是 必需的。使用 Search API 但未包含 用户代理字符串将收到较低的速率限制。

    超出 Search API 速率限制的应用程序 将收到 HTTP 420 响应代码。观看是最佳实践 对于这种错误情况并尊重 Retry-After 标头,它是 回来。 Retry-After 标头的值是您的秒数 应用程序应在从 Search API 请求日期之前等待 再次。

    改用流式 API

    搜索 API 最适合用户发起的即席查询。如果 您的应用程序需要重复的 Search API 轮询,您可能希望 改为考虑 Streaming API。

    避免被速率限制的提示

    以下提示可帮助您进行防御性编码并减少 可能会受到速率限制。

    您可能想要提供的一些应用程序功能很简单 鉴于速率限制是不可能的,尤其是在新鲜度方面 结果。如果实时信息是您的应用程序的目标, 查看用户流或站点流。

    缓存

    将 API 响应存储在您的应用程序中或您的网站上(如果您希望的话) 很多用处。例如,不要尝试在每个 您的网站登陆页面的页面加载。而是调用 API 不经常将响应加载到本地缓存中。当用户点击 您的网站会加载结果的缓存版本。

    优先考虑活跃用户

    如果您的网站跟踪许多 Twitter 用户(例如,获取 他们目前的状态或关于他们的 Twitter 使用情况的统计数据), 只考虑为最近登录的用户请求数据 您的网站。

    适应搜索结果

    如果您的应用程序监控大量搜索字词,请减少查询 通常用于没有结果的搜索而不是有结果的搜索。经过 使用回退,您可以及时了解流行的查询 但不要浪费周期请求很少更改的查询。

    或者,考虑使用 Streaming API 并在您的 搜索词。

    【讨论】:

    • 如何避免不受 Twitter 的限制 有没有 PHP 代码?
    • 你不能,你只需要发出更少的请求。正如文档所说,最佳做法是在“Retry-After”秒过去之前不要再拨打电话。 It is best practice to watch for this error condition and honor the Retry-After header which is returned. The Retry-After header's value is the number of seconds your application should wait before requesting date from the Search API again.
    【解决方案2】:

    如果您想绕过 twitter 限制,那么您要么获得大量具有不同 IP 的机器并在它们之间分发搜索请求,以便在不同时间调用它们,要么将查询移动到客户端(假设您有很多客户正在寻找)并希望他们提高您的带宽,尽管它不会那么安全。

    【讨论】:

      猜你喜欢
      • 2017-11-06
      • 2015-08-15
      • 1970-01-01
      • 2015-01-14
      • 2016-07-29
      • 2020-06-05
      • 1970-01-01
      • 1970-01-01
      • 2020-07-09
      相关资源
      最近更新 更多