вторник, 22 декабря 2009 г.

Youtube API, Поиск по видео

Вот столкнулся недавно с задачей, поиска видео на Youtube из моего приложения.

Как оказалось задача проста. В Youtube Api с использованием PHP все вызовы идут с использованием Zend Framewokr.

Вот собственно функция для поиска видео:

function searchAndPrint($searchTerms)
{
  $yt = new Zend_Gdata_YouTube(); 
  $query = $yt->newVideoQuery();
  $query->setOrderBy('viewCount');
  $query->setRacy('include');
  $query->setVideoQuery($searchTerms);
  $videoFeed = $yt->getVideoFeed($query);
  printVideoFeed($videoFeed, 'Search results for: ' . $searchTerms);
}

Где функция вывода на экран (printVideoFeed):

function printVideoFeed($videoFeed, $displayTitle = null) 
{
  $count = 1;
  if ($displayTitle === null) {
    $displayTitle = $videoFeed->title->text;
  }
  echo '

' . $displayTitle . "

\n"; echo "
\n";
  foreach ($videoFeed as $videoEntry) {
    echo 'Entry # ' . $count . "\n";
    printVideoEntry($videoEntry);
    echo "\n";
    $count++;
  }
  echo "
\n"; }
 
 
function printVideoEntry($videoEntry, $tabs = "") 
{
  // the videoEntry object contains many helper functions that access the underlying mediaGroup object
  echo $tabs . 'Video: ' . $videoEntry->getVideoTitle() . "\n";
  echo $tabs . "\tDescription: " . $videoEntry->getVideoDescription() . "\n";
  echo $tabs . "\tCategory: " . $videoEntry->getVideoCategory() . "\n";
  echo $tabs . "\tTags: " . implode(", ", $videoEntry->getVideoTags()) . "\n";
  echo $tabs . "\tWatch page: " . $videoEntry->getVideoWatchPageUrl() . "\n";
  echo $tabs . "\tFlash Player Url: " . $videoEntry->getFlashPlayerUrl() . "\n";
  echo $tabs . "\tDuration: " . $videoEntry->getVideoDuration() . "\n";
  echo $tabs . "\tView count: " . $videoEntry->getVideoViewCount() . "\n";
  echo $tabs . "\tRating: " . $videoEntry->getVideoRatingInfo() . "\n";
  echo $tabs . "\tGeo Location: " . $videoEntry->getVideoGeoLocation() . "\n";
  
  // see the paragraph above this function for more information on the 'mediaGroup' object
  // here we are using the mediaGroup object directly to its 'Mobile RSTP link' child
 foreach ($videoEntry->mediaGroup->content as $content) {
    if ($content->type === "video/3gpp") {
      echo $tabs . "\tMobile RTSP link: " . $content->url . "\n";
    }
  }
  
  echo $tabs . "\tThumbnails:\n";
  $videoThumbnails = $videoEntry->getVideoThumbnails();

  foreach($videoThumbnails as $videoThumbnail) {
    echo $tabs . "\t\t" . $videoThumbnail['time'] . " - " . $videoThumbnail['url'];
    echo " height=" . $videoThumbnail['height'];
    echo " width=" . $videoThumbnail['width'];
    echo "\n";
  }
} 

Вот собственно и все. А дальше уже оформляйте вывод как вам нравится:). Удачи

Комментариев нет: