YouTube.php 32 KB

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