在PHP中使用Twitter搜索时出错

时间:2012-04-22 19:09:20

标签: php twitter

在搜索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;
                    }
                }
            }

这是我的代码,请看一下,找到我正在讨论的确切解决方案

2 个答案:

答案 0 :(得分:1)

使用Twitter API有一定的限制。 如果您在共享服务器上测试该代码(意味着如果您的ip正由其他使用Twitter API的人使用)可能导致此问题。 或者你提出了太多要求......

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

  

搜索API速率限制

     

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

     

Search API的要求

     

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

     

超出Search API速率限制的应用程序   将收到HTTP 420响应代码。这是观看的最佳做法   对于此错误条件并尊重Retry-After标头   回。 Retry-After标头的值是您的秒数   应用程序应在从Search API请求日期之前等待   试。

     

使用Streaming API

     

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

     

避免被限价的提示

     

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

     

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

     

缓存

     

如果您愿意,可以在您的应用程序或您的网站上存储API响应   很多用途。例如,不要试图在每一个上调用Twitter API   页面加载您网站的目标网页。相反,请调用API   不经常将响应加载到本地缓存中。用户点击时   您的网站加载了结果的缓存版本。

     

优先考虑活跃用户

     

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

     

适应搜索结果

     

如果您的应用程序监控大量搜索字词,请少查询   通常用于没有结果的搜索。通过   使用后退功能,您可以及时了解受欢迎的查询   但不要浪费周期来请求很少改变的查询。

     

或者,考虑使用Streaming API并过滤您的   搜索条件。

答案 1 :(得分:0)

如果您想绕过Twitter限制,那么您要么获得许多具有不同IP的计算机并在其中分发搜索请求,以便在不同时间调用它们,或者将查询移动到客户端(假设您有很多客户看,并希望他们提高你的带宽,虽然它不会那么安全。