获得响应时该怎么办yt:too_many_calls?

时间:2014-06-01 17:04:30

标签: php youtube-api

我正在尝试更新viewcount,在我的桌子上的所有行上,但在它击中第200行后我开始得到响应," yt:too_many_calls",我该怎么做才能解决这个问题问题,这是我的代码,它为表中的每一行运行。我几乎把所有事情都累了。

<?php
    class youtube {
        public $channel;
        //Json for user
        public $data = null;
        public $viewCount;
        public function __construct () {
            $channel = $this->channel;
            $this->data = file_get_contents('http://gdata.youtube.com/feeds/api/users/' . $channel . '?v=2&alt=json&key=MY_DEV_KEY');
            $this->data = json_decode($this->data, true);
            $this->viewCount = $this->data['entry']['yt$statistics']['totalUploadViews'];
            return $this;
        }
    }
?>

我也累了添加这个,但没有运气

   if($this->data = 'yt:too_many_calls')
 {

    $timeMain = pow(2, $error_count);
    $timeSec = rand(1000000, 1000000000);
    time_nanosleep($timeMain, $timeSec);

 }

这就是我的代码每行循环的方式

    $query = $handler->query("SELECT * FROM channels LIMIT 400");     
    $query->setFetchMode(PDO::FETCH_CLASS , 'youtube');

1 个答案:

答案 0 :(得分:0)

“呼叫太多”,然后检查API控制台。也许在你的每日限制,因为也有“太多最近的电话”。 你需要设置更长的睡眠时间。

sleep(600); // sleep for 10 minutes.

来自YT博客:

如果您收到过多API请求的标记,您将收到代码为403的HTTP响应和包含

的响应正文
<errors><error><domain>yt:quota</domain><code>too_many_recent_calls</code></error></errors>

由您的代码来检测此错误并采取适当的措施。作为最佳做法,我们建议您在收到此类错误后10分钟停止应用程序中的所有API调用,以“重置”您的配额。当您恢复进行API调用时,您应该缩减请求的频率。通常,不是来自用户交互的API请求 - 例如,作为后台同步或批量导入过程的一部分而一个接一个地发出的请求 - 更有可能触发配额违规,因此应该避免它们。

参考:http://apiblog.youtube.com/2010/02/best-practices-for-avoiding-quota.html

更新: 首先,我不使用PDO。但是,由于计划是在呼叫之间增加等待时间,我认为在循环通过YouTube请求之前,您应首先将数据从数据库提取到数组中。这样您就可以更快地断开它。类似的东西:

    // pseudo code, to be changed.
 1) Save channel ID's from database into an array: $channelArray = Array();
 2) Loop thru this array, making a YouTube API request for each row.
 3) Set a limit for repeating the SAME request AGAIN: $maxLoopCount = 3;
 4) REPEAT "only for a quota error" AND "only for a limited consecutive times":
    FOREACH ($channelArray as $entry)
      { - INIT: $sameRequestLoopCounter = 0;
        - DO {
               - Make the YouTube API request for $entry[channelId];
               - $sameRequestLoopCounter++;
               - Check the response after each call:
                      {
                       - $responseText = ?;
                       - $errorMessage = ?; 
                       - Only if there is a quota error in the response data OR error message, then
                              { Wait 10 minutes. And then do the same API call again.}
                                With some timing, the wait time could be changed.
                          // Does Response OR Error message contains string "too_many_recent_calls": TRUE or FALSE:
                       - $check1 = strpos($responseText,"too_many_recent_calls");
                       - $check2 = strpos($errorMessage,"too_many_recent_calls");
                       - if ( ($check1 == 0) AND ($check2 == 0) )
                            $quotaError = FALSE;
                         else 
                            $quotaError = TRUE;
                        - if (other kind of error)
                             {
                              print $errorMessage;
                              BREAK 2;  // quit loops
                             }
                      }
               - if ($quotaError)
                   {
                     print "Waiting some time.";
                     sleep (600);
                   }
              }
                WHILE  ( ($sameRequestLoopCounter < $maxLoopCount) AND $quotaError );
        - Use the API response data for something;
     }