Tutorial for Services_YouTube
Introduction
Introduction to Services_YouTube
Services_YouTube is an abstraction for the webservice of YouTube APIs. Using YouTube APIs, you can easily integrate online videos from YouTube's rapidly growing repository of videos into your application. To use YouTube API and this package, create a YouTube account and create a YouTube developer profile.
YouTube APIs currently allow read-only access to key parts of the YouTube video respository and user community. Services_YouTube provides functions for YouTube APIs using O-O abstraction access.
API
Constructor
Constructor.
$youtube = new Services_YouTube($dev_id);
$dev_id is the developper ID of the youtube.com.
$youtube = new Services_YouTube($dev_id, array('useCache' => false,
'cacheOptions' => array(),
'responseFormat' => 'object',
'driver' => 'rest'));
You can set the options using hash in the seconde arguments. Currently, there are 4 options you can set.
- useCache
-
This option set the config of using Cache or not. If the value is true, use Cache_Lite to receive the result of the YouTube API. Then you need Cache_Lite installed. The default value is false.
- cacheOptions
-
This option is the same option of the Cache_Lite. If the value useCache is false, setting this option does nothing. The default value is array().
- responseFormat
-
This option set the config of the response of the API to be SimpleXMLElement or array. Set the option value either "object", or "array". The default value is "object". That mean, you receive the SimpleXMLElement tree.
- driver
-
This option set the approach to use YouTube APIs. YouTube APIs support REST and XML-RPC approach. Set the option value either "rest", or "xmlrpc". The default value is "rest". If you preffered using XML-RPC approach, set "xmlrpc" in this option value. Then you need XML/RPC2 installed.
User Information
getProfile
Retrieves the public parts of a user profile. This method call youtube.users.get_profile function
$userProfile = $youtube->getProfile($user);
The argument, $user is the user id of the youtube.com. If the $user exists, the response is the following XML parsed by SimpleXMLElement or array. It depends on the responseFormat option.
<user_profile>
<first_name>Bob</first_name>
<last_name>Jones</last_name>
<about_me>This is my profile</about_me>
<age>29</age>
<video_upload_count>7</video_upload_count>
<ideo_watch_count>16</video_watch_count>
<homepage>http://www.myhomepage.com/</homepage>
<hometown>Los Angeles, CA</hometown>
<gender>m</gender> <!-- m or f -->
<occupations>Abstract Artist</occupations>
<companies>YouTube</companies>
<city>San Francisco, CA</city>
<country>US</country>
<books>Learning Python</books>
<obbies>YouTube, YouTube, YouTube</hobbies>
<movies>Star Wars Original Trilogy</movies>
<relationship>taken</relationship> <!-- single, taken, or open -->
<friend_count>5</friend_count>
<favorite_video_count>15</favorite_video_count>
<urrently_on>false</currently_on>
<user_profile>
If the $user does not exist, throw Services_YouTube_Exception.
listFavoriteVideos
Lists a user's favorite videos. This method call youtube.users.list_favorite_videos function
$videos = $youtube->listFavoriteVideos($user);
The argument, $user is the user id of the youtube.com. If the $user exists, the response is the following XML parsed by SimpleXMLElement or array. It depends on the responseFormat option.
<video_list>
<video>
<author>youtuberocks</author>
<id>k0gEeue2sLk</id> <!-- this ID can be used with youtube.videos.get_details -->
<title>My First Motion Picture</title>
<length_seconds>16</length_seconds> <!-- length of video -->
<rating_avg>3.75</rating_avg>
<rating_count>10</rating_count>
<description>This is the video description shown on the YouTube site.</description>
<view_count>170</view_count>
<upload_time>1121398533</upload_time> <!-- UNIX-style time, secs since 1/1/1970 -->
<comment_count>1</comment_count> <!-- how many comments does this video have? -->
<tags>feature film documentary</tags>
<url>http://www.youtube.com/watch?v=k04Eeue24Lk</url>
<thumbnail_url>http://static.youtube.com/get_still?video_id=k04Eeue24Lk</thumbnail_url>
</video>
<video>
... another video ...
</video>
</video_list>
If the $user does not exist, throw Services_YouTube_Exception.
listFriends
Lists a user's friends. This method call youtube.users.list_friends function
$friends = $youtube->listFriends($user);
The argument, $user, is the user id of the youtube.com. If the $user exists, the response is the following XML parsed by SimpleXMLElement or array. It depends on the responseFormat option.
<friend_list>
<friend>
<user>username1</user>
<video_upload_count>1</video_upload_count>
<favorite_count>2</favorite_count>
<friend_count>3</friend_count>
</friend>
<friend>
<user>username2</user>
<video_upload_count>5</video_upload_count>
<favorite_count>3</favorite_count>
<friend_count>2</friend_count>
</friend>
... and more ...
</friend_list>
If the $user does not exist, throw Services_YouTube_Exception.
Video Viewing
getDetails
Displays the details for a video. This method call youtube.videos.get_details function
$video = $youtube->getDetails($video);
The argument $video is the video id of the youtube.com. If the $video exists, the response is the following XML parsed by SimpleXMLElement or array. It depends on the responseFormat option.
<video_details>
<author>youtubeuser</author>
<title>My Trip to California</title>
<rating_avg>3.25</rating_avg>
<rating_count>10</rating_count>
<tags>california trip redwoods</tags>
<description>This video shows some highlights of my trip to California last year.</description>
<update_time>1129803584</update_time> <!-- UNIX time, secs since 1/1/70 -->
<view_count>7</view_count>
<upload_time>1127760809</upload_time> <!-- UNIX time, secs since 1/1/70 -->
<length_seconds>8</length_seconds>
<recording_date>None</recording_date>
<recording_location/>
<recording_country/>
<comment_list>
<comment>
<author>steve</author>
<text>asdfasdf</text>
<time>1129773022</time>
</comment>
</comment_list>
<channel_list>
<channel>Humor</channel>
<channel>Odd & Outrageous</channel>
</channel_list>
<thumbnail_url>http://static205.youtube.com/vi/bkZHmZmZUJk/2.jpg</thumbnail_url>
</video_details>
If the $video does not exist, throw Services_YouTube_Exception.
listByTag
Lists all videos that have the specified tag. This method call youtube.videos.list_by_tag function
$videos = $youtube->listByTag($tagsSepratedBySpace, $page, $perPage)
The argument, $tagsSeparatedBySpace is the tags list separated_by_spaces. $page is the "page" of the result(default:1). $perPage is the number of the results of one page(default:20, maximum 100). The response is the following XML parsed by SimpleXMLElement or array. It depends on the responseFormat option.
<video_list>
<video>
<author>youtuberocks</author>
<id>k0gEeue2sLk</id> <!-- this ID can be used with youtube.videos.get_details -->
<title>My First Motion Picture</title>
<length_seconds>16</length_seconds> <!-- length of video -->
<rating_avg>3.75</rating_avg>
<rating_count>10</rating_count>
<description>This is the video description shown on the YouTube site.</description>
<view_count>170</view_count>
<upload_time>1121398533</upload_time> <!-- UNIX-style time, secs since 1/1/1970 -->
<comment_count>1</comment_count> <!-- how many comments does this video have? -->
<tags>feature film documentary</tags>
<url>http://www.youtube.com/watch?v=k04Eeue24Lk</url>
<thumbnail_url>http://static.youtube.com/get_still?video_id=k04Eeue24Lk</thumbnail_url>
</video>
<video>
... another video ...
</video>
</video_list>
If the $perPage is bigger than 100, throw Services_YouTube_Exception.
listByUser
Lists all videos that were uploaded by the specified user. This method call youtube.videos.list_by_user function
$videos = $youtube->listByUser($user);
The argument, $user, is the user id of the youtube.com. If the $user exists, the response is the following XML parsed by SimpleXMLElement or array. It depends on the responseFormat option.
<video_list>
<video>
<author>youtuberocks</author>
<id>k0gEeue2sLk</id> <!-- this ID can be used with youtube.videos.get_details -->
<title>My First Motion Picture</title>
<length_seconds>16</length_seconds> <!-- length of video -->
<rating_avg>3.75</rating_avg>
<rating_count>10</rating_count>
<description>This is the video description shown on the YouTube site.</description>
<view_count>170</view_count>
<upload_time>1121398533</upload_time> <!-- UNIX-style time, secs since 1/1/1970 -->
<comment_count>1</comment_count> <!-- how many comments does this video have? -->
<tags>feature film documentary</tags>
<url>http://www.youtube.com/watch?v=k04Eeue24Lk</url>
<thumbnail_url>http://static.youtube.com/get_still?video_id=k04Eeue24Lk</thumbnail_url>
</video>
<video>
... another video ...
</video>
</video_list>
If the $user does not exist, throw Services_YouTube_Exception.
listFeatured
Lists all videos that were uploaded by the specified user. This method call youtube.videos.list_featured function
$videos = $youtube->listFeatured();
The response is the following XML parsed by SimpleXMLElement or array. It depends on the responseFormat option.
<video_list>
<video>
<author>youtuberocks</author>
<id>k0gEeue2sLk</id> <!-- this ID can be used with youtube.videos.get_details -->
<title>My First Motion Picture</title>
<length_seconds>16</length_seconds> <!-- length of video -->
<rating_avg>3.75</rating_avg>
<rating_count>10</rating_count>
<description>This is the video description shown on the YouTube site.</description>
<view_count>170</view_count>
<upload_time>1121398533</upload_time> <!-- UNIX-style time, secs since 1/1/1970 -->
<comment_count>1</comment_count> <!-- how many comments does this video have? -->
<tags>feature film documentary</tags>
<url>http://www.youtube.com/watch?v=k04Eeue24Lk</url>
<thumbnail_url>http://static.youtube.com/get_still?video_id=k04Eeue24Lk</thumbnail_url>
</video>
<video>
... another video ...
</video>
Optional API
* You can set this option using constructor.
setDriver
Choose REST or XML-RPC approach to use YouTube API.
$youtube->setDriver($driver);
The argument, $driver, has to be either "rest" or "xmlrpc". When set $driver "xmlrpc", you need XML_RPC2 installed.
setResponseFormat
Choose SimpleXMLElement or array to get the response of the YouTube API.
$youtube->setResponseFormat($responseFormat);
The argument, $responseFormat, has to be either "object" or "array".
setUseCache
Choose using cache or not.
$youtube->setUseCache($useCache, [$cacheOptions]);
The argument, $userCache, has to be boolean. If $userCache is true, Services_YouTube uses Cache_Lite. If you want to set the Cache_Lite option, set $cacheOptions.
Examples
listFeatured, listFavoriteVideos, listByTag, listByUser sample.
$dev_id = "YOUR_DEV_ID";
$user_id = "USER_ID";
$tag = "TAG";
$youtube = new Services_YouTube($dev_id, array('usesCache' => true));
$videos = $youtube->listFeatured();
// $videos = $youtube->listFavoriteVideos($user_id);
// $videos = $youtube->listByTag($tag);
// $videos = $youtube->listByUser($user_id);
print "<html><head></head><body>\n";
foreach ($videos->xpath('//video') as $i => $video) {
print "<img src='{$video->thumbnail_url}' alt='{$video->title}' />\n";
print "<a href='{$video->url}'>URL</a><br />\n";
}
print "</body></html>";
getProfile
$dev_id = "YOUR_DEV_ID";
$user_id = "USER_ID";
$youtube = new Services_YouTube($dev_id, array('usesCache' => true));
$user = $youtube->getProfile($user_id);
print "<html><head></head><body>\n";
$profile = $user->user_profile;
print "{$profile->first_name} {$profile->last_name} :({$profile->video_watch_count} Wathced.)\n";
print "</body></html>";
listFriends
$dev_id = "YOUR_DEV_ID";
$user_id = "USER_ID";
$youtube = new Services_YouTube($dev_id, array('usesCache' => true));
$users = $youtube->listFriends($user_id);
print "<html><head></head><body>\n";
foreach ($users->xpath('//friend') as $i => $friend) {
print "{$friend->user} : Upload: {$friend->video_upload_count}";
}
print "</body></html>";
getDetails
$dev_id = "YOUR_DEV_ID";
$video_id = "VIDEO_ID";
$youtube = new Services_YouTube($dev_id, array('usesCache' => true));
$video = $youtube->getDetails($video_id);
$details = $video->video_details;
print "<html><head></head><body>\n";
print "{$details->title} : ({$details->tags}) : RATE: {$details->rating_avg} in {$detials->rating_count}";
print "<img src='{$details->thumbnail_url}' alt={$details->title}><hr /><hr />";
foreach ($details->xpath('//comment') as $i => $comment) {
print "{$comment->author} : {$comment->text}<hr />";
}
print "</body></html>";