source: trunk/www.guidonia.net/wp/wp-content/plugins/webtv/Drivers/Zend/Gdata/YouTube.php@ 44

Last change on this file since 44 was 44, checked in by luciano, 14 years ago
File size: 31.0 KB
Line 
1<?php
2
3/**
4 * Zend Framework
5 *
6 * LICENSE
7 *
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.
15 *
16 * @category Zend
17 * @package Zend_Gdata
18 * @subpackage YouTube
19 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
20 * @license http://framework.zend.com/license/new-bsd New BSD License
21 */
22
23/**
24 * @see Zend_Gdata_Media
25 */
26require_once 'Zend/Gdata/Media.php';
27
28/**
29 * @see Zend_Gdata_YouTube_VideoEntry
30 */
31require_once 'Zend/Gdata/YouTube/VideoEntry.php';
32
33/**
34 * @see Zend_Gdata_YouTube_VideoFeed
35 */
36require_once 'Zend/Gdata/YouTube/VideoFeed.php';
37
38/**
39 * @see Zend_Gdata_YouTube_CommentFeed
40 */
41require_once 'Zend/Gdata/YouTube/CommentFeed.php';
42
43/**
44 * @see Zend_Gdata_YouTube_PlaylistListFeed
45 */
46require_once 'Zend/Gdata/YouTube/PlaylistListFeed.php';
47
48/**
49 * @see Zend_Gdata_YouTube_SubscriptionFeed
50 */
51require_once 'Zend/Gdata/YouTube/SubscriptionFeed.php';
52
53/**
54 * @see Zend_Gdata_YouTube_ContactFeed
55 */
56require_once 'Zend/Gdata/YouTube/ContactFeed.php';
57
58/**
59 * @see Zend_Gdata_YouTube_PlaylistVideoFeed
60 */
61require_once 'Zend/Gdata/YouTube/PlaylistVideoFeed.php';
62
63/**
64 * @see Zend_Gdata_YouTube_ActivityFeed
65 */
66require_once 'Zend/Gdata/YouTube/ActivityFeed.php';
67
68/**
69 * @see Zend_Gdata_YouTube_InboxFeed
70 */
71require_once 'Zend/Gdata/YouTube/InboxFeed.php';
72
73
74/**
75 * Service class for interacting with the YouTube Data API.
76 * @link http://code.google.com/apis/youtube/
77 *
78 * @category Zend
79 * @package Zend_Gdata
80 * @subpackage YouTube
81 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
82 * @license http://framework.zend.com/license/new-bsd New BSD License
83 */
84class Zend_Gdata_YouTube extends Zend_Gdata_Media
85{
86
87 const AUTH_SERVICE_NAME = 'youtube';
88 const CLIENTLOGIN_URL = 'https://www.google.com/youtube/accounts/ClientLogin';
89
90 const STANDARD_TOP_RATED_URI = 'http://gdata.youtube.com/feeds/standardfeeds/top_rated';
91 const STANDARD_MOST_VIEWED_URI = 'http://gdata.youtube.com/feeds/standardfeeds/most_viewed';
92 const STANDARD_RECENTLY_FEATURED_URI = 'http://gdata.youtube.com/feeds/standardfeeds/recently_featured';
93 const STANDARD_WATCH_ON_MOBILE_URI = 'http://gdata.youtube.com/feeds/standardfeeds/watch_on_mobile';
94
95 const STANDARD_TOP_RATED_URI_V2 =
96 'http://gdata.youtube.com/feeds/api/standardfeeds/top_rated';
97 const STANDARD_MOST_VIEWED_URI_V2 =
98 'http://gdata.youtube.com/feeds/api/standardfeeds/most_viewed';
99 const STANDARD_RECENTLY_FEATURED_URI_V2 =
100 'http://gdata.youtube.com/feeds/api/standardfeeds/recently_featured';
101 const STANDARD_WATCH_ON_MOBILE_URI_V2 =
102 'http://gdata.youtube.com/feeds/api/standardfeeds/watch_on_mobile';
103
104 const USER_URI = 'http://gdata.youtube.com/feeds/api/users';
105 const VIDEO_URI = 'http://gdata.youtube.com/feeds/api/videos';
106 const PLAYLIST_REL = 'http://gdata.youtube.com/schemas/2007#playlist';
107 const USER_UPLOADS_REL = 'http://gdata.youtube.com/schemas/2007#user.uploads';
108 const USER_PLAYLISTS_REL = 'http://gdata.youtube.com/schemas/2007#user.playlists';
109 const USER_SUBSCRIPTIONS_REL = 'http://gdata.youtube.com/schemas/2007#user.subscriptions';
110 const USER_CONTACTS_REL = 'http://gdata.youtube.com/schemas/2007#user.contacts';
111 const USER_FAVORITES_REL = 'http://gdata.youtube.com/schemas/2007#user.favorites';
112 const VIDEO_RESPONSES_REL = 'http://gdata.youtube.com/schemas/2007#video.responses';
113 const VIDEO_RATINGS_REL = 'http://gdata.youtube.com/schemas/2007#video.ratings';
114 const VIDEO_COMPLAINTS_REL = 'http://gdata.youtube.com/schemas/2007#video.complaints';
115 const ACTIVITY_FEED_URI = 'http://gdata.youtube.com/feeds/api/events';
116 const FRIEND_ACTIVITY_FEED_URI =
117 'http://gdata.youtube.com/feeds/api/users/default/friendsactivity';
118
119 /**
120 * The URI of the in-reply-to schema for comments in reply to
121 * other comments.
122 *
123 * @var string
124 */
125 const IN_REPLY_TO_SCHEME =
126 'http://gdata.youtube.com/schemas/2007#in-reply-to';
127
128 /**
129 * The URI of the inbox feed for the currently authenticated user.
130 *
131 * @var string
132 */
133 const INBOX_FEED_URI =
134 'http://gdata.youtube.com/feeds/api/users/default/inbox';
135
136 /**
137 * The maximum number of users for which activity can be requested for,
138 * as enforced by the API.
139 *
140 * @var integer
141 */
142 const ACTIVITY_FEED_MAX_USERS = 20;
143
144 /**
145 * The suffix for a feed of favorites.
146 *
147 * @var string
148 */
149 const FAVORITES_URI_SUFFIX = 'favorites';
150
151 /**
152 * The suffix for the user's upload feed.
153 *
154 * @var string
155 */
156 const UPLOADS_URI_SUFFIX = 'uploads';
157
158 /**
159 * The suffix for a feed of video responses.
160 *
161 * @var string
162 */
163 const RESPONSES_URI_SUFFIX = 'responses';
164
165 /**
166 * The suffix for a feed of related videos.
167 *
168 * @var string
169 */
170 const RELATED_URI_SUFFIX = 'related';
171
172 /**
173 * The suffix for a feed of messages (inbox entries).
174 *
175 * @var string
176 */
177 const INBOX_URI_SUFFIX = 'inbox';
178
179 /**
180 * Namespaces used for Zend_Gdata_YouTube
181 *
182 * @var array
183 */
184 public static $namespaces = array(
185 array('yt', 'http://gdata.youtube.com/schemas/2007', 1, 0),
186 array('georss', 'http://www.georss.org/georss', 1, 0),
187 array('gml', 'http://www.opengis.net/gml', 1, 0),
188 array('media', 'http://search.yahoo.com/mrss/', 1, 0)
189 );
190
191 /**
192 * Create Zend_Gdata_YouTube object
193 *
194 * @param Zend_Http_Client $client (optional) The HTTP client to use when
195 * when communicating with the Google servers.
196 * @param string $applicationId The identity of the app in the form of
197 * Company-AppName-Version
198 * @param string $clientId The clientId issued by the YouTube dashboard
199 * @param string $developerKey The developerKey issued by the YouTube dashboard
200 */
201 public function __construct($client = null,
202 $applicationId = 'MyCompany-MyApp-1.0', $clientId = null,
203 $developerKey = null)
204 {
205 $this->registerPackage('Zend_Gdata_YouTube');
206 $this->registerPackage('Zend_Gdata_YouTube_Extension');
207 $this->registerPackage('Zend_Gdata_Media');
208 $this->registerPackage('Zend_Gdata_Media_Extension');
209
210 // NOTE This constructor no longer calls the parent constructor
211 $this->setHttpClient($client, $applicationId, $clientId, $developerKey);
212 }
213
214 /**
215 * Set the Zend_Http_Client object used for communication
216 *
217 * @param Zend_Http_Client $client The client to use for communication
218 * @throws Zend_Gdata_App_HttpException
219 * @return Zend_Gdata_App Provides a fluent interface
220 */
221 public function setHttpClient($client,
222 $applicationId = 'MyCompany-MyApp-1.0', $clientId = null,
223 $developerKey = null)
224 {
225 if ($client === null) {
226 $client = new Zend_Http_Client();
227 }
228 if (!$client instanceof Zend_Http_Client) {
229 require_once 'Zend/Gdata/App/HttpException.php';
230 throw new Zend_Gdata_App_HttpException(
231 'Argument is not an instance of Zend_Http_Client.');
232 }
233
234 if ($clientId != null) {
235 $client->setHeaders('X-GData-Client', $clientId);
236 }
237
238 if ($developerKey != null) {
239 $client->setHeaders('X-GData-Key', 'key='. $developerKey);
240 }
241
242 return parent::setHttpClient($client, $applicationId);
243 }
244
245 /**
246 * Retrieves a feed of videos.
247 *
248 * @param mixed $location (optional) The URL to query or a
249 * Zend_Gdata_Query object from which a URL can be determined
250 * @return Zend_Gdata_YouTube_VideoFeed The feed of videos found at the
251 * specified URL.
252 */
253 public function getVideoFeed($location = null)
254 {
255 if ($location == null) {
256 $uri = self::VIDEO_URI;
257 } else if ($location instanceof Zend_Gdata_Query) {
258 $uri = $location->getQueryUrl();
259 } else {
260 $uri = $location;
261 }
262 return parent::getFeed($uri, 'Zend_Gdata_YouTube_VideoFeed');
263 }
264
265 /**
266 * Retrieves a specific video entry.
267 *
268 * @param mixed $videoId The ID of the video to retrieve.
269 * @param mixed $location (optional) The URL to query or a
270 * Zend_Gdata_Query object from which a URL can be determined.
271 * @param boolean $fullEntry (optional) Retrieve the full metadata for the
272 * entry. Only possible if entry belongs to currently authenticated
273 * user. An exception will be thrown otherwise.
274 * @throws Zend_Gdata_App_HttpException
275 * @return Zend_Gdata_YouTube_VideoEntry The video entry found at the
276 * specified URL.
277 */
278 public function getVideoEntry($videoId = null, $location = null,
279 $fullEntry = false)
280 {
281 if ($videoId !== null) {
282 if ($fullEntry) {
283 return $this->getFullVideoEntry($videoId);
284 } else {
285 $uri = self::VIDEO_URI . "/" . $videoId;
286 }
287 } else if ($location instanceof Zend_Gdata_Query) {
288 $uri = $location->getQueryUrl();
289 } else {
290 $uri = $location;
291 }
292 return parent::getEntry($uri, 'Zend_Gdata_YouTube_VideoEntry');
293 }
294
295 /**
296 * Retrieves a video entry from the user's upload feed.
297 *
298 * @param mixed $videoID The ID of the video to retrieve.
299 * @throws Zend_Gdata_App_HttpException
300 * @return Zend_Gdata_YouTube_VideoEntry|null The video entry to be
301 * retrieved, or null if it was not found or the user requesting it
302 * did not have the appropriate permissions.
303 */
304 public function getFullVideoEntry($videoId)
305 {
306 $uri = self::USER_URI . "/default/" .
307 self::UPLOADS_URI_SUFFIX . "/$videoId";
308 return parent::getEntry($uri, 'Zend_Gdata_YouTube_VideoEntry');
309 }
310
311 /**
312 * Retrieves a feed of videos related to the specified video ID.
313 *
314 * @param string $videoId The videoId of interest
315 * @param mixed $location (optional) The URL to query or a
316 * Zend_Gdata_Query object from which a URL can be determined
317 * @return Zend_Gdata_YouTube_VideoFeed The feed of videos found at the
318 * specified URL.
319 */
320 public function getRelatedVideoFeed($videoId = null, $location = null)
321 {
322 if ($videoId !== null) {
323 $uri = self::VIDEO_URI . "/" . $videoId . "/" .
324 self::RELATED_URI_SUFFIX;
325 } else if ($location instanceof Zend_Gdata_Query) {
326 $uri = $location->getQueryUrl();
327 } else {
328 $uri = $location;
329 }
330 return parent::getFeed($uri, 'Zend_Gdata_YouTube_VideoFeed');
331 }
332
333 /**
334 * Retrieves a feed of video responses related to the specified video ID.
335 *
336 * @param string $videoId The videoId of interest
337 * @param mixed $location (optional) The URL to query or a
338 * Zend_Gdata_Query object from which a URL can be determined
339 * @return Zend_Gdata_YouTube_VideoFeed The feed of videos found at the
340 * specified URL.
341 */
342 public function getVideoResponseFeed($videoId = null, $location = null)
343 {
344 if ($videoId !== null) {
345 $uri = self::VIDEO_URI . "/" . $videoId . "/" .
346 self::RESPONSES_URI_SUFFIX;
347 } else if ($location instanceof Zend_Gdata_Query) {
348 $uri = $location->getQueryUrl();
349 } else {
350 $uri = $location;
351 }
352 return parent::getFeed($uri, 'Zend_Gdata_YouTube_VideoFeed');
353 }
354
355 /**
356 * Retrieves a feed of comments related to the specified video ID.
357 *
358 * @param string $videoId The videoId of interest
359 * @param mixed $location (optional) The URL to query or a
360 * Zend_Gdata_Query object from which a URL can be determined
361 * @return Zend_Gdata_YouTube_CommentFeed The feed of videos found at the
362 * specified URL.
363 */
364 public function getVideoCommentFeed($videoId = null, $location = null)
365 {
366 if ($videoId !== null) {
367 $uri = self::VIDEO_URI . "/" . $videoId . "/comments";
368 } else if ($location instanceof Zend_Gdata_Query) {
369 $uri = $location->getQueryUrl();
370 } else {
371 $uri = $location;
372 }
373 return parent::getFeed($uri, 'Zend_Gdata_YouTube_CommentFeed');
374 }
375
376 /**
377 * Retrieves a feed of comments related to the specified video ID.
378 *
379 * @param mixed $location (optional) The URL to query or a
380 * Zend_Gdata_Query object from which a URL can be determined
381 * @return Zend_Gdata_YouTube_CommentFeed The feed of videos found at the
382 * specified URL.
383 */
384 public function getTopRatedVideoFeed($location = null)
385 {
386 $standardFeedUri = self::STANDARD_TOP_RATED_URI;
387
388 if ($this->getMajorProtocolVersion() == 2) {
389 $standardFeedUri = self::STANDARD_TOP_RATED_URI_V2;
390 }
391
392 if ($location == null) {
393 $uri = $standardFeedUri;
394 } else if ($location instanceof Zend_Gdata_Query) {
395 if ($location instanceof Zend_Gdata_YouTube_VideoQuery) {
396 if (!isset($location->url)) {
397 $location->setFeedType('top rated');
398 }
399 }
400 $uri = $location->getQueryUrl();
401 } else {
402 $uri = $location;
403 }
404 return parent::getFeed($uri, 'Zend_Gdata_YouTube_VideoFeed');
405 }
406
407
408 /**
409 * Retrieves a feed of the most viewed videos.
410 *
411 * @param mixed $location (optional) The URL to query or a
412 * Zend_Gdata_Query object from which a URL can be determined
413 * @return Zend_Gdata_YouTube_VideoFeed The feed of videos found at the
414 * specified URL.
415 */
416 public function getMostViewedVideoFeed($location = null)
417 {
418 $standardFeedUri = self::STANDARD_MOST_VIEWED_URI;
419
420 if ($this->getMajorProtocolVersion() == 2) {
421 $standardFeedUri = self::STANDARD_MOST_VIEWED_URI_V2;
422 }
423
424 if ($location == null) {
425 $uri = $standardFeedUri;
426 } else if ($location instanceof Zend_Gdata_Query) {
427 if ($location instanceof Zend_Gdata_YouTube_VideoQuery) {
428 if (!isset($location->url)) {
429 $location->setFeedType('most viewed');
430 }
431 }
432 $uri = $location->getQueryUrl();
433 } else {
434 $uri = $location;
435 }
436 return parent::getFeed($uri, 'Zend_Gdata_YouTube_VideoFeed');
437 }
438
439 /**
440 * Retrieves a feed of recently featured videos.
441 *
442 * @param mixed $location (optional) The URL to query or a
443 * Zend_Gdata_Query object from which a URL can be determined
444 * @return Zend_Gdata_YouTube_VideoFeed The feed of videos found at the
445 * specified URL.
446 */
447 public function getRecentlyFeaturedVideoFeed($location = null)
448 {
449 $standardFeedUri = self::STANDARD_RECENTLY_FEATURED_URI;
450
451 if ($this->getMajorProtocolVersion() == 2) {
452 $standardFeedUri = self::STANDARD_RECENTLY_FEATURED_URI_V2;
453 }
454
455 if ($location == null) {
456 $uri = $standardFeedUri;
457 } else if ($location instanceof Zend_Gdata_Query) {
458 if ($location instanceof Zend_Gdata_YouTube_VideoQuery) {
459 if (!isset($location->url)) {
460 $location->setFeedType('recently featured');
461 }
462 }
463 $uri = $location->getQueryUrl();
464 } else {
465 $uri = $location;
466 }
467 return parent::getFeed($uri, 'Zend_Gdata_YouTube_VideoFeed');
468 }
469
470 /**
471 * Retrieves a feed of videos recently featured for mobile devices.
472 * These videos will have RTSP links in the $entry->mediaGroup->content
473 *
474 * @param mixed $location (optional) The URL to query or a
475 * Zend_Gdata_Query object from which a URL can be determined
476 * @return Zend_Gdata_YouTube_VideoFeed The feed of videos found at the
477 * specified URL.
478 */
479 public function getWatchOnMobileVideoFeed($location = null)
480 {
481 $standardFeedUri = self::STANDARD_WATCH_ON_MOBILE_URI;
482
483 if ($this->getMajorProtocolVersion() == 2) {
484 $standardFeedUri = self::STANDARD_WATCH_ON_MOBILE_URI_V2;
485 }
486
487 if ($location == null) {
488 $uri = $standardFeedUri;
489 } else if ($location instanceof Zend_Gdata_Query) {
490 if ($location instanceof Zend_Gdata_YouTube_VideoQuery) {
491 if (!isset($location->url)) {
492 $location->setFeedType('watch on mobile');
493 }
494 }
495 $uri = $location->getQueryUrl();
496 } else {
497 $uri = $location;
498 }
499 return parent::getFeed($uri, 'Zend_Gdata_YouTube_VideoFeed');
500 }
501
502 /**
503 * Retrieves a feed which lists a user's playlist
504 *
505 * @param string $user (optional) The username of interest
506 * @param mixed $location (optional) The URL to query or a
507 * Zend_Gdata_Query object from which a URL can be determined
508 * @return Zend_Gdata_YouTube_PlaylistListFeed The feed of playlists
509 */
510 public function getPlaylistListFeed($user = null, $location = null)
511 {
512 if ($user !== null) {
513 $uri = self::USER_URI . '/' . $user . '/playlists';
514 } else if ($location instanceof Zend_Gdata_Query) {
515 $uri = $location->getQueryUrl();
516 } else {
517 $uri = $location;
518 }
519 return parent::getFeed($uri, 'Zend_Gdata_YouTube_PlaylistListFeed');
520 }
521
522 /**
523 * Retrieves a feed of videos in a particular playlist
524 *
525 * @param mixed $location (optional) The URL to query or a
526 * Zend_Gdata_Query object from which a URL can be determined
527 * @return Zend_Gdata_YouTube_PlaylistVideoFeed The feed of videos found at
528 * the specified URL.
529 */
530 public function getPlaylistVideoFeed($location)
531 {
532 if ($location instanceof Zend_Gdata_Query) {
533 $uri = $location->getQueryUrl();
534 } else {
535 $uri = $location;
536 }
537 return parent::getFeed($uri, 'Zend_Gdata_YouTube_PlaylistVideoFeed');
538 }
539
540 /**
541 * Retrieves a feed of a user's subscriptions
542 *
543 * @param string $user (optional) The username of interest
544 * @param mixed $location (optional) The URL to query or a
545 * Zend_Gdata_Query object from which a URL can be determined
546 * @return Zend_Gdata_YouTube_SubscriptionListFeed The feed of subscriptions
547 */
548 public function getSubscriptionFeed($user = null, $location = null)
549 {
550 if ($user !== null) {
551 $uri = self::USER_URI . '/' . $user . '/subscriptions';
552 } else if ($location instanceof Zend_Gdata_Query) {
553 $uri = $location->getQueryUrl();
554 } else {
555 $uri = $location;
556 }
557 return parent::getFeed($uri, 'Zend_Gdata_YouTube_SubscriptionFeed');
558 }
559
560 /**
561 * Retrieves a feed of a user's contacts
562 *
563 * @param string $user (optional) The username of interest
564 * @param mixed $location (optional) The URL to query or a
565 * Zend_Gdata_Query object from which a URL can be determined
566 * @return Zend_Gdata_YouTube_ContactFeed The feed of contacts
567 */
568 public function getContactFeed($user = null, $location = null)
569 {
570 if ($user !== null) {
571 $uri = self::USER_URI . '/' . $user . '/contacts';
572 } else if ($location instanceof Zend_Gdata_Query) {
573 $uri = $location->getQueryUrl();
574 } else {
575 $uri = $location;
576 }
577 return parent::getFeed($uri, 'Zend_Gdata_YouTube_ContactFeed');
578 }
579
580 /**
581 * Retrieves a user's uploads
582 *
583 * @param string $user (optional) The username of interest
584 * @param mixed $location (optional) The URL to query or a
585 * Zend_Gdata_Query object from which a URL can be determined
586 * @return Zend_Gdata_YouTube_VideoFeed The videos uploaded by the user
587 */
588 public function getUserUploads($user = null, $location = null)
589 {
590 if ($user !== null) {
591 $uri = self::USER_URI . '/' . $user . '/' .
592 self::UPLOADS_URI_SUFFIX;
593 } else if ($location instanceof Zend_Gdata_Query) {
594 $uri = $location->getQueryUrl();
595 } else {
596 $uri = $location;
597 }
598 return parent::getFeed($uri, 'Zend_Gdata_YouTube_VideoFeed');
599 }
600
601 /**
602 * Retrieves a user's favorites
603 *
604 * @param string $user (optional) The username of interest
605 * @param mixed $location (optional) The URL to query or a
606 * Zend_Gdata_Query object from which a URL can be determined
607 * @return Zend_Gdata_YouTube_VideoFeed The videos favorited by the user
608 */
609 public function getUserFavorites($user = null, $location = null)
610 {
611 if ($user !== null) {
612 $uri = self::USER_URI . '/' . $user . '/' .
613 self::FAVORITES_URI_SUFFIX;
614 } else if ($location instanceof Zend_Gdata_Query) {
615 $uri = $location->getQueryUrl();
616 } else {
617 $uri = $location;
618 }
619 return parent::getFeed($uri, 'Zend_Gdata_YouTube_VideoFeed');
620 }
621
622 /**
623 * Retrieves a user's profile as an entry
624 *
625 * @param string $user (optional) The username of interest
626 * @param mixed $location (optional) The URL to query or a
627 * Zend_Gdata_Query object from which a URL can be determined
628 * @return Zend_Gdata_YouTube_UserProfileEntry The user profile entry
629 */
630 public function getUserProfile($user = null, $location = null)
631 {
632 if ($user !== null) {
633 $uri = self::USER_URI . '/' . $user;
634 } else if ($location instanceof Zend_Gdata_Query) {
635 $uri = $location->getQueryUrl();
636 } else {
637 $uri = $location;
638 }
639 return parent::getEntry($uri, 'Zend_Gdata_YouTube_UserProfileEntry');
640 }
641
642 /**
643 * Helper function for parsing a YouTube token response
644 *
645 * @param string $response The service response
646 * @throws Zend_Gdata_App_Exception
647 * @return array An array containing the token and URL
648 */
649 public static function parseFormUploadTokenResponse($response)
650 {
651 // Load the feed as an XML DOMDocument object
652 @ini_set('track_errors', 1);
653 $doc = new DOMDocument();
654 $success = @$doc->loadXML($response);
655 @ini_restore('track_errors');
656
657 if (!$success) {
658 require_once 'Zend/Gdata/App/Exception.php';
659 throw new Zend_Gdata_App_Exception(
660 "Zend_Gdata_YouTube::parseFormUploadTokenResponse - " .
661 "DOMDocument cannot parse XML: $php_errormsg");
662 }
663 $responseElement = $doc->getElementsByTagName('response')->item(0);
664
665 $urlText = null;
666 $tokenText = null;
667 if ($responseElement != null) {
668 $urlElement =
669 $responseElement->getElementsByTagName('url')->item(0);
670 $tokenElement =
671 $responseElement->getElementsByTagName('token')->item(0);
672
673 if ($urlElement && $urlElement->hasChildNodes() &&
674 $tokenElement && $tokenElement->hasChildNodes()) {
675
676 $urlText = $urlElement->firstChild->nodeValue;
677 $tokenText = $tokenElement->firstChild->nodeValue;
678 }
679 }
680
681 if ($tokenText != null && $urlText != null) {
682 return array('token' => $tokenText, 'url' => $urlText);
683 } else {
684 require_once 'Zend/Gdata/App/Exception.php';
685 throw new Zend_Gdata_App_Exception(
686 'Form upload token not found in response');
687 }
688 }
689
690 /**
691 * Retrieves a YouTube token
692 *
693 * @param Zend_Gdata_YouTube_VideoEntry $videoEntry The video entry
694 * @param string $url The location as a string URL
695 * @throws Zend_Gdata_App_Exception
696 * @return array An array containing a token and URL
697 */
698 public function getFormUploadToken($videoEntry,
699 $url='http://gdata.youtube.com/action/GetUploadToken')
700 {
701 if ($url != null && is_string($url)) {
702 // $response is a Zend_Http_response object
703 $response = $this->post($videoEntry, $url);
704 return self::parseFormUploadTokenResponse($response->getBody());
705 } else {
706 require_once 'Zend/Gdata/App/Exception.php';
707 throw new Zend_Gdata_App_Exception(
708 'Url must be provided as a string URL');
709 }
710 }
711
712 /**
713 * Retrieves the activity feed for users
714 *
715 * @param mixed $usernames A string identifying the usernames for which to
716 * retrieve activity for. This can also be a Zend_Gdata_Query
717 * object from which a URL can be determined.
718 * @throws Zend_Gdata_App_VersionException if using version less than 2.
719 * @return Zend_Gdata_YouTube_ActivityFeed
720 */
721 public function getActivityForUser($username)
722 {
723 if ($this->getMajorProtocolVersion() == 1) {
724 require_once 'Zend/Gdata/App/VersionException.php';
725 throw new Zend_Gdata_App_VersionException('User activity feeds ' .
726 'are not available in API version 1.');
727 }
728
729 $uri = null;
730 if ($username instanceof Zend_Gdata_Query) {
731 $uri = $username->getQueryUrl();
732 } else {
733 if (count(explode(',', $username)) >
734 self::ACTIVITY_FEED_MAX_USERS) {
735 require_once 'Zend/Gdata/App/InvalidArgumentException.php';
736 throw new Zend_Gdata_App_InvalidArgumentException(
737 'Activity feed can only retrieve for activity for up to ' .
738 self::ACTIVITY_FEED_MAX_USERS . ' users per request');
739 }
740 $uri = self::ACTIVITY_FEED_URI . '?author=' . $username;
741 }
742
743 return parent::getFeed($uri, 'Zend_Gdata_YouTube_ActivityFeed');
744 }
745
746 /**
747 * Retrieve the activity of the currently authenticated users friend.
748 *
749 * @throws Zend_Gdata_App_Exception if not logged in.
750 * @return Zend_Gdata_YouTube_ActivityFeed
751 */
752 public function getFriendActivityForCurrentUser()
753 {
754 if (!$this->isAuthenticated()) {
755 require_once 'Zend/Gdata/App/Exception.php';
756 throw new Zend_Gdata_App_Exception('You must be authenticated to ' .
757 'use the getFriendActivityForCurrentUser function in Zend_' .
758 'Gdata_YouTube.');
759 }
760 return parent::getFeed(self::FRIEND_ACTIVITY_FEED_URI,
761 'Zend_Gdata_YouTube_ActivityFeed');
762 }
763
764 /**
765 * Retrieve a feed of messages in the currently authenticated user's inbox.
766 *
767 * @throws Zend_Gdata_App_Exception if not logged in.
768 * @return Zend_Gdata_YouTube_InboxFeed|null
769 */
770 public function getInboxFeedForCurrentUser()
771 {
772 if (!$this->isAuthenticated()) {
773 require_once 'Zend/Gdata/App/Exception.php';
774 throw new Zend_Gdata_App_Exception('You must be authenticated to ' .
775 'use the getInboxFeedForCurrentUser function in Zend_' .
776 'Gdata_YouTube.');
777 }
778
779 return parent::getFeed(self::INBOX_FEED_URI,
780 'Zend_Gdata_YouTube_InboxFeed');
781 }
782
783 /**
784 * Send a video message.
785 *
786 * Note: Either a Zend_Gdata_YouTube_VideoEntry or a valid video ID must
787 * be provided.
788 *
789 * @param string $body The body of the message
790 * @param Zend_Gdata_YouTube_VideoEntry (optional) The video entry to send
791 * @param string $videoId The id of the video to send
792 * @param string $recipientUserName The username of the recipient
793 * @throws Zend_Gdata_App_InvalidArgumentException if no valid
794 * Zend_Gdata_YouTube_VideoEntry or videoId were provided
795 * @return Zend_Gdata_YouTube_InboxEntry|null The
796 * Zend_Gdata_YouTube_Inbox_Entry representing the sent message.
797 *
798 */
799 public function sendVideoMessage($body, $videoEntry = null,
800 $videoId = null, $recipientUserName)
801 {
802 if (!$videoId && !$videoEntry) {
803 require_once 'Zend/Gdata/App/InvalidArgumentException.php';
804 throw new Zend_Gdata_App_InvalidArgumentException(
805 'Expecting either a valid videoID or a videoEntry object in ' .
806 'Zend_Gdata_YouTube->sendVideoMessage().');
807 }
808
809 $messageEntry = new Zend_Gdata_YouTube_InboxEntry();
810
811 if ($this->getMajorProtocolVersion() == null ||
812 $this->getMajorProtocolVersion() == 1) {
813
814 if (!$videoId) {
815 $videoId = $videoEntry->getVideoId();
816 } elseif (strlen($videoId) < 12) {
817 //Append the full URI
818 $videoId = self::VIDEO_URI . '/' . $videoId;
819 }
820
821 $messageEntry->setId($this->newId($videoId));
822 // TODO there seems to be a bug where v1 inbox entries dont
823 // retain their description...
824 $messageEntry->setDescription(
825 new Zend_Gdata_YouTube_Extension_Description($body));
826
827 } else {
828 if (!$videoId) {
829 $videoId = $videoEntry->getVideoId();
830 $videoId = substr($videoId, strrpos($videoId, ':'));
831 }
832 $messageEntry->setId($this->newId($videoId));
833 $messageEntry->setSummary($this->newSummary($body));
834 }
835
836 $insertUrl = 'http://gdata.youtube.com/feeds/api/users/' .
837 $recipientUserName . '/inbox';
838 $response = $this->insertEntry($messageEntry, $insertUrl,
839 'Zend_Gdata_YouTube_InboxEntry');
840 return $response;
841 }
842
843 /**
844 * Post a comment in reply to an existing comment
845 *
846 * @param $commentEntry Zend_Gdata_YouTube_CommentEntry The comment entry
847 * to reply to
848 * @param $commentText string The text of the comment to post
849 * @return A Zend_Gdata_YouTube_CommentEntry representing the posted
850 * comment
851 */
852 public function replyToCommentEntry($commentEntry, $commentText)
853 {
854 $newComment = $this->newCommentEntry();
855 $newComment->content = $this->newContent()->setText($commentText);
856 $commentId = $commentEntry->getId();
857 $commentIdArray = explode(':', $commentId);
858
859 // create a new link element
860 $inReplyToLinkHref = self::VIDEO_URI . '/' . $commentIdArray[3] .
861 '/comments/' . $commentIdArray[5];
862 $inReplyToLink = $this->newLink($inReplyToLinkHref,
863 self::IN_REPLY_TO_SCHEME, $type="application/atom+xml");
864 $links = $newComment->getLink();
865 $links[] = $inReplyToLink;
866 $newComment->setLink($links);
867 $commentFeedPostUrl = self::VIDEO_URI . '/' . $commentIdArray[3] .
868 '/comments';
869 return $this->insertEntry($newComment,
870 $commentFeedPostUrl, 'Zend_Gdata_YouTube_CommentEntry');
871 }
872
873}
Note: See TracBrowser for help on using the repository browser.