YouTube.php 32 KB

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