The YouTube API allows a user to retrieve a lot of information about an user’s profile and his videos (his uploads, his favorite videos). For each of these videos, it also allows to retrieve a lot of information like duration, keywords, comments and trackbacks… These information are accessible without the need of an API key. Most of these information can also be set though this time (to write) an API key will be needed.

The Zend Framework offers a serie of classes in its GData (Google Data) module and a few of them are aimed at working with youTube. These GData classes are well written and very staraightforward to use.

Here is a simple example of code to get information for a given video about the Mustang Shelby GT500 2005. Out of the URL, you can easily extract the video id: _YTYaB7fP-A

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Include the Gdata_YouTube class
require_once 'Zend/Gdata/YouTube.php';
$yt = new Zend_Gdata_YouTube();

# Declare the videoId
$videoId = 'videoIds';

#Get the video entry calling the YouTube API
$videoEntry = $yt->getVideoEntry($videoId);

# Now what we have a Video Entry Object, we can use the methods declared in the VideoEntry Class (in the Zend GData module)
# to obtain information about the video
echo "VideoId: <strong>".$videoId."</strong><br />";
echo "Title: <strong>".$videoEntry->getVideoTitle()."</strong><br />";
echo "Duration: <strong>".$videoEntry->getVideoDuration()." seconds</strong><br /><br />";

echo "Description: <br />".$videoEntry->getVideoDescription()."<br /><br />";
echo "Keywords: <br />";
echo implode($videoEntry->getVideoTags(), ', ');
echo "<br /><br />";

echo "Can Be embeded: <strong>"; echo !$videoEntry->getNoEmbed() || $videoEntry->getNoEmbed() == Zend_Gdata_YouTube_Extension_NoEmbed ? "true" : "false"; echo "</strong></br />";

echo "<br />";
echo "Watch: <a href='".$videoEntry->getVideoWatchPageUrl()."'>".$videoEntry->getVideoWatchPageUrl()."</a><br />";
echo "URL: <a href='".$videoEntry->getFlashPlayerUrl()."'>".$videoEntry->getFlashPlayerUrl()."</a><br /><br />";

echo "Thumb: <br />";
$thumbs = $videoEntry->getVideoThumbnails()    ;
if (is_array($thumbs) && count($thumbs)) {
foreach($thumbs as $thumb) {
echo '<img width="200" src="'.$thumb['url'].'" />';
}
}

 

Filed under: Computing, PHP

Tagged with: , , , , ,

Liked this page?

Tweet it

Subscribe to the RSS feed or sign up for the newsletter now.

Posts related to PHP: Working with the YouTube API with Zend Framework

Name (required)

Email (required)

Website

Leave a Reply

  • About Me


    photo of Samantha Halfon Samantha Halfon
    Software Engineer
    blueKiwi software
    Paris, France
    I enjoy playing with my computer(s), listening to Bob Dylan (and related artists) and watching movies (especially if they were directed by Martin Scorsese or John Cassavetes). Sometimes, I play a little guitar... If not doing any of the above, I am either riding a small red bike around Paris, or, making videos. About my videomaking please check out World Wide Angle and its blog.