Kleine Einführung in die YouTube API
So, heute ist die YouTube API dran, nachdem ich letzte Woche die SlideShare API vorgestellt hatte. Mit Hilfe der YouTube Data API ist es möglich Videos hochzuladen, nach Videos zu suchen und Kommentare abzurufen. Man kann sich mit der API auch “einloggen” und Dinge tun die man als eingeloggter User machen kann, beispielsweise als dieser User Videos hochladen, Playlisten bearbeiten usw.
Da wir uns die Hände nicht unnötig schmutzig machen wollen nutzen wir wieder Zend-Klassen dafür. Ich zeige hier die Verwendung von Zend_Gdata_YouTube, wenn man das ganze Zend Framework scheut kann man die Zend_Gdata Klassen aber auch einzeln downloaden. PHP 5.1.4 und Zend_Gdata > 1.7.7 werden aktuell benötigt.
Für die öffentlichen Funktionen benötigen wir keine Authentifizierung, wir können direkt loslegen. Wenn die YouTube VideoID bekannt ist kann man Informationen zum betreffenden Video wie folgt abrufen:
<? // loader or autoloader 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); $entry = $yt->getVideoEntry('oebqlzblfyo'); printVideoEntry($entry);
Die printVideoEntry() Funktion zeigt in diesem Fall Informationen des Zend_Gdata_YouTube_VideoEntry Objekts an:
function printVideoEntry($videoEntry) { // the videoEntry object contains many helper functions // that access the underlying mediaGroup object echo 'Video: ' . $videoEntry->getVideoTitle() . "\n"; echo 'Video ID: ' . $videoEntry->getVideoId() . "\n"; echo 'Updated: ' . $videoEntry->getUpdated() . "\n"; echo 'Description: ' . $videoEntry->getVideoDescription() . "\n"; echo 'Category: ' . $videoEntry->getVideoCategory() . "\n"; echo 'Tags: ' . implode(", ", $videoEntry->getVideoTags()) . "\n"; echo 'Watch page: ' . $videoEntry->getVideoWatchPageUrl() . "\n"; echo 'Flash Player Url: ' . $videoEntry->getFlashPlayerUrl() . "\n"; echo 'Duration: ' . $videoEntry->getVideoDuration() . "\n"; echo 'View count: ' . $videoEntry->getVideoViewCount() . "\n"; echo 'Rating: ' . $videoEntry->getVideoRatingInfo() . "\n"; echo 'Geo Location: ' . $videoEntry->getVideoGeoLocation() . "\n"; echo 'Recorded on: ' . $videoEntry->getVideoRecorded() . "\n"; // see the paragraph above this function for more information on the // 'mediaGroup' object. in the following code, we use the mediaGroup // object directly to retrieve its 'Mobile RSTP link' child foreach ($videoEntry->mediaGroup->content as $content) { if ($content->type === "video/3gpp") { echo 'Mobile RTSP link: ' . $content->url . "\n"; } } echo "Thumbnails:\n"; $videoThumbnails = $videoEntry->getVideoThumbnails(); foreach($videoThumbnails as $videoThumbnail) { echo $videoThumbnail['time'] . ' - ' . $videoThumbnail['url']; echo ' height=' . $videoThumbnail['height']; echo ' width=' . $videoThumbnail['width'] . "\n"; } }
Die Ausgabe sieht dann so aus:
Video: Velocity Europe, Artur Bergman, "Full Stack Awareness" Video ID: oebqlzblfyo Updated: 2011-12-08T16:06:11.000Z Description: Artur Bergman VP Engineering and Operations, Wikia/Fastly Performance and operability doesn't come from simply focusing on a single part of your application or infrastructure. They come from having a systemic view of what makes your stack work, what could bottleneck it, and what could bring it down. I'll talk about this journey from kernel to continents. Category: Tech Tags: velocityconf, europe, 2011 Watch page: http://www.youtube.com/watch?v=oebqlzblfyo&feature=youtube_gdata_player Flash Player Url: http://www.youtube.com/v/oebqlzblfyo?version=3&f=videos&app=youtube_gdata Duration: 1090 View count: 2883 Rating: Array Geo Location: Recorded on: Mobile RTSP link: rtsp://v8.cache2.c.youtube.com/CiILENy73wIaGQkqf-U2l-rmoRMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp Mobile RTSP link: rtsp://v4.cache3.c.youtube.com/CiILENy73wIaGQkqf-U2l-rmoRMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp Thumbnails: 00:09:05 - http://i.ytimg.com/vi/oebqlzblfyo/default.jpg height=90 width=120 - http://i.ytimg.com/vi/oebqlzblfyo/hqdefault.jpg height=360 width=480 00:04:32.500 - http://i.ytimg.com/vi/oebqlzblfyo/1.jpg height=90 width=120 00:09:05 - http://i.ytimg.com/vi/oebqlzblfyo/2.jpg height=90 width=120 00:13:37.500 - http://i.ytimg.com/vi/oebqlzblfyo/3.jpg height=90 width=120
Es ist auch möglich einige Standard-Feeds abzurufen, wie beispielsweise recently_features, most_viewed, top_rated, top_favorites usw. Und das geht so:
printVideoFeed($yt->getRecentlyFeaturedVideoFeed());
Die printVideoFeed() Funktion ist einfach eine Schleife über die Ergebnisse:
function printVideoFeed($videoFeed) { $count = 1; foreach ($videoFeed as $videoEntry) { echo "Entry # " . $count . "\n"; printVideoEntry($videoEntry); echo "\n"; $count++; } }
Oder aber alle ähnlichen Videos zum oben gezeigten:
printVideoFeed($yt->getRelatedVideoFeed('oebqlzblfyo'));
Oder alle Videos eines bestimmten Users:
printVideoFeed($yt->getuserUploads('OreillyMedia'));
Alle Antworten die Feeds betreffen liefern standardmäßig erstmal maximal 25 Ergebnisse zurück. Möchte man “weiterblättern” kann man das so tun:
try { $nextFeed = $videoFeed->getNextFeed(); } catch (Zend_Gdata_App_Exception $e) { echo $e->getMessage() . "\n"; }
Gibt es keine nächstes Seite wird eine Exception geworfen.
Eine Suche nach einem Suchstring sieht wie folgt aus:
$query = $yt->newVideoQuery(); $query->videoQuery = 'phpunit test'; $query->startIndex = 10; $query->maxResults = 20; $query->orderBy = 'viewCount'; $videoFeed = $yt->getVideoFeed($query->getQueryUrl(2)); printVideoFeed($videoFeed);
Für alle “non-public” Zugriffe auf die API benötigt man einen Developer-Account, mit dem dann ein API-Key generiert werden kann. Das betrifft beispielsweise den Video-Upload, Kommentare und Ratings setzen, Nachrichten lesen, Playlists bearbeiten und vielem mehr. Im YouTube Developer API Guide befinden sich noch viele weitere Informationen zur API, genauso wie im Zend_Gdata_YouTube Manual.
Ähnliche Artikel: