PageRenderTime 60ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Joomla/Twitter/Statuses.php

https://github.com/piotr-cz/joomla-framework
PHP | 654 lines | 288 code | 89 blank | 277 comment | 46 complexity | 55b084865f1ba8894714ac766f203890 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Part of the Joomla Framework Twitter Package
  4. *
  5. * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  6. * @license GNU General Public License version 2 or later; see LICENSE
  7. */
  8. namespace Joomla\Twitter;
  9. /**
  10. * Twitter API Statuses class for the Joomla Framework.
  11. *
  12. * @since 1.0
  13. */
  14. class Statuses extends Object
  15. {
  16. /**
  17. * Method to get a single tweet with the given ID.
  18. *
  19. * @param integer $id The ID of the tweet to retrieve.
  20. * @param boolean $trim_user When set to true, each tweet returned in a timeline will include a user object including only
  21. * the status author's numerical ID.
  22. * @param boolean $entities When set to true, each tweet will include a node called "entities,". This node offers a variety of metadata
  23. * about the tweet in a discreet structure, including: user_mentions, urls, and hashtags.
  24. * @param boolean $my_retweet When set to either true, t or 1, any statuses returned that have been retweeted by the authenticating user will
  25. * include an additional current_user_retweet node, containing the ID of the source status for the retweet.
  26. *
  27. * @return array The decoded JSON response
  28. *
  29. * @since 1.0
  30. */
  31. public function getTweetById($id, $trim_user = null, $entities = null, $my_retweet = null)
  32. {
  33. // Check the rate limit for remaining hits
  34. $this->checkRateLimit("statuses", "show/:id");
  35. // Set the API base
  36. $path = '/statuses/show/' . $id . '.json';
  37. $data = array();
  38. // Check if trim_user is specified
  39. if (!is_null($trim_user))
  40. {
  41. $data['trim_user'] = $trim_user;
  42. }
  43. // Check if entities is specified
  44. if (!is_null($entities))
  45. {
  46. $data['include_entities'] = $entities;
  47. }
  48. // Check if my_retweet is specified
  49. if (!is_null($my_retweet))
  50. {
  51. $data['include_my_retweet'] = $my_retweet;
  52. }
  53. // Send the request.
  54. return $this->sendRequest($path, 'GET', $data);
  55. }
  56. /**
  57. * Method to retrieve the latest statuses from the specified user timeline.
  58. *
  59. * @param mixed $user Either an integer containing the user ID or a string containing the screen name.
  60. * @param integer $count Specifies the number of tweets to try and retrieve, up to a maximum of 200. Retweets are always included
  61. * in the count, so it is always suggested to set $include_rts to true
  62. * @param boolean $include_rts When set to true, the timeline will contain native retweets in addition to the standard stream of tweets.
  63. * @param boolean $no_replies This parameter will prevent replies from appearing in the returned timeline. This parameter is only supported
  64. * for JSON and XML responses.
  65. * @param integer $since_id Returns results with an ID greater than (that is, more recent than) the specified ID.
  66. * @param integer $max_id Returns results with an ID less than (that is, older than) the specified ID.
  67. * @param boolean $trim_user When set to true, each tweet returned in a timeline will include a user object including only
  68. * the status author's numerical ID.
  69. * @param boolean $contributor This parameter enhances the contributors element of the status response to include the screen_name of the
  70. * contributor. By default only the user_id of the contributor is included.
  71. *
  72. * @return array The decoded JSON response
  73. *
  74. * @since 1.0
  75. * @throws \RuntimeException
  76. */
  77. public function getUserTimeline($user, $count = 20, $include_rts = null, $no_replies = null, $since_id = 0, $max_id = 0, $trim_user = null,
  78. $contributor = null)
  79. {
  80. // Check the rate limit for remaining hits
  81. $this->checkRateLimit('statuses', 'user_timeline');
  82. $data = array();
  83. // Determine which type of data was passed for $user
  84. if (is_numeric($user))
  85. {
  86. $data['user_id'] = $user;
  87. }
  88. elseif (is_string($user))
  89. {
  90. $data['screen_name'] = $user;
  91. }
  92. else
  93. {
  94. // We don't have a valid entry
  95. throw new \RuntimeException('The specified username is not in the correct format; must use integer or string');
  96. }
  97. // Set the API base
  98. $path = '/statuses/user_timeline.json';
  99. // Set the count string
  100. $data['count'] = $count;
  101. // Check if include_rts is specified
  102. if (!is_null($include_rts))
  103. {
  104. $data['include_rts'] = $include_rts;
  105. }
  106. // Check if no_replies is specified
  107. if (!is_null($no_replies))
  108. {
  109. $data['exclude_replies'] = $no_replies;
  110. }
  111. // Check if a since_id is specified
  112. if ($since_id > 0)
  113. {
  114. $data['since_id'] = (int) $since_id;
  115. }
  116. // Check if a max_id is specified
  117. if ($max_id > 0)
  118. {
  119. $data['max_id'] = (int) $max_id;
  120. }
  121. // Check if trim_user is specified
  122. if (!is_null($trim_user))
  123. {
  124. $data['trim_user'] = $trim_user;
  125. }
  126. // Check if contributor details is specified
  127. if (!is_null($contributor))
  128. {
  129. $data['contributor_details'] = $contributor;
  130. }
  131. // Send the request.
  132. return $this->sendRequest($path, 'GET', $data);
  133. }
  134. /**
  135. * Method to post a tweet.
  136. *
  137. * @param string $status The text of the tweet.
  138. * @param integer $in_reply_to_status_id The ID of an existing status that the update is in reply to.
  139. * @param float $lat The latitude of the location this tweet refers to.
  140. * @param float $long The longitude of the location this tweet refers to.
  141. * @param string $place_id A place in the world.
  142. * @param boolean $display_coordinates Whether or not to put a pin on the exact coordinates a tweet has been sent from.
  143. * @param boolean $trim_user When set to true, each tweet returned in a timeline will include a user object including only
  144. * the status author's numerical ID.
  145. *
  146. * @return array The decoded JSON response
  147. *
  148. * @since 1.0
  149. */
  150. public function tweet($status, $in_reply_to_status_id = null, $lat = null, $long = null, $place_id = null, $display_coordinates = null,
  151. $trim_user = null)
  152. {
  153. // Set the API base.
  154. $path = '/statuses/update.json';
  155. // Set POST data.
  156. $data = array('status' => utf8_encode($status));
  157. // Check if in_reply_to_status_id is specified.
  158. if ($in_reply_to_status_id)
  159. {
  160. $data['in_reply_to_status_id'] = $in_reply_to_status_id;
  161. }
  162. // Check if lat is specified.
  163. if ($lat)
  164. {
  165. $data['lat'] = $lat;
  166. }
  167. // Check if long is specified.
  168. if ($long)
  169. {
  170. $data['long'] = $long;
  171. }
  172. // Check if place_id is specified.
  173. if ($place_id)
  174. {
  175. $data['place_id'] = $place_id;
  176. }
  177. // Check if display_coordinates is specified.
  178. if (!is_null($display_coordinates))
  179. {
  180. $data['display_coordinates'] = $display_coordinates;
  181. }
  182. // Check if trim_user is specified.
  183. if (!is_null($trim_user))
  184. {
  185. $data['trim_user'] = $trim_user;
  186. }
  187. // Send the request.
  188. return $this->sendRequest($path, 'POST', $data);
  189. }
  190. /**
  191. * Method to retrieve the most recent mentions for the authenticating user.
  192. *
  193. * @param integer $count Specifies the number of tweets to try and retrieve, up to a maximum of 200. Retweets are always included
  194. * in the count, so it is always suggested to set $include_rts to true
  195. * @param boolean $include_rts When set to true, the timeline will contain native retweets in addition to the standard stream of tweets.
  196. * @param boolean $entities When set to true, each tweet will include a node called "entities,". This node offers a variety of metadata
  197. * about the tweet in a discreet structure, including: user_mentions, urls, and hashtags.
  198. * @param integer $since_id Returns results with an ID greater than (that is, more recent than) the specified ID.
  199. * @param integer $max_id Returns results with an ID less than (that is, older than) the specified ID.
  200. * @param boolean $trim_user When set to true, each tweet returned in a timeline will include a user object including only
  201. * the status author's numerical ID.
  202. * @param string $contributor This parameter enhances the contributors element of the status response to include the screen_name
  203. * of the contributor.
  204. *
  205. * @return array The decoded JSON response
  206. *
  207. * @since 1.0
  208. * @throws \RuntimeException
  209. */
  210. public function getMentions($count = 20, $include_rts = null, $entities = null, $since_id = 0, $max_id = 0,
  211. $trim_user = null, $contributor = null)
  212. {
  213. // Check the rate limit for remaining hits
  214. $this->checkRateLimit('statuses', 'mentions_timeline');
  215. // Set the API base
  216. $path = '/statuses/mentions_timeline.json';
  217. // Set the count string
  218. $data['count'] = $count;
  219. // Check if include_rts is specified
  220. if (!is_null($include_rts))
  221. {
  222. $data['include_rts'] = $include_rts;
  223. }
  224. // Check if entities is specified
  225. if (!is_null($entities))
  226. {
  227. $data['include_entities'] = $entities;
  228. }
  229. // Check if a since_id is specified
  230. if ($since_id > 0)
  231. {
  232. $data['since_id'] = (int) $since_id;
  233. }
  234. // Check if a max_id is specified
  235. if ($max_id > 0)
  236. {
  237. $data['max_id'] = (int) $max_id;
  238. }
  239. // Check if trim_user is specified
  240. if (!is_null($trim_user))
  241. {
  242. $data['trim_user'] = $trim_user;
  243. }
  244. // Check if contributor is specified
  245. if (!is_null($contributor))
  246. {
  247. $data['contributor_details'] = $contributor;
  248. }
  249. // Send the request.
  250. return $this->sendRequest($path, 'GET', $data);
  251. }
  252. /**
  253. * Method to get the most recent tweets of the authenticated user that have been retweeted by others.
  254. *
  255. * @param integer $count Specifies the number of tweets to try and retrieve, up to a maximum of 200. Retweets are always included
  256. * in the count, so it is always suggested to set $include_rts to true
  257. * @param integer $since_id Returns results with an ID greater than (that is, more recent than) the specified ID.
  258. * @param boolean $entities When set to true, each tweet will include a node called "entities,". This node offers a variety of metadata
  259. * about the tweet in a discreet structure, including: user_mentions, urls, and hashtags.
  260. * @param boolean $user_entities The user entities node will be disincluded when set to false.
  261. * @param integer $max_id Returns results with an ID less than (that is, older than) the specified ID.
  262. * @param boolean $trim_user When set to true, each tweet returned in a timeline will include a user object including only
  263. * the status author's numerical ID.
  264. *
  265. * @return array The decoded JSON response
  266. *
  267. * @since 1.0
  268. */
  269. public function getRetweetsOfMe($count = 20, $since_id = 0, $entities = null, $user_entities = null, $max_id = 0, $trim_user = null)
  270. {
  271. // Check the rate limit for remaining hits
  272. $this->checkRateLimit('statuses', 'retweets_of_me');
  273. // Set the API path
  274. $path = '/statuses/retweets_of_me.json';
  275. // Set the count string
  276. $data['count'] = $count;
  277. // Check if a since_id is specified
  278. if ($since_id > 0)
  279. {
  280. $data['since_id'] = (int) $since_id;
  281. }
  282. // Check if a max_id is specified
  283. if ($max_id > 0)
  284. {
  285. $data['max_id'] = (int) $max_id;
  286. }
  287. // Check if trim_user is specified
  288. if (!is_null($trim_user))
  289. {
  290. $data['trim_user'] = $trim_user;
  291. }
  292. // Check if entities is specified
  293. if (!is_null($entities))
  294. {
  295. $data['include_entities'] = $entities;
  296. }
  297. // Check if entities is specified
  298. if (!is_null($user_entities))
  299. {
  300. $data['include_user_entities'] = $user_entities;
  301. }
  302. // Send the request.
  303. return $this->sendRequest($path, 'GET', $data);
  304. }
  305. /**
  306. * Method to show user objects of up to 100 members who retweeted the status.
  307. *
  308. * @param integer $id The numerical ID of the desired status.
  309. * @param integer $count Specifies the number of retweets to try and retrieve, up to a maximum of 100.
  310. * @param integer $cursor Causes the list of IDs to be broken into pages of no more than 100 IDs at a time.
  311. * The number of IDs returned is not guaranteed to be 100 as suspended users are
  312. * filtered out after connections are queried. If no cursor is provided, a value of
  313. * -1 will be assumed, which is the first "page."
  314. * @param boolean $stringify_ids Set to true to return IDs as strings, false to return as integers.
  315. *
  316. * @return array The decoded JSON response
  317. *
  318. * @since 1.0
  319. */
  320. public function getRetweeters($id, $count = 20, $cursor = null, $stringify_ids = null)
  321. {
  322. // Check the rate limit for remaining hits
  323. $this->checkRateLimit('statuses', 'retweeters/ids');
  324. // Set the API path
  325. $path = '/statuses/retweeters/ids.json';
  326. // Set the status id.
  327. $data['id'] = $id;
  328. // Set the count string
  329. $data['count'] = $count;
  330. // Check if cursor is specified
  331. if (!is_null($cursor))
  332. {
  333. $data['cursor'] = $cursor;
  334. }
  335. // Check if entities is specified
  336. if (!is_null($stringify_ids))
  337. {
  338. $data['stringify_ids'] = $stringify_ids;
  339. }
  340. // Send the request.
  341. return $this->sendRequest($path, 'GET', $data);
  342. }
  343. /**
  344. * Method to get up to 100 of the first retweets of a given tweet.
  345. *
  346. * @param integer $id The numerical ID of the desired status.
  347. * @param integer $count Specifies the number of tweets to try and retrieve, up to a maximum of 200. Retweets are always included
  348. * in the count, so it is always suggested to set $include_rts to true
  349. * @param boolean $trim_user When set to true, each tweet returned in a timeline will include a user object including only
  350. * the status author's numerical ID.
  351. *
  352. * @return array The decoded JSON response
  353. *
  354. * @since 1.0
  355. */
  356. public function getRetweetsById($id, $count = 20, $trim_user = null)
  357. {
  358. // Check the rate limit for remaining hits
  359. $this->checkRateLimit('statuses', 'retweets/:id');
  360. // Set the API path
  361. $path = '/statuses/retweets/' . $id . '.json';
  362. // Set the count string
  363. $data['count'] = $count;
  364. // Check if trim_user is specified
  365. if (!is_null($trim_user))
  366. {
  367. $data['trim_user'] = $trim_user;
  368. }
  369. // Send the request.
  370. return $this->sendRequest($path, 'GET', $data);
  371. }
  372. /**
  373. * Method to delete the status specified by the required ID parameter.
  374. *
  375. * @param integer $id The numerical ID of the desired status.
  376. * @param boolean $trim_user When set to true, each tweet returned in a timeline will include a user object including only
  377. * the status author's numerical ID.
  378. *
  379. * @return array The decoded JSON response
  380. *
  381. * @since 1.0
  382. */
  383. public function deleteTweet($id, $trim_user = null)
  384. {
  385. // Set the API path
  386. $path = '/statuses/destroy/' . $id . '.json';
  387. $data = array();
  388. // Check if trim_user is specified
  389. if (!is_null($trim_user))
  390. {
  391. $data['trim_user'] = $trim_user;
  392. }
  393. // Send the request.
  394. return $this->sendRequest($path, 'POST', $data);
  395. }
  396. /**
  397. * Method to retweet a tweet.
  398. *
  399. * @param integer $id The numerical ID of the desired status.
  400. * @param boolean $trim_user When set to true, each tweet returned in a timeline will include a user object including only
  401. * the status author's numerical ID.
  402. *
  403. * @return array The decoded JSON response
  404. *
  405. * @since 1.0
  406. */
  407. public function retweet($id, $trim_user = null)
  408. {
  409. // Set the API path
  410. $path = '/statuses/retweet/' . $id . '.json';
  411. $data = array();
  412. // Check if trim_user is specified
  413. if (!is_null($trim_user))
  414. {
  415. $data['trim_user'] = $trim_user;
  416. }
  417. // Send the request.
  418. return $this->sendRequest($path, 'POST', $data);
  419. }
  420. /**
  421. * Method to post a tweet with media.
  422. *
  423. * @param string $status The text of the tweet.
  424. * @param string $media File to upload
  425. * @param integer $in_reply_to_status_id The ID of an existing status that the update is in reply to.
  426. * @param float $lat The latitude of the location this tweet refers to.
  427. * @param float $long The longitude of the location this tweet refers to.
  428. * @param string $place_id A place in the world.
  429. * @param boolean $display_coordinates Whether or not to put a pin on the exact coordinates a tweet has been sent from.
  430. * @param boolean $sensitive Set to true for content which may not be suitable for every audience.
  431. *
  432. * @return array The decoded JSON response
  433. *
  434. * @since 1.0
  435. * @throws \RuntimeException
  436. */
  437. public function tweetWithMedia($status, $media, $in_reply_to_status_id = null, $lat = null, $long = null, $place_id = null,
  438. $display_coordinates = null, $sensitive = null)
  439. {
  440. // Set the API request path.
  441. $path = '/statuses/update_with_media.json';
  442. // Set POST data.
  443. $data = array(
  444. 'status' => utf8_encode($status),
  445. 'media[]' => "@{$media}"
  446. );
  447. $header = array('Content-Type' => 'multipart/form-data');
  448. // Check if in_reply_to_status_id is specified.
  449. if (!is_null($in_reply_to_status_id))
  450. {
  451. $data['in_reply_to_status_id'] = $in_reply_to_status_id;
  452. }
  453. // Check if lat is specified.
  454. if ($lat)
  455. {
  456. $data['lat'] = $lat;
  457. }
  458. // Check if long is specified.
  459. if ($long)
  460. {
  461. $data['long'] = $long;
  462. }
  463. // Check if place_id is specified.
  464. if ($place_id)
  465. {
  466. $data['place_id'] = $place_id;
  467. }
  468. // Check if display_coordinates is specified.
  469. if (!is_null($display_coordinates))
  470. {
  471. $data['display_coordinates'] = $display_coordinates;
  472. }
  473. // Check if sensitive is specified.
  474. if (!is_null($sensitive))
  475. {
  476. $data['possibly_sensitive'] = $sensitive;
  477. }
  478. // Send the request.
  479. return $this->sendRequest($path, 'POST', $data, $header);
  480. }
  481. /**
  482. * Method to get information allowing the creation of an embedded representation of a Tweet on third party sites.
  483. * Note: either the id or url parameters must be specified in a request. It is not necessary to include both.
  484. *
  485. * @param integer $id The Tweet/status ID to return embed code for.
  486. * @param string $url The URL of the Tweet/status to be embedded.
  487. * @param integer $maxwidth The maximum width in pixels that the embed should be rendered at. This value is constrained to be
  488. * between 250 and 550 pixels.
  489. * @param boolean $hide_media Specifies whether the embedded Tweet should automatically expand images which were uploaded via
  490. * POST statuses/update_with_media.
  491. * @param boolean $hide_thread Specifies whether the embedded Tweet should automatically show the original message in the case that
  492. * the embedded Tweet is a reply.
  493. * @param boolean $omit_script Specifies whether the embedded Tweet HTML should include a <script> element pointing to widgets.js. In cases where
  494. * a page already includes widgets.js, setting this value to true will prevent a redundant script element from being included.
  495. * @param string $align Specifies whether the embedded Tweet should be left aligned, right aligned, or centered in the page.
  496. * Valid values are left, right, center, and none.
  497. * @param string $related A value for the TWT related parameter, as described in Web Intents. This value will be forwarded to all
  498. * Web Intents calls.
  499. * @param string $lang Language code for the rendered embed. This will affect the text and localization of the rendered HTML.
  500. *
  501. * @return array The decoded JSON response
  502. *
  503. * @since 1.0
  504. * @throws \RuntimeException
  505. */
  506. public function getOembed($id = null, $url = null, $maxwidth = null, $hide_media = null, $hide_thread = null, $omit_script = null,
  507. $align = null, $related = null, $lang = null)
  508. {
  509. // Check the rate limit for remaining hits.
  510. $this->checkRateLimit('statuses', 'oembed');
  511. // Set the API request path.
  512. $path = '/statuses/oembed.json';
  513. // Determine which of $id and $url is specified.
  514. if ($id)
  515. {
  516. $data['id'] = $id;
  517. }
  518. elseif ($url)
  519. {
  520. $data['url'] = rawurlencode($url);
  521. }
  522. else
  523. {
  524. // We don't have a valid entry.
  525. throw new \RuntimeException('Either the id or url parameters must be specified in a request.');
  526. }
  527. // Check if maxwidth is specified.
  528. if ($maxwidth)
  529. {
  530. $data['maxwidth'] = $maxwidth;
  531. }
  532. // Check if hide_media is specified.
  533. if (!is_null($hide_media))
  534. {
  535. $data['hide_media'] = $hide_media;
  536. }
  537. // Check if hide_thread is specified.
  538. if (!is_null($hide_thread))
  539. {
  540. $data['hide_thread'] = $hide_thread;
  541. }
  542. // Check if omit_script is specified.
  543. if (!is_null($omit_script))
  544. {
  545. $data['omit_script'] = $omit_script;
  546. }
  547. // Check if align is specified.
  548. if ($align)
  549. {
  550. $data['align'] = $align;
  551. }
  552. // Check if related is specified.
  553. if ($related)
  554. {
  555. $data['related'] = $related;
  556. }
  557. // Check if lang is specified.
  558. if ($lang)
  559. {
  560. $data['lang'] = $lang;
  561. }
  562. // Send the request.
  563. return $this->sendRequest($path, 'GET', $data);
  564. }
  565. }