Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/zend/Zend/Gdata/YouTube.php
Go to the documentation of this file.
00001 <?php
00002 
00027 require_once 'Zend/Gdata/Media.php';
00028 
00032 require_once 'Zend/Gdata/YouTube/VideoEntry.php';
00033 
00037 require_once 'Zend/Gdata/YouTube/VideoFeed.php';
00038 
00042 require_once 'Zend/Gdata/YouTube/CommentFeed.php';
00043 
00047 require_once 'Zend/Gdata/YouTube/PlaylistListFeed.php';
00048 
00052 require_once 'Zend/Gdata/YouTube/SubscriptionFeed.php';
00053 
00057 require_once 'Zend/Gdata/YouTube/ContactFeed.php';
00058 
00062 require_once 'Zend/Gdata/YouTube/PlaylistVideoFeed.php';
00063 
00067 require_once 'Zend/Gdata/YouTube/ActivityFeed.php';
00068 
00072 require_once 'Zend/Gdata/YouTube/InboxFeed.php';
00073 
00074 
00085 class Zend_Gdata_YouTube extends Zend_Gdata_Media
00086 {
00087 
00088     const AUTH_SERVICE_NAME = 'youtube';
00089     const CLIENTLOGIN_URL = 'https://www.google.com/youtube/accounts/ClientLogin';
00090 
00091     const STANDARD_TOP_RATED_URI = 'http://gdata.youtube.com/feeds/api/standardfeeds/top_rated';
00092     const STANDARD_MOST_VIEWED_URI = 'http://gdata.youtube.com/feeds/api/standardfeeds/most_viewed';
00093     const STANDARD_RECENTLY_FEATURED_URI = 'http://gdata.youtube.com/feeds/api/standardfeeds/recently_featured';
00094     const STANDARD_WATCH_ON_MOBILE_URI = 'http://gdata.youtube.com/feeds/api/standardfeeds/watch_on_mobile';
00095 
00096     const STANDARD_TOP_RATED_URI_V2 =
00097         'http://gdata.youtube.com/feeds/api/standardfeeds/top_rated';
00098     const STANDARD_MOST_VIEWED_URI_V2 =
00099         'http://gdata.youtube.com/feeds/api/standardfeeds/most_viewed';
00100     const STANDARD_RECENTLY_FEATURED_URI_V2 =
00101         'http://gdata.youtube.com/feeds/api/standardfeeds/recently_featured';
00102     const STANDARD_WATCH_ON_MOBILE_URI_V2 =
00103         'http://gdata.youtube.com/feeds/api/standardfeeds/watch_on_mobile';
00104 
00105     const USER_URI = 'http://gdata.youtube.com/feeds/api/users';
00106     const VIDEO_URI = 'http://gdata.youtube.com/feeds/api/videos';
00107     const PLAYLIST_REL = 'http://gdata.youtube.com/schemas/2007#playlist';
00108     const USER_UPLOADS_REL = 'http://gdata.youtube.com/schemas/2007#user.uploads';
00109     const USER_PLAYLISTS_REL = 'http://gdata.youtube.com/schemas/2007#user.playlists';
00110     const USER_SUBSCRIPTIONS_REL = 'http://gdata.youtube.com/schemas/2007#user.subscriptions';
00111     const USER_CONTACTS_REL = 'http://gdata.youtube.com/schemas/2007#user.contacts';
00112     const USER_FAVORITES_REL = 'http://gdata.youtube.com/schemas/2007#user.favorites';
00113     const VIDEO_RESPONSES_REL = 'http://gdata.youtube.com/schemas/2007#video.responses';
00114     const VIDEO_RATINGS_REL = 'http://gdata.youtube.com/schemas/2007#video.ratings';
00115     const VIDEO_COMPLAINTS_REL = 'http://gdata.youtube.com/schemas/2007#video.complaints';
00116     const ACTIVITY_FEED_URI = 'http://gdata.youtube.com/feeds/api/events';
00117     const FRIEND_ACTIVITY_FEED_URI =
00118         'http://gdata.youtube.com/feeds/api/users/default/friendsactivity';
00119 
00126      const IN_REPLY_TO_SCHEME =
00127          'http://gdata.youtube.com/schemas/2007#in-reply-to';
00128 
00134     const INBOX_FEED_URI =
00135         'http://gdata.youtube.com/feeds/api/users/default/inbox';
00136 
00143     const ACTIVITY_FEED_MAX_USERS = 20;
00144 
00150     const FAVORITES_URI_SUFFIX = 'favorites';
00151 
00157     const UPLOADS_URI_SUFFIX = 'uploads';
00158 
00164     const RESPONSES_URI_SUFFIX = 'responses';
00165 
00171     const RELATED_URI_SUFFIX = 'related';
00172 
00178     const INBOX_URI_SUFFIX = 'inbox';
00179 
00185     public static $namespaces = array(
00186         array('yt', 'http://gdata.youtube.com/schemas/2007', 1, 0),
00187         array('georss', 'http://www.georss.org/georss', 1, 0),
00188         array('gml', 'http://www.opengis.net/gml', 1, 0),
00189         array('media', 'http://search.yahoo.com/mrss/', 1, 0)
00190     );
00191 
00202     public function __construct($client = null,
00203         $applicationId = 'MyCompany-MyApp-1.0', $clientId = null,
00204         $developerKey = null)
00205     {
00206         $this->registerPackage('Zend_Gdata_YouTube');
00207         $this->registerPackage('Zend_Gdata_YouTube_Extension');
00208         $this->registerPackage('Zend_Gdata_Media');
00209         $this->registerPackage('Zend_Gdata_Media_Extension');
00210 
00211         // NOTE This constructor no longer calls the parent constructor
00212         $this->setHttpClient($client, $applicationId, $clientId, $developerKey);
00213     }
00214 
00222     public function setHttpClient($client,
00223         $applicationId = 'MyCompany-MyApp-1.0', $clientId = null,
00224         $developerKey = null)
00225     {
00226         if ($client === null) {
00227             $client = new Zend_Http_Client();
00228         }
00229         if (!$client instanceof Zend_Http_Client) {
00230             require_once 'Zend/Gdata/App/HttpException.php';
00231             throw new Zend_Gdata_App_HttpException(
00232                 'Argument is not an instance of Zend_Http_Client.');
00233         }
00234 
00235         if ($clientId != null) {
00236             $client->setHeaders('X-GData-Client', $clientId);
00237         }
00238 
00239         if ($developerKey != null) {
00240             $client->setHeaders('X-GData-Key', 'key='. $developerKey);
00241         }
00242 
00243         return parent::setHttpClient($client, $applicationId);
00244     }
00245 
00254     public function getVideoFeed($location = null)
00255     {
00256         if ($location == null) {
00257             $uri = self::VIDEO_URI;
00258         } else if ($location instanceof Zend_Gdata_Query) {
00259             $uri = $location->getQueryUrl();
00260         } else {
00261             $uri = $location;
00262         }
00263         return parent::getFeed($uri, 'Zend_Gdata_YouTube_VideoFeed');
00264     }
00265 
00279     public function getVideoEntry($videoId = null, $location = null,
00280         $fullEntry = false)
00281     {
00282         if ($videoId !== null) {
00283             if ($fullEntry) {
00284                 return $this->getFullVideoEntry($videoId);
00285             } else {
00286                 $uri = self::VIDEO_URI . "/" . $videoId;
00287             }
00288         } else if ($location instanceof Zend_Gdata_Query) {
00289             $uri = $location->getQueryUrl();
00290         } else {
00291             $uri = $location;
00292         }
00293         return parent::getEntry($uri, 'Zend_Gdata_YouTube_VideoEntry');
00294     }
00295 
00305     public function getFullVideoEntry($videoId)
00306     {
00307         $uri = self::USER_URI . "/default/" .
00308             self::UPLOADS_URI_SUFFIX . "/$videoId";
00309         return parent::getEntry($uri, 'Zend_Gdata_YouTube_VideoEntry');
00310     }
00311 
00321     public function getRelatedVideoFeed($videoId = null, $location = null)
00322     {
00323         if ($videoId !== null) {
00324             $uri = self::VIDEO_URI . "/" . $videoId . "/" .
00325                 self::RELATED_URI_SUFFIX;
00326         } else if ($location instanceof Zend_Gdata_Query) {
00327             $uri = $location->getQueryUrl();
00328         } else {
00329             $uri = $location;
00330         }
00331         return parent::getFeed($uri, 'Zend_Gdata_YouTube_VideoFeed');
00332     }
00333 
00343     public function getVideoResponseFeed($videoId = null, $location = null)
00344     {
00345         if ($videoId !== null) {
00346             $uri = self::VIDEO_URI . "/" . $videoId . "/" .
00347                 self::RESPONSES_URI_SUFFIX;
00348         } else if ($location instanceof Zend_Gdata_Query) {
00349             $uri = $location->getQueryUrl();
00350         } else {
00351             $uri = $location;
00352         }
00353         return parent::getFeed($uri, 'Zend_Gdata_YouTube_VideoFeed');
00354     }
00355 
00365     public function getVideoCommentFeed($videoId = null, $location = null)
00366     {
00367         if ($videoId !== null) {
00368             $uri = self::VIDEO_URI . "/" . $videoId . "/comments";
00369         } else if ($location instanceof Zend_Gdata_Query) {
00370             $uri = $location->getQueryUrl();
00371         } else {
00372             $uri = $location;
00373         }
00374         return parent::getFeed($uri, 'Zend_Gdata_YouTube_CommentFeed');
00375     }
00376 
00385     public function getTopRatedVideoFeed($location = null)
00386     {
00387         $standardFeedUri = self::STANDARD_TOP_RATED_URI;
00388 
00389         if ($this->getMajorProtocolVersion() == 2) {
00390             $standardFeedUri = self::STANDARD_TOP_RATED_URI_V2;
00391         }
00392 
00393         if ($location == null) {
00394             $uri = $standardFeedUri;
00395         } else if ($location instanceof Zend_Gdata_Query) {
00396             if ($location instanceof Zend_Gdata_YouTube_VideoQuery) {
00397                 if (!isset($location->url)) {
00398                     $location->setFeedType('top rated');
00399                 }
00400             }
00401             $uri = $location->getQueryUrl();
00402         } else {
00403             $uri = $location;
00404         }
00405         return parent::getFeed($uri, 'Zend_Gdata_YouTube_VideoFeed');
00406     }
00407 
00408 
00417     public function getMostViewedVideoFeed($location = null)
00418     {
00419         $standardFeedUri = self::STANDARD_MOST_VIEWED_URI;
00420 
00421         if ($this->getMajorProtocolVersion() == 2) {
00422             $standardFeedUri = self::STANDARD_MOST_VIEWED_URI_V2;
00423         }
00424 
00425         if ($location == null) {
00426             $uri = $standardFeedUri;
00427         } else if ($location instanceof Zend_Gdata_Query) {
00428             if ($location instanceof Zend_Gdata_YouTube_VideoQuery) {
00429                 if (!isset($location->url)) {
00430                     $location->setFeedType('most viewed');
00431                 }
00432             }
00433             $uri = $location->getQueryUrl();
00434         } else {
00435             $uri = $location;
00436         }
00437         return parent::getFeed($uri, 'Zend_Gdata_YouTube_VideoFeed');
00438     }
00439 
00448     public function getRecentlyFeaturedVideoFeed($location = null)
00449     {
00450         $standardFeedUri = self::STANDARD_RECENTLY_FEATURED_URI;
00451 
00452         if ($this->getMajorProtocolVersion() == 2) {
00453             $standardFeedUri = self::STANDARD_RECENTLY_FEATURED_URI_V2;
00454         }
00455 
00456         if ($location == null) {
00457             $uri = $standardFeedUri;
00458         } else if ($location instanceof Zend_Gdata_Query) {
00459             if ($location instanceof Zend_Gdata_YouTube_VideoQuery) {
00460                 if (!isset($location->url)) {
00461                     $location->setFeedType('recently featured');
00462                 }
00463             }
00464             $uri = $location->getQueryUrl();
00465         } else {
00466             $uri = $location;
00467         }
00468         return parent::getFeed($uri, 'Zend_Gdata_YouTube_VideoFeed');
00469     }
00470 
00480     public function getWatchOnMobileVideoFeed($location = null)
00481     {
00482         $standardFeedUri = self::STANDARD_WATCH_ON_MOBILE_URI;
00483 
00484         if ($this->getMajorProtocolVersion() == 2) {
00485             $standardFeedUri = self::STANDARD_WATCH_ON_MOBILE_URI_V2;
00486         }
00487 
00488         if ($location == null) {
00489             $uri = $standardFeedUri;
00490         } else if ($location instanceof Zend_Gdata_Query) {
00491             if ($location instanceof Zend_Gdata_YouTube_VideoQuery) {
00492                 if (!isset($location->url)) {
00493                     $location->setFeedType('watch on mobile');
00494                 }
00495             }
00496             $uri = $location->getQueryUrl();
00497         } else {
00498             $uri = $location;
00499         }
00500         return parent::getFeed($uri, 'Zend_Gdata_YouTube_VideoFeed');
00501     }
00502 
00511     public function getPlaylistListFeed($user = null, $location = null)
00512     {
00513         if ($user !== null) {
00514             $uri = self::USER_URI . '/' . $user . '/playlists';
00515         } else if ($location instanceof Zend_Gdata_Query) {
00516             $uri = $location->getQueryUrl();
00517         } else {
00518             $uri = $location;
00519         }
00520         return parent::getFeed($uri, 'Zend_Gdata_YouTube_PlaylistListFeed');
00521     }
00522 
00531     public function getPlaylistVideoFeed($location)
00532     {
00533         if ($location instanceof Zend_Gdata_Query) {
00534             $uri = $location->getQueryUrl();
00535         } else {
00536             $uri = $location;
00537         }
00538         return parent::getFeed($uri, 'Zend_Gdata_YouTube_PlaylistVideoFeed');
00539     }
00540 
00549     public function getSubscriptionFeed($user = null, $location = null)
00550     {
00551         if ($user !== null) {
00552             $uri = self::USER_URI . '/' . $user . '/subscriptions';
00553         } else if ($location instanceof Zend_Gdata_Query) {
00554             $uri = $location->getQueryUrl();
00555         } else {
00556             $uri = $location;
00557         }
00558         return parent::getFeed($uri, 'Zend_Gdata_YouTube_SubscriptionFeed');
00559     }
00560 
00569     public function getContactFeed($user = null, $location = null)
00570     {
00571         if ($user !== null) {
00572             $uri = self::USER_URI . '/' . $user . '/contacts';
00573         } else if ($location instanceof Zend_Gdata_Query) {
00574             $uri = $location->getQueryUrl();
00575         } else {
00576             $uri = $location;
00577         }
00578         return parent::getFeed($uri, 'Zend_Gdata_YouTube_ContactFeed');
00579     }
00580 
00589     public function getUserUploads($user = null, $location = null)
00590     {
00591         if ($user !== null) {
00592             $uri = self::USER_URI . '/' . $user . '/' .
00593                    self::UPLOADS_URI_SUFFIX;
00594         } else if ($location instanceof Zend_Gdata_Query) {
00595             $uri = $location->getQueryUrl();
00596         } else {
00597             $uri = $location;
00598         }
00599         return parent::getFeed($uri, 'Zend_Gdata_YouTube_VideoFeed');
00600     }
00601 
00610     public function getUserFavorites($user = null, $location = null)
00611     {
00612         if ($user !== null) {
00613             $uri = self::USER_URI . '/' . $user . '/' .
00614                    self::FAVORITES_URI_SUFFIX;
00615         } else if ($location instanceof Zend_Gdata_Query) {
00616             $uri = $location->getQueryUrl();
00617         } else {
00618             $uri = $location;
00619         }
00620         return parent::getFeed($uri, 'Zend_Gdata_YouTube_VideoFeed');
00621     }
00622 
00631     public function getUserProfile($user = null, $location = null)
00632     {
00633         if ($user !== null) {
00634             $uri = self::USER_URI . '/' . $user;
00635         } else if ($location instanceof Zend_Gdata_Query) {
00636             $uri = $location->getQueryUrl();
00637         } else {
00638             $uri = $location;
00639         }
00640         return parent::getEntry($uri, 'Zend_Gdata_YouTube_UserProfileEntry');
00641     }
00642 
00650     public static function parseFormUploadTokenResponse($response)
00651     {
00652         // Load the feed as an XML DOMDocument object
00653         @ini_set('track_errors', 1);
00654         $doc = new DOMDocument();
00655         $success = @$doc->loadXML($response);
00656         @ini_restore('track_errors');
00657 
00658         if (!$success) {
00659             require_once 'Zend/Gdata/App/Exception.php';
00660             throw new Zend_Gdata_App_Exception(
00661                 "Zend_Gdata_YouTube::parseFormUploadTokenResponse - " .
00662                 "DOMDocument cannot parse XML: $php_errormsg");
00663         }
00664         $responseElement = $doc->getElementsByTagName('response')->item(0);
00665 
00666         $urlText = null;
00667         $tokenText = null;
00668         if ($responseElement != null) {
00669             $urlElement =
00670                 $responseElement->getElementsByTagName('url')->item(0);
00671             $tokenElement =
00672                 $responseElement->getElementsByTagName('token')->item(0);
00673 
00674             if ($urlElement && $urlElement->hasChildNodes() &&
00675                 $tokenElement && $tokenElement->hasChildNodes()) {
00676 
00677                 $urlText = $urlElement->firstChild->nodeValue;
00678                 $tokenText = $tokenElement->firstChild->nodeValue;
00679             }
00680         }
00681 
00682         if ($tokenText != null && $urlText != null) {
00683             return array('token' => $tokenText, 'url' => $urlText);
00684         } else {
00685             require_once 'Zend/Gdata/App/Exception.php';
00686             throw new Zend_Gdata_App_Exception(
00687                 'Form upload token not found in response');
00688         }
00689     }
00690 
00699     public function getFormUploadToken($videoEntry,
00700         $url='http://gdata.youtube.com/action/GetUploadToken')
00701     {
00702         if ($url != null && is_string($url)) {
00703             // $response is a Zend_Http_response object
00704             $response = $this->post($videoEntry, $url);
00705             return self::parseFormUploadTokenResponse($response->getBody());
00706         } else {
00707             require_once 'Zend/Gdata/App/Exception.php';
00708             throw new Zend_Gdata_App_Exception(
00709                 'Url must be provided as a string URL');
00710         }
00711     }
00712 
00722     public function getActivityForUser($username)
00723     {
00724         if ($this->getMajorProtocolVersion() == 1) {
00725             require_once 'Zend/Gdata/App/VersionException.php';
00726             throw new Zend_Gdata_App_VersionException('User activity feeds ' .
00727                 'are not available in API version 1.');
00728         }
00729 
00730         $uri = null;
00731         if ($username instanceof Zend_Gdata_Query) {
00732             $uri = $username->getQueryUrl();
00733         } else {
00734             if (count(explode(',', $username)) >
00735                 self::ACTIVITY_FEED_MAX_USERS) {
00736                 require_once 'Zend/Gdata/App/InvalidArgumentException.php';
00737                 throw new Zend_Gdata_App_InvalidArgumentException(
00738                     'Activity feed can only retrieve for activity for up to ' .
00739                     self::ACTIVITY_FEED_MAX_USERS .  ' users per request');
00740             }
00741             $uri = self::ACTIVITY_FEED_URI . '?author=' . $username;
00742         }
00743 
00744         return parent::getFeed($uri, 'Zend_Gdata_YouTube_ActivityFeed');
00745     }
00746 
00753     public function getFriendActivityForCurrentUser()
00754     {
00755         if (!$this->isAuthenticated()) {
00756             require_once 'Zend/Gdata/App/Exception.php';
00757             throw new Zend_Gdata_App_Exception('You must be authenticated to ' .
00758                 'use the getFriendActivityForCurrentUser function in Zend_' .
00759                 'Gdata_YouTube.');
00760         }
00761         return parent::getFeed(self::FRIEND_ACTIVITY_FEED_URI,
00762             'Zend_Gdata_YouTube_ActivityFeed');
00763     }
00764 
00771     public function getInboxFeedForCurrentUser()
00772     {
00773         if (!$this->isAuthenticated()) {
00774             require_once 'Zend/Gdata/App/Exception.php';
00775             throw new Zend_Gdata_App_Exception('You must be authenticated to ' .
00776                 'use the getInboxFeedForCurrentUser function in Zend_' .
00777                 'Gdata_YouTube.');
00778         }
00779 
00780         return parent::getFeed(self::INBOX_FEED_URI,
00781             'Zend_Gdata_YouTube_InboxFeed');
00782     }
00783 
00800     public function sendVideoMessage($body, $videoEntry = null,
00801         $videoId = null, $recipientUserName)
00802     {
00803         if (!$videoId && !$videoEntry) {
00804             require_once 'Zend/Gdata/App/InvalidArgumentException.php';
00805             throw new Zend_Gdata_App_InvalidArgumentException(
00806                 'Expecting either a valid videoID or a videoEntry object in ' .
00807                 'Zend_Gdata_YouTube->sendVideoMessage().');
00808         }
00809 
00810         $messageEntry = new Zend_Gdata_YouTube_InboxEntry();
00811 
00812         if ($this->getMajorProtocolVersion() == null ||
00813             $this->getMajorProtocolVersion() == 1) {
00814 
00815             if (!$videoId) {
00816                 $videoId = $videoEntry->getVideoId();
00817             } elseif (strlen($videoId) < 12) {
00818                 //Append the full URI
00819                 $videoId = self::VIDEO_URI . '/' . $videoId;
00820             }
00821 
00822             $messageEntry->setId($this->newId($videoId));
00823             // TODO there seems to be a bug where v1 inbox entries dont
00824             // retain their description...
00825             $messageEntry->setDescription(
00826                 new Zend_Gdata_YouTube_Extension_Description($body));
00827 
00828         } else {
00829             if (!$videoId) {
00830                 $videoId = $videoEntry->getVideoId();
00831                 $videoId = substr($videoId, strrpos($videoId, ':'));
00832             }
00833             $messageEntry->setId($this->newId($videoId));
00834             $messageEntry->setSummary($this->newSummary($body));
00835         }
00836 
00837         $insertUrl = 'http://gdata.youtube.com/feeds/api/users/' .
00838             $recipientUserName . '/inbox';
00839         $response = $this->insertEntry($messageEntry, $insertUrl,
00840             'Zend_Gdata_YouTube_InboxEntry');
00841         return $response;
00842     }
00843 
00853     public function replyToCommentEntry($commentEntry, $commentText)
00854     {
00855         $newComment = $this->newCommentEntry();
00856         $newComment->content = $this->newContent()->setText($commentText);
00857         $commentId = $commentEntry->getId();
00858         $commentIdArray = explode(':', $commentId);
00859 
00860         // create a new link element
00861         $inReplyToLinkHref = self::VIDEO_URI . '/' . $commentIdArray[3] .
00862             '/comments/' . $commentIdArray[5];
00863         $inReplyToLink = $this->newLink($inReplyToLinkHref,
00864             self::IN_REPLY_TO_SCHEME, $type="application/atom+xml");
00865         $links = $newComment->getLink();
00866         $links[] = $inReplyToLink;
00867         $newComment->setLink($links);
00868         $commentFeedPostUrl = self::VIDEO_URI . '/' . $commentIdArray[3] .
00869             '/comments';
00870         return $this->insertEntry($newComment,
00871             $commentFeedPostUrl, 'Zend_Gdata_YouTube_CommentEntry');
00872     }
00873 
00874 }
 All Data Structures Namespaces Files Functions Variables Enumerations