使用 YouTube API 抓取超过 1000 个结果的数据,而无需使用最大内存


Grabbing data using the YouTube API over 1000 results and without using the Max Memory

我正在尝试获取整个频道的视频源(所有视频数据)并将其存储到我的MySQL数据库中,以便在我目前正在处理的应用程序中使用。我对YouTube API不是最有经验的。我正在使用的代码如下:

public function printVideoFeed($count)
{
    $this->startIndex($count);
    $data = $this->yt->getVideoFeed($this->query);
    foreach($data as $video)
    {
        echo $count .' - '.$video->getVideoTitle().'<br/>';
        $count++;
    }
    //check if there are more videos
    try{
        $nextFeed = $data->getNextFeed();
    } catch(Zend_Gdata_App_Exception $e)
    {
        echo $e->getMessage(). '<br/>';
    }
    if($nextFeed)
    {
        $this->printVideoFeed($count);
    }
}

我得到的错误是:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 36 bytes) in C:'Program Files'ZendFrameworkCli'library'Zend'Gdata'App'Base.php on line 431

这是我在尝试抓取 3000 多个视频时遇到的几个错误之一。我的问题是,我怎样才能在继续再次执行 printVideoFeed 方法的同时不继续扩展内存使用量。如果有办法让它脱离循环,但如果还有视频,那就太好了。我一直在寻找,但谷歌这个问题是一件很难的事情(为了得到我正在寻找的结果)。

您是否尝试过使用迭代而不是递归?我可以想象PHP可能会保留函数中声明的变量,即特别是$data,直到函数离开。或者,您可以在开始递归之前调用unset($data);

另外:你确定你没有无限循环吗?也许您需要在致电getNextFeed()之前再次致电startIndex()