Search code examples
youtube-apizend-gdata

Mismatch between reported totalResults from video feen and actual total in YouTube PHP API?


I'm using the Zend Youtube API. I do a query for a feed, which reports it has a total of 886 entries, but when I fetch all the feeds and iterate, the actual number is like 782.

Is there actually a mismatch, or is there some foolish error in my code I've overlooked?

Snippet (debug() is just a convenience reporting function):

$query = $yt->newVideoQuery($queryUrl);
$query->setMaxResults(50); //Max allowed, to reduce API hits
$feeds = array();
try {
  $feed = $yt->getVideoFeed($query);
  $totalResults = $feed->getTotalResults(); //For debug
  $feeds[] = $feed;
  $totalFromFeeds = sizeof($feed); //For debug
  while ($feed = $feed->getNextFeed()) {
    $feeds[] = $feed;
    $totalFromFeeds += sizeof($feed); //For debug
  }
} catch (Zend_Gdata_App_Exception $e) {
  debug("$e"); //For debug
  //Just means no more feeds; do nothing
}
$numFeeds = sizeof($feeds); //For debug

$entries = array();
foreach ($feeds as $feed) {
  foreach ($feed as $entry) {
    $entries[] = $entry;
  }
}
$numEntries = sizeof($entries); //For debug
debug("According to feed, [$totalResults]; sizeof entries = [$numEntries] 
Num Feeds: $numFeeds;   totalfromfeeds: $totalFromFeeds");

Output from debug:

exception 'Zend_Gdata_App_Exception' with message 'No link to next set of results found.
According to feed, [886]; sizeof entries = [782] 
Num Feeds: 16;   totalfromfeeds: 782

Thanks for any pointers,

Chris


Solution

  • The <openSearch:totalResults> value is just an estimate. See the docs for more information: https://developers.google.com/youtube/2.0/reference#Total_Result_Counts