2
0

YouTube.php 31 KB

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