Created
October 2, 2012 06:08
-
-
Save amereservant/3816646 to your computer and use it in GitHub Desktop.
Youtube API - Keywords
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<head> | |
<meta charset="utf-8" /> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" /> | |
<title>Youtube API Test</title> | |
<meta name="description" content="" /> | |
<meta name="author" content="" /> | |
<meta name="viewport" content="width=device-width,initial-scale=1" /> | |
</head> | |
<body> | |
<?php | |
require 'init.php'; | |
/** | |
* Get Youtube Playlist Thumbnails | |
* | |
* @param object $entry The playlistEntry object to return thumbnails for | |
* @return array An array of thumbnails, varying in size | |
*/ | |
function getPlaylistThumbnail( $entry ) | |
{ | |
$mediaArray = array(); | |
$extEls = $entry->getExtensionElements(); | |
foreach( $extEls as $extEl ) | |
{ | |
if( $extEl->rootNamespaceURI == 'http://search.yahoo.com/mrss/' && $extEl->rootElement == 'group' ) | |
{ | |
// this is a media:group element | |
$mediaExtEls = $extEl->getExtensionElements(); | |
$i = 0; | |
foreach( $mediaExtEls as $mediaExtEl ) | |
{ | |
$elName = $mediaExtEl->rootElement; | |
// Filter for thumbnail elements only | |
if( $elName == 'thumbnail' ) | |
{ | |
$mediaArray[$elName][$i] = array(); | |
$attrs = $mediaExtEl->getExtensionAttributes(); | |
// Loop over attributes and group them based on thumbnail quality | |
foreach ($attrs as $attrName=>$attrVal) | |
{ | |
// Find the quality key name | |
if( $attrName == 'http://gdata.youtube.com/schemas/2007:name' ) | |
{ | |
$quality = $attrs['http://gdata.youtube.com/schemas/2007:name']['value']; | |
$mediaArray[$elName][$i]['quality'] = $quality; | |
} | |
else { | |
$mediaArray[$elName][$i][$attrName] = $attrVal['value']; | |
} | |
} | |
} | |
$i++; | |
} | |
} | |
} | |
// Return empty array if no thumbnails were found | |
if( !isset($mediaArray['thumbnail']) ) | |
return $mediaArray; | |
return $mediaArray['thumbnail']; | |
} | |
$yt = new Zend_Gdata_YouTube(null, 'MyAPIClient', null, YOUTUBE_DEV_KEY); | |
$yt->setMajorProtocolVersion(2); | |
$playlists = $yt->getPlaylistListFeed( YOUTUBE_USER ); | |
$thumbf = '<strong>%s - %d x %d</strong> <img src="%s" width="%2$d" height=%3$d" /> '; | |
foreach($playlists as $list) | |
{ | |
echo "<h3>{$list->title->text}</h3>"; | |
echo "<p><strong>URL:</strong> {$list->getPlaylistVideoFeedUrl()}<br />"; | |
echo "<strong>ID:</strong> {$list->getPlaylistId()}<br />"; | |
foreach( getPlaylistThumbnail($list) as $thumb ) | |
{ | |
printf($thumbf, $thumb['quality'], $thumb['width'], $thumb['height'], $thumb['url']); | |
} | |
echo '</p><br />'; | |
} | |
$playlist = $playlists[1]; | |
$vids = $yt->getVideoFeed($playlist->getPlaylistVideoFeedUrl()); | |
foreach( $vids as $video ) | |
{ | |
var_dump($video->getVideoTags()); // Always an empty array | |
} | |
?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment