Wednesday, May 16, 2012

Working PHP Youtube API


Thank you Google for a direct API code !

<?php

$q = "KEYWORD HERE";

require_once 'Zend/Loader.php'; // the Zend dir must be in your include_path

Zend_Loader::loadClass('Zend_Gdata_YouTube');
$yt = new Zend_Gdata_YouTube();
$yt->setMajorProtocolVersion(2);

function printVideoFeed($videoFeed)
{
  $count = 1;
  foreach ($videoFeed as $videoEntry) {
    //echo "Entry # " . $count . "<br /><br /><br />";
    echo "<br /><br /><br />";
    printVideoEntry($videoEntry);
    echo "\n";
    $count++;
  }
}

function printVideoEntry($videoEntry)
{
  // the videoEntry object contains many helper functions
  // that access the underlying mediaGroup object
  echo  $videoEntry->getVideoTitle() . "<br />";
  echo 'Description: ' . $videoEntry->getVideoDescription() . "<br />";
  echo 'Category: ' . $videoEntry->getVideoCategory() . "<br />";
  echo 'Tags: ' . implode(", ", $videoEntry->getVideoTags()) . "<br />";
  echo '<a href="' . $videoEntry->getVideoWatchPageUrl() . '">'.$videoEntry->getVideoTitle().'</a><br />';
  echo '<iframe width="420" height="315" src="' . $videoEntry->getFlashPlayerUrl() .'" frameborder="0" allowfullscreen></iframe>';

}

$query = $yt->newVideoQuery();
  $query->setSafeSearch('moderate');
  $query->setMaxResults(5);
  $query->setVideoQuery($q);

  // Note that we need to pass the version number to the query URL function
  // to ensure backward compatibility with version 1 of the API.
  $videoFeed = $yt->getVideoFeed($query->getQueryUrl(2));
  printVideoFeed($videoFeed, 'Search results for: ' . $q);
?>

No comments:

Post a Comment