8 * This source file is subject to the new BSD license that is bundled
9 * with this package in the file LICENSE.txt.
10 * It is also available through the world-wide-web at this URL:
11 * http://framework.zend.com/license/new-bsd
12 * If you did not receive a copy of the license and are unable to
13 * obtain it through the world-wide-web, please send an email
14 * to license@zend.com so we can send you a copy immediately.
19 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
20 * @license http://framework.zend.com/license/new-bsd New BSD License
25 * @see Zend_Gdata_Media
27 require_once 'Zend/Gdata/Media.php';
30 * @see Zend_Gdata_YouTube_VideoEntry
32 require_once 'Zend/Gdata/YouTube/VideoEntry.php';
35 * @see Zend_Gdata_YouTube_VideoFeed
37 require_once 'Zend/Gdata/YouTube/VideoFeed.php';
40 * @see Zend_Gdata_YouTube_CommentFeed
42 require_once 'Zend/Gdata/YouTube/CommentFeed.php';
45 * @see Zend_Gdata_YouTube_PlaylistListFeed
47 require_once 'Zend/Gdata/YouTube/PlaylistListFeed.php';
50 * @see Zend_Gdata_YouTube_SubscriptionFeed
52 require_once 'Zend/Gdata/YouTube/SubscriptionFeed.php';
55 * @see Zend_Gdata_YouTube_ContactFeed
57 require_once 'Zend/Gdata/YouTube/ContactFeed.php';
60 * @see Zend_Gdata_YouTube_PlaylistVideoFeed
62 require_once 'Zend/Gdata/YouTube/PlaylistVideoFeed.php';
65 * @see Zend_Gdata_YouTube_ActivityFeed
67 require_once 'Zend/Gdata/YouTube/ActivityFeed.php';
70 * @see Zend_Gdata_YouTube_InboxFeed
72 require_once 'Zend/Gdata/YouTube/InboxFeed.php';
76 * Service class for interacting with the YouTube Data API.
77 * @link http://code.google.com/apis/youtube/
82 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
83 * @license http://framework.zend.com/license/new-bsd New BSD License
85 class Zend_Gdata_YouTube extends Zend_Gdata_Media
88 const AUTH_SERVICE_NAME = 'youtube';
89 const CLIENTLOGIN_URL = 'https://www.google.com/youtube/accounts/ClientLogin';
91 const STANDARD_TOP_RATED_URI = 'http://gdata.youtube.com/feeds/api/standardfeeds/top_rated';
92 const STANDARD_MOST_VIEWED_URI = 'http://gdata.youtube.com/feeds/api/standardfeeds/most_viewed';
93 const STANDARD_RECENTLY_FEATURED_URI = 'http://gdata.youtube.com/feeds/api/standardfeeds/recently_featured';
94 const STANDARD_WATCH_ON_MOBILE_URI = 'http://gdata.youtube.com/feeds/api/standardfeeds/watch_on_mobile';
96 const STANDARD_TOP_RATED_URI_V2 =
97 'http://gdata.youtube.com/feeds/api/standardfeeds/top_rated';
98 const STANDARD_MOST_VIEWED_URI_V2 =
99 'http://gdata.youtube.com/feeds/api/standardfeeds/most_viewed';
100 const STANDARD_RECENTLY_FEATURED_URI_V2 =
101 'http://gdata.youtube.com/feeds/api/standardfeeds/recently_featured';
102 const STANDARD_WATCH_ON_MOBILE_URI_V2 =
103 'http://gdata.youtube.com/feeds/api/standardfeeds/watch_on_mobile';
105 const USER_URI = 'http://gdata.youtube.com/feeds/api/users';
106 const VIDEO_URI = 'http://gdata.youtube.com/feeds/api/videos';
107 const PLAYLIST_REL = 'http://gdata.youtube.com/schemas/2007#playlist';
108 const USER_UPLOADS_REL = 'http://gdata.youtube.com/schemas/2007#user.uploads';
109 const USER_PLAYLISTS_REL = 'http://gdata.youtube.com/schemas/2007#user.playlists';
110 const USER_SUBSCRIPTIONS_REL = 'http://gdata.youtube.com/schemas/2007#user.subscriptions';
111 const USER_CONTACTS_REL = 'http://gdata.youtube.com/schemas/2007#user.contacts';
112 const USER_FAVORITES_REL = 'http://gdata.youtube.com/schemas/2007#user.favorites';
113 const VIDEO_RESPONSES_REL = 'http://gdata.youtube.com/schemas/2007#video.responses';
114 const VIDEO_RATINGS_REL = 'http://gdata.youtube.com/schemas/2007#video.ratings';
115 const VIDEO_COMPLAINTS_REL = 'http://gdata.youtube.com/schemas/2007#video.complaints';
116 const ACTIVITY_FEED_URI = 'http://gdata.youtube.com/feeds/api/events';
117 const FRIEND_ACTIVITY_FEED_URI =
118 'http://gdata.youtube.com/feeds/api/users/default/friendsactivity';
121 * The URI of the in-reply-to schema for comments in reply to
126 const IN_REPLY_TO_SCHEME =
127 'http://gdata.youtube.com/schemas/2007#in-reply-to';
130 * The URI of the inbox feed for the currently authenticated user.
134 const INBOX_FEED_URI =
135 'http://gdata.youtube.com/feeds/api/users/default/inbox';
138 * The maximum number of users for which activity can be requested for,
139 * as enforced by the API.
143 const ACTIVITY_FEED_MAX_USERS = 20;
146 * The suffix for a feed of favorites.
150 const FAVORITES_URI_SUFFIX = 'favorites';
153 * The suffix for the user's upload feed.
157 const UPLOADS_URI_SUFFIX = 'uploads';
160 * The suffix for a feed of video responses.
164 const RESPONSES_URI_SUFFIX = 'responses';
167 * The suffix for a feed of related videos.
171 const RELATED_URI_SUFFIX = 'related';
174 * The suffix for a feed of messages (inbox entries).
178 const INBOX_URI_SUFFIX = 'inbox';
181 * Namespaces used for Zend_Gdata_YouTube
185 public static $namespaces = array(
186 array('yt', 'http://gdata.youtube.com/schemas/2007', 1, 0),
187 array('georss', 'http://www.georss.org/georss', 1, 0),
188 array('gml', 'http://www.opengis.net/gml', 1, 0),
189 array('media', 'http://search.yahoo.com/mrss/', 1, 0)
193 * Create Zend_Gdata_YouTube object
195 * @param Zend_Http_Client $client (optional) The HTTP client to use when
196 * when communicating with the Google servers.
197 * @param string $applicationId The identity of the app in the form of
198 * Company-AppName-Version
199 * @param string $clientId The clientId issued by the YouTube dashboard
200 * @param string $developerKey The developerKey issued by the YouTube dashboard
202 public function __construct($client = null,
203 $applicationId = 'MyCompany-MyApp-1.0', $clientId = null,
204 $developerKey = null)
206 $this->registerPackage('Zend_Gdata_YouTube');
207 $this->registerPackage('Zend_Gdata_YouTube_Extension');
208 $this->registerPackage('Zend_Gdata_Media');
209 $this->registerPackage('Zend_Gdata_Media_Extension');
211 // NOTE This constructor no longer calls the parent constructor
212 $this->setHttpClient($client, $applicationId, $clientId, $developerKey);
216 * Set the Zend_Http_Client object used for communication
218 * @param Zend_Http_Client $client The client to use for communication
219 * @throws Zend_Gdata_App_HttpException
220 * @return Zend_Gdata_App Provides a fluent interface
222 public function setHttpClient($client,
223 $applicationId = 'MyCompany-MyApp-1.0', $clientId = null,
224 $developerKey = null)
226 if ($client === null) {
227 $client = new Zend_Http_Client();
229 if (!$client instanceof Zend_Http_Client) {
230 require_once 'Zend/Gdata/App/HttpException.php';
231 throw new Zend_Gdata_App_HttpException(
232 'Argument is not an instance of Zend_Http_Client.');
235 if ($clientId != null) {
236 $client->setHeaders('X-GData-Client', $clientId);
239 if ($developerKey != null) {
240 $client->setHeaders('X-GData-Key', 'key='. $developerKey);
243 return parent::setHttpClient($client, $applicationId);
247 * Retrieves a feed of videos.
249 * @param mixed $location (optional) The URL to query or a
250 * Zend_Gdata_Query object from which a URL can be determined
251 * @return Zend_Gdata_YouTube_VideoFeed The feed of videos found at the
254 public function getVideoFeed($location = null)
256 if ($location == null) {
257 $uri = self::VIDEO_URI;
258 } else if ($location instanceof Zend_Gdata_Query) {
259 $uri = $location->getQueryUrl();
263 return parent::getFeed($uri, 'Zend_Gdata_YouTube_VideoFeed');
267 * Retrieves a specific video entry.
269 * @param mixed $videoId The ID of the video to retrieve.
270 * @param mixed $location (optional) The URL to query or a
271 * Zend_Gdata_Query object from which a URL can be determined.
272 * @param boolean $fullEntry (optional) Retrieve the full metadata for the
273 * entry. Only possible if entry belongs to currently authenticated
274 * user. An exception will be thrown otherwise.
275 * @throws Zend_Gdata_App_HttpException
276 * @return Zend_Gdata_YouTube_VideoEntry The video entry found at the
279 public function getVideoEntry($videoId = null, $location = null,
282 if ($videoId !== null) {
284 return $this->getFullVideoEntry($videoId);
286 $uri = self::VIDEO_URI . "/" . $videoId;
288 } else if ($location instanceof Zend_Gdata_Query) {
289 $uri = $location->getQueryUrl();
293 return parent::getEntry($uri, 'Zend_Gdata_YouTube_VideoEntry');
297 * Retrieves a video entry from the user's upload feed.
299 * @param mixed $videoID The ID of the video to retrieve.
300 * @throws Zend_Gdata_App_HttpException
301 * @return Zend_Gdata_YouTube_VideoEntry|null The video entry to be
302 * retrieved, or null if it was not found or the user requesting it
303 * did not have the appropriate permissions.
305 public function getFullVideoEntry($videoId)
307 $uri = self::USER_URI . "/default/" .
308 self::UPLOADS_URI_SUFFIX . "/$videoId";
309 return parent::getEntry($uri, 'Zend_Gdata_YouTube_VideoEntry');
313 * Retrieves a feed of videos related to the specified video ID.
315 * @param string $videoId The videoId of interest
316 * @param mixed $location (optional) The URL to query or a
317 * Zend_Gdata_Query object from which a URL can be determined
318 * @return Zend_Gdata_YouTube_VideoFeed The feed of videos found at the
321 public function getRelatedVideoFeed($videoId = null, $location = null)
323 if ($videoId !== null) {
324 $uri = self::VIDEO_URI . "/" . $videoId . "/" .
325 self::RELATED_URI_SUFFIX;
326 } else if ($location instanceof Zend_Gdata_Query) {
327 $uri = $location->getQueryUrl();
331 return parent::getFeed($uri, 'Zend_Gdata_YouTube_VideoFeed');
335 * Retrieves a feed of video responses related to the specified video ID.
337 * @param string $videoId The videoId of interest
338 * @param mixed $location (optional) The URL to query or a
339 * Zend_Gdata_Query object from which a URL can be determined
340 * @return Zend_Gdata_YouTube_VideoFeed The feed of videos found at the
343 public function getVideoResponseFeed($videoId = null, $location = null)
345 if ($videoId !== null) {
346 $uri = self::VIDEO_URI . "/" . $videoId . "/" .
347 self::RESPONSES_URI_SUFFIX;
348 } else if ($location instanceof Zend_Gdata_Query) {
349 $uri = $location->getQueryUrl();
353 return parent::getFeed($uri, 'Zend_Gdata_YouTube_VideoFeed');
357 * Retrieves a feed of comments related to the specified video ID.
359 * @param string $videoId The videoId of interest
360 * @param mixed $location (optional) The URL to query or a
361 * Zend_Gdata_Query object from which a URL can be determined
362 * @return Zend_Gdata_YouTube_CommentFeed The feed of videos found at the
365 public function getVideoCommentFeed($videoId = null, $location = null)
367 if ($videoId !== null) {
368 $uri = self::VIDEO_URI . "/" . $videoId . "/comments";
369 } else if ($location instanceof Zend_Gdata_Query) {
370 $uri = $location->getQueryUrl();
374 return parent::getFeed($uri, 'Zend_Gdata_YouTube_CommentFeed');
378 * Retrieves a feed of comments related to the specified video ID.
380 * @param mixed $location (optional) The URL to query or a
381 * Zend_Gdata_Query object from which a URL can be determined
382 * @return Zend_Gdata_YouTube_CommentFeed The feed of videos found at the
385 public function getTopRatedVideoFeed($location = null)
387 $standardFeedUri = self::STANDARD_TOP_RATED_URI;
389 if ($this->getMajorProtocolVersion() == 2) {
390 $standardFeedUri = self::STANDARD_TOP_RATED_URI_V2;
393 if ($location == null) {
394 $uri = $standardFeedUri;
395 } else if ($location instanceof Zend_Gdata_Query) {
396 if ($location instanceof Zend_Gdata_YouTube_VideoQuery) {
397 if (!isset($location->url)) {
398 $location->setFeedType('top rated');
401 $uri = $location->getQueryUrl();
405 return parent::getFeed($uri, 'Zend_Gdata_YouTube_VideoFeed');
410 * Retrieves a feed of the most viewed videos.
412 * @param mixed $location (optional) The URL to query or a
413 * Zend_Gdata_Query object from which a URL can be determined
414 * @return Zend_Gdata_YouTube_VideoFeed The feed of videos found at the
417 public function getMostViewedVideoFeed($location = null)
419 $standardFeedUri = self::STANDARD_MOST_VIEWED_URI;
421 if ($this->getMajorProtocolVersion() == 2) {
422 $standardFeedUri = self::STANDARD_MOST_VIEWED_URI_V2;
425 if ($location == null) {
426 $uri = $standardFeedUri;
427 } else if ($location instanceof Zend_Gdata_Query) {
428 if ($location instanceof Zend_Gdata_YouTube_VideoQuery) {
429 if (!isset($location->url)) {
430 $location->setFeedType('most viewed');
433 $uri = $location->getQueryUrl();
437 return parent::getFeed($uri, 'Zend_Gdata_YouTube_VideoFeed');
441 * Retrieves a feed of recently featured videos.
443 * @param mixed $location (optional) The URL to query or a
444 * Zend_Gdata_Query object from which a URL can be determined
445 * @return Zend_Gdata_YouTube_VideoFeed The feed of videos found at the
448 public function getRecentlyFeaturedVideoFeed($location = null)
450 $standardFeedUri = self::STANDARD_RECENTLY_FEATURED_URI;
452 if ($this->getMajorProtocolVersion() == 2) {
453 $standardFeedUri = self::STANDARD_RECENTLY_FEATURED_URI_V2;
456 if ($location == null) {
457 $uri = $standardFeedUri;
458 } else if ($location instanceof Zend_Gdata_Query) {
459 if ($location instanceof Zend_Gdata_YouTube_VideoQuery) {
460 if (!isset($location->url)) {
461 $location->setFeedType('recently featured');
464 $uri = $location->getQueryUrl();
468 return parent::getFeed($uri, 'Zend_Gdata_YouTube_VideoFeed');
472 * Retrieves a feed of videos recently featured for mobile devices.
473 * These videos will have RTSP links in the $entry->mediaGroup->content
475 * @param mixed $location (optional) The URL to query or a
476 * Zend_Gdata_Query object from which a URL can be determined
477 * @return Zend_Gdata_YouTube_VideoFeed The feed of videos found at the
480 public function getWatchOnMobileVideoFeed($location = null)
482 $standardFeedUri = self::STANDARD_WATCH_ON_MOBILE_URI;
484 if ($this->getMajorProtocolVersion() == 2) {
485 $standardFeedUri = self::STANDARD_WATCH_ON_MOBILE_URI_V2;
488 if ($location == null) {
489 $uri = $standardFeedUri;
490 } else if ($location instanceof Zend_Gdata_Query) {
491 if ($location instanceof Zend_Gdata_YouTube_VideoQuery) {
492 if (!isset($location->url)) {
493 $location->setFeedType('watch on mobile');
496 $uri = $location->getQueryUrl();
500 return parent::getFeed($uri, 'Zend_Gdata_YouTube_VideoFeed');
504 * Retrieves a feed which lists a user's playlist
506 * @param string $user (optional) The username of interest
507 * @param mixed $location (optional) The URL to query or a
508 * Zend_Gdata_Query object from which a URL can be determined
509 * @return Zend_Gdata_YouTube_PlaylistListFeed The feed of playlists
511 public function getPlaylistListFeed($user = null, $location = null)
513 if ($user !== null) {
514 $uri = self::USER_URI . '/' . $user . '/playlists';
515 } else if ($location instanceof Zend_Gdata_Query) {
516 $uri = $location->getQueryUrl();
520 return parent::getFeed($uri, 'Zend_Gdata_YouTube_PlaylistListFeed');
524 * Retrieves a feed of videos in a particular playlist
526 * @param mixed $location (optional) The URL to query or a
527 * Zend_Gdata_Query object from which a URL can be determined
528 * @return Zend_Gdata_YouTube_PlaylistVideoFeed The feed of videos found at
531 public function getPlaylistVideoFeed($location)
533 if ($location instanceof Zend_Gdata_Query) {
534 $uri = $location->getQueryUrl();
538 return parent::getFeed($uri, 'Zend_Gdata_YouTube_PlaylistVideoFeed');
542 * Retrieves a feed of a user's subscriptions
544 * @param string $user (optional) The username of interest
545 * @param mixed $location (optional) The URL to query or a
546 * Zend_Gdata_Query object from which a URL can be determined
547 * @return Zend_Gdata_YouTube_SubscriptionListFeed The feed of subscriptions
549 public function getSubscriptionFeed($user = null, $location = null)
551 if ($user !== null) {
552 $uri = self::USER_URI . '/' . $user . '/subscriptions';
553 } else if ($location instanceof Zend_Gdata_Query) {
554 $uri = $location->getQueryUrl();
558 return parent::getFeed($uri, 'Zend_Gdata_YouTube_SubscriptionFeed');
562 * Retrieves a feed of a user's contacts
564 * @param string $user (optional) The username of interest
565 * @param mixed $location (optional) The URL to query or a
566 * Zend_Gdata_Query object from which a URL can be determined
567 * @return Zend_Gdata_YouTube_ContactFeed The feed of contacts
569 public function getContactFeed($user = null, $location = null)
571 if ($user !== null) {
572 $uri = self::USER_URI . '/' . $user . '/contacts';
573 } else if ($location instanceof Zend_Gdata_Query) {
574 $uri = $location->getQueryUrl();
578 return parent::getFeed($uri, 'Zend_Gdata_YouTube_ContactFeed');
582 * Retrieves a user's uploads
584 * @param string $user (optional) The username of interest
585 * @param mixed $location (optional) The URL to query or a
586 * Zend_Gdata_Query object from which a URL can be determined
587 * @return Zend_Gdata_YouTube_VideoFeed The videos uploaded by the user
589 public function getUserUploads($user = null, $location = null)
591 if ($user !== null) {
592 $uri = self::USER_URI . '/' . $user . '/' .
593 self::UPLOADS_URI_SUFFIX;
594 } else if ($location instanceof Zend_Gdata_Query) {
595 $uri = $location->getQueryUrl();
599 return parent::getFeed($uri, 'Zend_Gdata_YouTube_VideoFeed');
603 * Retrieves a user's favorites
605 * @param string $user (optional) The username of interest
606 * @param mixed $location (optional) The URL to query or a
607 * Zend_Gdata_Query object from which a URL can be determined
608 * @return Zend_Gdata_YouTube_VideoFeed The videos favorited by the user
610 public function getUserFavorites($user = null, $location = null)
612 if ($user !== null) {
613 $uri = self::USER_URI . '/' . $user . '/' .
614 self::FAVORITES_URI_SUFFIX;
615 } else if ($location instanceof Zend_Gdata_Query) {
616 $uri = $location->getQueryUrl();
620 return parent::getFeed($uri, 'Zend_Gdata_YouTube_VideoFeed');
624 * Retrieves a user's profile as an entry
626 * @param string $user (optional) The username of interest
627 * @param mixed $location (optional) The URL to query or a
628 * Zend_Gdata_Query object from which a URL can be determined
629 * @return Zend_Gdata_YouTube_UserProfileEntry The user profile entry
631 public function getUserProfile($user = null, $location = null)
633 if ($user !== null) {
634 $uri = self::USER_URI . '/' . $user;
635 } else if ($location instanceof Zend_Gdata_Query) {
636 $uri = $location->getQueryUrl();
640 return parent::getEntry($uri, 'Zend_Gdata_YouTube_UserProfileEntry');
644 * Helper function for parsing a YouTube token response
646 * @param string $response The service response
647 * @throws Zend_Gdata_App_Exception
648 * @return array An array containing the token and URL
650 public static function parseFormUploadTokenResponse($response)
652 // Load the feed as an XML DOMDocument object
653 @ini_set('track_errors', 1);
654 $doc = new DOMDocument();
655 $success = @$doc->loadXML($response);
656 @ini_restore('track_errors');
659 require_once 'Zend/Gdata/App/Exception.php';
660 throw new Zend_Gdata_App_Exception(
661 "Zend_Gdata_YouTube::parseFormUploadTokenResponse - " .
662 "DOMDocument cannot parse XML: $php_errormsg");
664 $responseElement = $doc->getElementsByTagName('response')->item(0);
668 if ($responseElement != null) {
670 $responseElement->getElementsByTagName('url')->item(0);
672 $responseElement->getElementsByTagName('token')->item(0);
674 if ($urlElement && $urlElement->hasChildNodes() &&
675 $tokenElement && $tokenElement->hasChildNodes()) {
677 $urlText = $urlElement->firstChild->nodeValue;
678 $tokenText = $tokenElement->firstChild->nodeValue;
682 if ($tokenText != null && $urlText != null) {
683 return array('token' => $tokenText, 'url' => $urlText);
685 require_once 'Zend/Gdata/App/Exception.php';
686 throw new Zend_Gdata_App_Exception(
687 'Form upload token not found in response');
692 * Retrieves a YouTube token
694 * @param Zend_Gdata_YouTube_VideoEntry $videoEntry The video entry
695 * @param string $url The location as a string URL
696 * @throws Zend_Gdata_App_Exception
697 * @return array An array containing a token and URL
699 public function getFormUploadToken($videoEntry,
700 $url='http://gdata.youtube.com/action/GetUploadToken')
702 if ($url != null && is_string($url)) {
703 // $response is a Zend_Http_response object
704 $response = $this->post($videoEntry, $url);
705 return self::parseFormUploadTokenResponse($response->getBody());
707 require_once 'Zend/Gdata/App/Exception.php';
708 throw new Zend_Gdata_App_Exception(
709 'Url must be provided as a string URL');
714 * Retrieves the activity feed for users
716 * @param mixed $usernames A string identifying the usernames for which to
717 * retrieve activity for. This can also be a Zend_Gdata_Query
718 * object from which a URL can be determined.
719 * @throws Zend_Gdata_App_VersionException if using version less than 2.
720 * @return Zend_Gdata_YouTube_ActivityFeed
722 public function getActivityForUser($username)
724 if ($this->getMajorProtocolVersion() == 1) {
725 require_once 'Zend/Gdata/App/VersionException.php';
726 throw new Zend_Gdata_App_VersionException('User activity feeds ' .
727 'are not available in API version 1.');
731 if ($username instanceof Zend_Gdata_Query) {
732 $uri = $username->getQueryUrl();
734 if (count(explode(',', $username)) >
735 self::ACTIVITY_FEED_MAX_USERS) {
736 require_once 'Zend/Gdata/App/InvalidArgumentException.php';
737 throw new Zend_Gdata_App_InvalidArgumentException(
738 'Activity feed can only retrieve for activity for up to ' .
739 self::ACTIVITY_FEED_MAX_USERS . ' users per request');
741 $uri = self::ACTIVITY_FEED_URI . '?author=' . $username;
744 return parent::getFeed($uri, 'Zend_Gdata_YouTube_ActivityFeed');
748 * Retrieve the activity of the currently authenticated users friend.
750 * @throws Zend_Gdata_App_Exception if not logged in.
751 * @return Zend_Gdata_YouTube_ActivityFeed
753 public function getFriendActivityForCurrentUser()
755 if (!$this->isAuthenticated()) {
756 require_once 'Zend/Gdata/App/Exception.php';
757 throw new Zend_Gdata_App_Exception('You must be authenticated to ' .
758 'use the getFriendActivityForCurrentUser function in Zend_' .
761 return parent::getFeed(self::FRIEND_ACTIVITY_FEED_URI,
762 'Zend_Gdata_YouTube_ActivityFeed');
766 * Retrieve a feed of messages in the currently authenticated user's inbox.
768 * @throws Zend_Gdata_App_Exception if not logged in.
769 * @return Zend_Gdata_YouTube_InboxFeed|null
771 public function getInboxFeedForCurrentUser()
773 if (!$this->isAuthenticated()) {
774 require_once 'Zend/Gdata/App/Exception.php';
775 throw new Zend_Gdata_App_Exception('You must be authenticated to ' .
776 'use the getInboxFeedForCurrentUser function in Zend_' .
780 return parent::getFeed(self::INBOX_FEED_URI,
781 'Zend_Gdata_YouTube_InboxFeed');
785 * Send a video message.
787 * Note: Either a Zend_Gdata_YouTube_VideoEntry or a valid video ID must
790 * @param string $body The body of the message
791 * @param Zend_Gdata_YouTube_VideoEntry (optional) The video entry to send
792 * @param string $videoId The id of the video to send
793 * @param string $recipientUserName The username of the recipient
794 * @throws Zend_Gdata_App_InvalidArgumentException if no valid
795 * Zend_Gdata_YouTube_VideoEntry or videoId were provided
796 * @return Zend_Gdata_YouTube_InboxEntry|null The
797 * Zend_Gdata_YouTube_Inbox_Entry representing the sent message.
800 public function sendVideoMessage($body, $videoEntry = null,
801 $videoId = null, $recipientUserName)
803 if (!$videoId && !$videoEntry) {
804 require_once 'Zend/Gdata/App/InvalidArgumentException.php';
805 throw new Zend_Gdata_App_InvalidArgumentException(
806 'Expecting either a valid videoID or a videoEntry object in ' .
807 'Zend_Gdata_YouTube->sendVideoMessage().');
810 $messageEntry = new Zend_Gdata_YouTube_InboxEntry();
812 if ($this->getMajorProtocolVersion() == null ||
813 $this->getMajorProtocolVersion() == 1) {
816 $videoId = $videoEntry->getVideoId();
817 } elseif (strlen($videoId) < 12) {
818 //Append the full URI
819 $videoId = self::VIDEO_URI . '/' . $videoId;
822 $messageEntry->setId($this->newId($videoId));
823 // TODO there seems to be a bug where v1 inbox entries dont
824 // retain their description...
825 $messageEntry->setDescription(
826 new Zend_Gdata_YouTube_Extension_Description($body));
830 $videoId = $videoEntry->getVideoId();
831 $videoId = substr($videoId, strrpos($videoId, ':'));
833 $messageEntry->setId($this->newId($videoId));
834 $messageEntry->setSummary($this->newSummary($body));
837 $insertUrl = 'http://gdata.youtube.com/feeds/api/users/' .
838 $recipientUserName . '/inbox';
839 $response = $this->insertEntry($messageEntry, $insertUrl,
840 'Zend_Gdata_YouTube_InboxEntry');
845 * Post a comment in reply to an existing comment
847 * @param $commentEntry Zend_Gdata_YouTube_CommentEntry The comment entry
849 * @param $commentText string The text of the comment to post
850 * @return A Zend_Gdata_YouTube_CommentEntry representing the posted
853 public function replyToCommentEntry($commentEntry, $commentText)
855 $newComment = $this->newCommentEntry();
856 $newComment->content = $this->newContent()->setText($commentText);
857 $commentId = $commentEntry->getId();
858 $commentIdArray = explode(':', $commentId);
860 // create a new link element
861 $inReplyToLinkHref = self::VIDEO_URI . '/' . $commentIdArray[3] .
862 '/comments/' . $commentIdArray[5];
863 $inReplyToLink = $this->newLink($inReplyToLinkHref,
864 self::IN_REPLY_TO_SCHEME, $type="application/atom+xml");
865 $links = $newComment->getLink();
866 $links[] = $inReplyToLink;
867 $newComment->setLink($links);
868 $commentFeedPostUrl = self::VIDEO_URI . '/' . $commentIdArray[3] .
870 return $this->insertEntry($newComment,
871 $commentFeedPostUrl, 'Zend_Gdata_YouTube_CommentEntry');