PageRenderTime 63ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/TwitterApiClient.class.php

http://github.com/n1k0/phptwitterbot
PHP | 815 lines | 376 code | 93 blank | 346 comment | 113 complexity | f8c0f58ef6de3f2c6ba22901ada9daff MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. require_once dirname(__FILE__).'/TwitterApiServer.class.php';
  3. require_once dirname(__FILE__).'/Tweet.class.php';
  4. require_once dirname(__FILE__).'/TweetCollection.class.php';
  5. require_once dirname(__FILE__).'/TwitterDirectMessage.class.php';
  6. require_once dirname(__FILE__).'/TwitterDirectMessageCollection.class.php';
  7. require_once dirname(__FILE__).'/TwitterUser.class.php';
  8. require_once dirname(__FILE__).'/TwitterUserCollection.class.php';
  9. /**
  10. * Twitter api client
  11. *
  12. * Credits:
  13. * - Some parts of the code are based on the work of Tijs Verkoyen on the PHPTwitter project (BSD licensed)
  14. *
  15. * @author Nicolas Perriault <nperriault@gmail.com>
  16. * @license MIT License
  17. */
  18. class TwitterApiClient
  19. {
  20. protected
  21. $debug = false,
  22. $server = null,
  23. $userAgent = 'PHPTwitterBot (http://code.google.com/p/phptwitterbot/)';
  24. /**
  25. * Default constructor
  26. *
  27. * @param TwitterApiServer\null $server
  28. * @param Boolean $debug
  29. */
  30. public function __construct(TwitterApiServer $server = null, $debug = false)
  31. {
  32. if (is_null($server))
  33. {
  34. // Default server configuration
  35. $server = new TwitterApiServer('http://twitter.com', array(
  36. 'userAgent' => $this->getUserAgent(),
  37. 'httpPort' => 80,
  38. 'timeOut' => 30,
  39. ));
  40. }
  41. $this->debug = $debug;
  42. $this->server = $server;
  43. }
  44. /**
  45. * Make the call
  46. *
  47. * @param string $url API url to call
  48. * @param array $parameters Parameters for the request
  49. * @param bool $authenticate Shall we use authentication?
  50. * @param bool $usePost Uses POST method instead of GET
  51. *
  52. * @return mixed
  53. *
  54. * @throws InvalidArgumentException if the type provided is not supported
  55. * @throws TwitterApiServerException if the request fails for any reason
  56. * @throws RuntimeException if the xml response is invalid
  57. */
  58. protected function doCall($url, $parameters = array(), $authenticate = false, $usePost = true, $type = 'entity')
  59. {
  60. $response = $this->server->request(sprintf('%s.xml', $url), $parameters, $usePost ? 'POST' : 'GET');
  61. switch ($type)
  62. {
  63. case 'entity':
  64. $dom = new DOMDocument();
  65. $dom->loadXML($response);
  66. return TwitterEntity::createFromXml($dom);
  67. break;
  68. case 'boolean':
  69. if (!$xml = @simplexml_load_string($response))
  70. {
  71. throw new RuntimeException('XML error');
  72. }
  73. return (bool) ((string) $xml === 'true');
  74. break;
  75. case 'hash':
  76. if (!$xml = @simplexml_load_string($response))
  77. {
  78. throw new RuntimeException('XML error');
  79. }
  80. return (array) $xml;
  81. break;
  82. case 'search_results':
  83. return TweetCollection::createFromJSON($response);
  84. break;
  85. default:
  86. throw new InvalidArgumentException(sprintf('Type "%s" is not supported', $type));
  87. break;
  88. }
  89. }
  90. /**
  91. * Search in public or friends timeline for tweets. We cannot use the doCall method
  92. * because twitter doesn't provide XML format for searches, and will therefore force us to use
  93. * JSON.
  94. *
  95. * Available options:
  96. *
  97. * - source: can be 'public' or 'friends'
  98. * - max: Number of items to retrieve
  99. * - page: Page number to query
  100. *
  101. * @param string $terms Search terms string
  102. * @param array $options Options
  103. *
  104. * @return TweetCollection
  105. *
  106. * @throws InvalidArgumentException if unsupported source name
  107. */
  108. public function search($terms, array $options = array())
  109. {
  110. $options = array_merge(array('source' => 'public', 'max' => 15, 'page' => 1), $options);
  111. if (!in_array($options['source'], array('public', 'friends')))
  112. {
  113. throw new InvalidArgumentException(sprintf('Source "%s" is not supported', $source));
  114. }
  115. if ('public' === $options['source'])
  116. {
  117. $parameters = array('q' => $terms, 'page' => $options['page'], 'rpp' => $options['max']);
  118. return $this->doCall('search', $parameters, false, false, 'search_results');
  119. }
  120. else
  121. {
  122. $results = array();
  123. foreach ($this->getFriendsTimeline(null, null, 200) as $tweet)
  124. {
  125. if (preg_match(sprintf('/%s/i', $terms), $tweet->text))
  126. {
  127. $results[] = $tweet;
  128. }
  129. }
  130. return new TweetCollection($results);
  131. }
  132. }
  133. /**
  134. * Returns the 20 most recent statuses from non-protected users who have set a custom user icon.
  135. *
  136. * Note that the public timeline is cached for 60 seconds so requesting it more often than that
  137. * is a waste of resources.
  138. *
  139. * @return TweetCollection
  140. */
  141. public function getPublicTimeline()
  142. {
  143. return $this->doCall('statuses/public_timeline');
  144. }
  145. /**
  146. * Returns the 20 most recent statuses posted by the authenticating user and that user's friends.
  147. * This is the equivalent of /home on the Web.
  148. *
  149. * @param int $since Narrows the returned results to just those statuses created after the specified UNIX-timestamp, up to 24 hours old.
  150. * @param int $sinceId Returns only statuses with an id greater than (that is, more recent than) the specified $sinceId.
  151. * @param int $count Specifies the number of statuses to retrieve. May not be greater than 200.
  152. * @param int $page
  153. *
  154. * @return TweetCollection
  155. */
  156. public function getFriendsTimeline($since = null, $sinceId = null, $count = null, $page = null)
  157. {
  158. // validate parameters
  159. if (!is_null($since) && (int) $since <= 0) throw new TwitterApiClientException('Invalid timestamp for since.');
  160. if (!is_null($sinceId) && (int) $sinceId <= 0) throw new TwitterApiClientException('Invalid value for sinceId.');
  161. if (!is_null($count) && (int) $count > 200) throw new TwitterApiClientException('Count can\'t be larger then 200.');
  162. // build url
  163. $parameters = array();
  164. if (!is_null($since)) $parameters['since'] = date('r', (int) $since);
  165. if (!is_null($sinceId)) $parameters['since_id'] = (int) $sinceId;
  166. if (!is_null($count)) $parameters['count'] = (int) $count;
  167. if (!is_null($page)) $parameters['page'] = (int) $page;
  168. return $this->doCall('statuses/friends_timeline', $parameters, true, false);
  169. }
  170. /**
  171. * Returns the 20 most recent statuses posted from the authenticating user.
  172. *
  173. * It's also possible to request another user's timeline via the id parameter below.
  174. * This is the equivalent of the Web /archive page for your own user, or the profile page for a third party.
  175. *
  176. * @param string $id Specifies the id or screen name of the user for whom to return the friends_timeline.
  177. * @param int $since Narrows the returned results to just those statuses created after the specified UNIX-timestamp, up to 24 hours old
  178. * @param int $sinceId Returns only statuses with an id greater than (that is, more recent than) the specified $sinceId.
  179. * @param int $count Specifies the number of statuses to retrieve. May not be greater than 200.
  180. * @param int $page Page number
  181. *
  182. * @return TweetCollection
  183. */
  184. public function getUserTimeline($id = null, $since = null, $sinceId = null, $count = null, $page = null)
  185. {
  186. // validate parameters
  187. if (!is_null($since) && (int) $since <= 0) throw new TwitterApiClientException('Invalid timestamp for since.');
  188. if (!is_null($sinceId) && (int) $sinceId <= 0) throw new TwitterApiClientException('Invalid value for sinceId.');
  189. if (!is_null($count) && (int) $count > 200) throw new TwitterApiClientException('Count can\'t be larger then 200.');
  190. // build parameters
  191. $parameters = array();
  192. if (!is_null($since)) $parameters['since'] = date('r', (int) $since);
  193. if (!is_null($sinceId)) $parameters['since_id'] = (int) $sinceId;
  194. if (!is_null($count)) $parameters['count'] = (int) $count;
  195. if (!is_null($page)) $parameters['page'] = (int) $page;
  196. // build url
  197. $url = 'statuses/user_timeline';
  198. if (!is_null($id))
  199. {
  200. $url = 'statuses/user_timeline/'.urlencode($id);
  201. }
  202. return $this->doCall($url, $parameters, true, false);
  203. }
  204. /**
  205. * Returns a single status, specified by the id parameter below.
  206. *
  207. * @param int $id The numerical id of the status you're trying to retrieve.
  208. *
  209. * @return Tweet
  210. */
  211. public function getStatus($id)
  212. {
  213. return $this->doCall('statuses/show/'.urlencode($id));
  214. }
  215. /**
  216. * Checks if a given status has already been published recently
  217. *
  218. * @param string $status Status text
  219. * @param int $max Number of existing statuses to check
  220. *
  221. * @return Booleanean
  222. */
  223. public function isDuplicateStatus($status, $max = 1)
  224. {
  225. foreach ($this->getUserTimeline() as $tweet)
  226. {
  227. if (trim(strtolower($tweet->text)) == trim(strtolower($status)))
  228. {
  229. return true;
  230. }
  231. }
  232. return false;
  233. }
  234. /**
  235. * Updates the authenticating user's status.
  236. * A status update with text identical to the authenticating user's current status will be ignored.
  237. *
  238. * @param string $status The text of your status update. Should not be more than 140 characters.
  239. * @param int $inReplyToId The id of an existing status that the status to be posted is in reply to.
  240. *
  241. * @return Tweet
  242. */
  243. public function updateStatus($status, $inReplyToId = null)
  244. {
  245. if (mb_strlen($status) > 140)
  246. {
  247. throw new TwitterApiClientException('Maximum 140 characters allowed for status.');
  248. }
  249. $parameters = array('status' => $status);
  250. if (!is_null($inReplyToId))
  251. {
  252. $parameters['in_reply_to_status_id'] = $inReplyToId;
  253. }
  254. return $this->doCall('statuses/update', $parameters, true);
  255. }
  256. /**
  257. * Returns the 20 most recent @replies (status updates prefixed with @username) for the authenticating user.
  258. *
  259. * @param int $since Narrows the returned results to just those replies created after the specified UNIX-timestamp, up to 24 hours old.
  260. * @param int $sinceId Returns only statuses with an id greater than (that is, more recent than) the specified $sinceId.
  261. * @param int $page
  262. *
  263. * @return TweetCollection
  264. */
  265. public function getReplies($since = null, $sinceId = null, $page = null)
  266. {
  267. if (!is_null($since) && (int) $since <= 0) throw new TwitterApiClientException('Invalid timestamp for since.');
  268. if (!is_null($sinceId) && (int) $sinceId <= 0) throw new TwitterApiClientException('Invalid value for sinceId.');
  269. $parameters = array();
  270. if (!is_null($since)) $parameters['since'] = date('r', (int) $since);
  271. if (!is_null($sinceId)) $parameters['since_id'] = (int) $sinceId;
  272. if (!is_null($page)) $parameters['page'] = (int) $page;
  273. return $this->doCall('statuses/replies', $parameters, true, false);
  274. }
  275. /**
  276. * Destroys the status specified by the required $id parameter.
  277. * The authenticating user must be the author of the specified status.
  278. *
  279. * @param int $id
  280. *
  281. * @return Tweet
  282. */
  283. public function deleteStatus($id)
  284. {
  285. return $this->doCall('statuses/destroy/'.urlencode($id), array('id' => $id), true);
  286. }
  287. /**
  288. * Returns up to 100 of the authenticating user's friends who have most recently updated.
  289. * It's also possible to request another user's recent friends list via the $id parameter.
  290. *
  291. * @param string $id The id or screen name of the user for whom to request a list of friends.
  292. * @param int $page
  293. *
  294. * @return TwitterUserCollection
  295. */
  296. public function getFriends($id = null, $page = null)
  297. {
  298. $parameters = array();
  299. if (!is_null($page))
  300. {
  301. $parameters['page'] = (int) $page;
  302. }
  303. $url = 'statuses/friends';
  304. if (!is_null($id))
  305. {
  306. $url = 'statuses/friends/'.urlencode($id);
  307. }
  308. return $this->doCall($url, $parameters, true, false);
  309. }
  310. /**
  311. * Returns the authenticating user's followers.
  312. *
  313. * @param string $id The id or screen name of the user for whom to request a list of followers.
  314. * @param int $page
  315. *
  316. * @return TwitterUserCollection
  317. */
  318. public function getFollowers($id = null, $page = null)
  319. {
  320. $parameters = array();
  321. if (!is_null($page))
  322. {
  323. $parameters['page'] = (int) $page;
  324. }
  325. $url = 'statuses/followers';
  326. if (!is_null($id))
  327. {
  328. $url = 'statuses/followers/'.urlencode($id);
  329. }
  330. return $this->doCall($url, $parameters, true, false);
  331. }
  332. /**
  333. * Returns extended information of a given user, specified by id or screen name.
  334. * This information includes design settings, so third party developers can theme their widgets according to a given user's preferences.
  335. * You must be properly authenticated to request the page of a protected user.
  336. *
  337. * @param string $id The id or screen name of a user.
  338. * @param string $email May be used in place of $id.
  339. *
  340. * @return TwitterUser
  341. */
  342. public function getUser($id)
  343. {
  344. return $this->doCall('users/show/'.urlencode($id), array('id' => $id), true, false);
  345. }
  346. /**
  347. * Returns a direct message. This method of the twitter API is not documented but exists though.
  348. *
  349. * @param int $id Direct message id
  350. *
  351. * @return TwitterDirectMessage
  352. */
  353. public function getDirectMessage($id)
  354. {
  355. return $this->doCall('direct_messages/show/'.urlencode($id), array('id' => $id), true, false);
  356. }
  357. /**
  358. * Returns a list of the 20 most recent direct messages sent to the authenticating user.
  359. *
  360. * @param int $since Narrows the resulting list of direct messages to just those sent after the specified UNIX-timestamp, up to 24 hours old.
  361. * @param int $sinceId Returns only direct messages with an id greater than (that is, more recent than) the specified $sinceId.
  362. * @param int $page
  363. *
  364. * @return TwitterDirectMessageCollection
  365. */
  366. public function getDirectMessages($since = null, $sinceId = null, $page = null)
  367. {
  368. if (!is_null($since) && (int) $since <= 0) throw new TwitterApiClientException('Invalid timestamp for since.');
  369. if (!is_null($sinceId) && (int) $sinceId <= 0) throw new TwitterApiClientException('Invalid value for sinceId.');
  370. $parameters = array();
  371. if (!is_null($since)) $parameters['since'] = date('r', (int) $since);
  372. if (!is_null($sinceId)) $parameters['since_id'] = (int) $sinceId;
  373. if (!is_null($page)) $parameters['page'] = (int) $page;
  374. return $this->doCall('direct_messages', $parameters, true, false);
  375. }
  376. /**
  377. * Returns a list of the 20 most recent direct messages sent by the authenticating user.
  378. *
  379. * @param int $since Narrows the resulting list of direct messages to just those sent after the specified UNIX-timestamp, up to 24 hours old.
  380. * @param int $sinceId Returns only sent direct messages with an id greater than (that is, more recent than) the specified $sinceId.
  381. * @param int $page
  382. *
  383. * @return TwitterDirectMessageCollection
  384. */
  385. public function getSentDirectMessages($since = null, $sinceId = null, $page = null)
  386. {
  387. if (!is_null($since) && (int) $since <= 0) throw new TwitterApiClientException('Invalid timestamp for since.');
  388. if (!is_null($sinceId) && (int) $sinceId <= 0) throw new TwitterApiClientException('Invalid value for sinceId.');
  389. $parameters = array();
  390. if (!is_null($since)) $parameters['since'] = date('r', (int) $since);
  391. if (!is_null($sinceId)) $parameters['since_id'] = (int) $sinceId;
  392. if (!is_null($page)) $parameters['page'] = (int) $page;
  393. return $this->doCall('direct_messages/sent', $parameters, true, false);
  394. }
  395. /**
  396. * Sends a new direct message to the specified user from the authenticating user.
  397. *
  398. * @param string $id The id or screen name of the recipient user.
  399. * @param string $text The text of your direct message. Keep it under 140 characters.
  400. *
  401. * @return TwitterDirectMessage
  402. */
  403. public function sendDirectMessage($id, $text)
  404. {
  405. if (mb_strlen($text) > 140)
  406. {
  407. throw new TwitterApiClientException('Maximum 140 characters allowed for status.');
  408. }
  409. return $this->doCall('direct_messages/new', array('user' => $id, 'text' => $text), true);
  410. }
  411. /**
  412. * Destroys the direct message.
  413. * The authenticating user must be the recipient of the specified direct message.
  414. *
  415. * @param string $id
  416. *
  417. * @return TwitterDirectMessage
  418. */
  419. public function deleteDirectMessage($id)
  420. {
  421. return $this->doCall('direct_messages/destroy/'.urlencode($id), array('id' => $id), true);
  422. }
  423. /**
  424. * Befriends the user specified in the id parameter as the authenticating user.
  425. *
  426. * @param string $id The id or screen name of the user to befriend.
  427. * @param bool $follow Enable notifications for the target user in addition to becoming friends.
  428. *
  429. * @return TwitterUser
  430. */
  431. public function createFriendship($id, $follow = true)
  432. {
  433. $parameters = array('id' => $id);
  434. if ($follow)
  435. {
  436. $parameters['follow'] = $follow;
  437. }
  438. return $this->doCall('friendships/create/'.urlencode($id), $parameters, true);
  439. }
  440. /**
  441. * Discontinues friendship with the user.
  442. *
  443. * @param string $id
  444. */
  445. public function deleteFriendship($id)
  446. {
  447. return $this->doCall('friendships/destroy/'.urlencode($id), array('id' => $id), true);
  448. }
  449. /**
  450. * Tests if a friendship exists between two users.
  451. *
  452. * @param string $id The id or screen_name of the first user to test friendship for.
  453. * @param string $friendId The id or screen_name of the second user to test friendship for.
  454. */
  455. public function existsFriendship($id, $friendId)
  456. {
  457. return $this->doCall('friendships/exists', array('user_a' => $id, 'user_b' => $friendId), true, false, 'boolean');
  458. }
  459. /**
  460. * Verifies your credentials
  461. * Use this method to test if supplied user credentials are valid.
  462. *
  463. * @return Boolean
  464. */
  465. public function verifyCredentials()
  466. {
  467. try
  468. {
  469. return $this->doCall('account/verify_credentials', array(), true) instanceof TwitterUser;
  470. }
  471. catch (Exception $e)
  472. {
  473. if ($e->getCode() == 401 || $e->getMessage() == 'Could not authenticate you.')
  474. {
  475. return false;
  476. }
  477. else
  478. {
  479. throw $e;
  480. }
  481. }
  482. }
  483. /**
  484. * Sets values that users are able to set under the "Account" tab of their settings page.
  485. * Only the parameters specified will be updated.
  486. *
  487. * @param string $name
  488. * @param string $email
  489. * @param string $url
  490. * @param string $location
  491. * @param string $description
  492. *
  493. * @return TwitterUser
  494. */
  495. public function updateProfile($name = null, $email = null, $url = null, $location = null, $description = null)
  496. {
  497. if ($name === null && $email === null && $url === null && $location === null && $description === null) throw new TwitterApiClientException('Specify at least one parameter.');
  498. if (!is_null($name) && mb_strlen($name) > 40) throw new TwitterApiClientException('Maximum 40 characters allowed for name.');
  499. if (!is_null($email) && mb_strlen($email) > 40) throw new TwitterApiClientException('Maximum 40 characters allowed for email.');
  500. if (!is_null($url) && mb_strlen($url) > 100) throw new TwitterApiClientException('Maximum 100 characters allowed for url.');
  501. if (!is_null($location) && mb_strlen($location) > 30) throw new TwitterApiClientException('Maximum 30 characters allowed for location.');
  502. if (!is_null($description) && mb_strlen($description) > 160) throw new TwitterApiClientException('Maximum 160 characters allowed for description.');
  503. $parameters = array();
  504. if (!is_null($name)) $parameters['name'] = (string) $name;
  505. if (!is_null($email)) $parameters['email'] = (string) $email;
  506. if (!is_null($url)) $parameters['url'] = (string) $url;
  507. if (!is_null($location)) $parameters['location'] = (string) $location;
  508. if (!is_null($description)) $parameters['description'] = (string) $description;
  509. return $this->doCall('account/update_profile', $parameters, true);
  510. }
  511. /**
  512. * Sets one or more hex values that control the color scheme of the authenticating user's profile page on twitter.com.
  513. * Only the parameters specified will be updated.
  514. *
  515. * @param string $backgroundColor
  516. * @param string $textColor
  517. * @param string $linkColor
  518. * @param string $sidebarBackgroundColor
  519. * @param string $sidebarBorderColor
  520. *
  521. * @return TwitterUser
  522. */
  523. public function updateProfileColors($backgroundColor = null, $textColor = null, $linkColor = null, $sidebarBackgroundColor = null, $sidebarBorderColor = null)
  524. {
  525. if ($backgroundColor === null && $textColor === null && $linkColor === null && $sidebarBackgroundColor === null && $sidebarBorderColor === null) throw new TwitterApiClientException('Specify at least one parameter.');
  526. if (!is_null($backgroundColor) && (mb_strlen($backgroundColor) < 3 || mb_strlen($backgroundColor) > 6)) throw new TwitterApiClientException('Invalid color for background color.');
  527. if (!is_null($textColor) && (mb_strlen($textColor) < 3 || mb_strlen($textColor) > 6)) throw new TwitterApiClientException('Invalid color for text color.');
  528. if (!is_null($linkColor) && (mb_strlen($linkColor) < 3 || mb_strlen($linkColor) > 6)) throw new TwitterApiClientException('Invalid color for link color.');
  529. if (!is_null($sidebarBackgroundColor) && (mb_strlen($sidebarBackgroundColor) < 3 || mb_strlen($sidebarBackgroundColor) > 6)) throw new TwitterApiClientException('Invalid color for sidebar background color.');
  530. if (!is_null($sidebarBorderColor) && (mb_strlen($sidebarBorderColor) < 3 || mb_strlen($sidebarBorderColor) > 6)) throw new TwitterApiClientException('Invalid color for sidebar border color.');
  531. $parameters = array();
  532. if (!is_null($backgroundColor)) $parameters['profile_background_color'] = (string) $backgroundColor;
  533. if (!is_null($textColor)) $parameters['profile_text_color'] = (string) $textColor;
  534. if (!is_null($linkColor)) $parameters['profile_link_color'] = (string) $linkColor;
  535. if (!is_null($sidebarBackgroundColor)) $parameters['profile_sidebar_fill_color'] = (string) $sidebarBackgroundColor;
  536. if (!is_null($sidebarBorderColor)) $parameters['profile_sidebar_border_color'] = (string) $sidebarBorderColor;
  537. return $this->doCall('account/update_profile_colors', $parameters, true);
  538. }
  539. /**
  540. * Returns the remaining number of API requests available to the requesting user before
  541. * the API limit is reached for the current hour.
  542. *
  543. * @return array
  544. */
  545. public function getRateLimitStatus()
  546. {
  547. return $this->doCall('account/rate_limit_status', array(), true, false, 'hash');
  548. }
  549. /**
  550. * Returns the 20 most recent favorite statuses for the authenticating user or user specified
  551. * by the $id parameter
  552. *
  553. * @param string $id The id or screen name of the user for whom to request a list of favorite statuses.
  554. * @param int $page
  555. *
  556. * @return TweetCollection
  557. */
  558. public function getFavorites($id = null, $page = null)
  559. {
  560. $parameters = array();
  561. if (!is_null($page))
  562. {
  563. $parameters['page'] = (int) $page;
  564. }
  565. $url = 'favorites';
  566. if (!is_null($id))
  567. {
  568. $url = 'favorites/'.urlencode($id);
  569. }
  570. return $this->doCall($url, $parameters, true, false);
  571. }
  572. /**
  573. * Favorites the status specified in the id parameter as the authenticating user.
  574. *
  575. * @param string $id
  576. *
  577. * @return Tweet
  578. */
  579. public function createFavorite($id)
  580. {
  581. return $this->doCall('favorites/create/'.urlencode($id), array('id' => $id), true);
  582. }
  583. /**
  584. * Un-favorites the status specified in the id parameter as the authenticating user.
  585. *
  586. * @param string $id
  587. *
  588. * @return Tweet
  589. */
  590. public function deleteFavorite($id)
  591. {
  592. return $this->doCall('favorites/destroy/'.urlencode($id), array('id' => $id), true);
  593. }
  594. /**
  595. * Enables notifications for updates from the specified user to the authenticating user.
  596. * This method requires the authenticated user to already be friends with the specified
  597. * user otherwise the error "there was a problem following the specified user" will be
  598. * returned.
  599. *
  600. * @param string $id
  601. *
  602. * @return TwitterUser
  603. */
  604. public function followUser($id)
  605. {
  606. return $this->doCall('notifications/follow/'.urlencode($id), array('id' => $id), true);
  607. }
  608. /**
  609. * Disables notifications for updates from the specified user to the authenticating user.
  610. * This method requires the authenticated user to already be friends with the specified
  611. * user otherwise the error "there was a problem following the specified user" will be
  612. * returned.
  613. *
  614. * @param string $id
  615. */
  616. public function unfollowUser($id)
  617. {
  618. return $this->doCall('notifications/leave/'.urlencode($id), array('id' => $id), true);
  619. }
  620. /**
  621. * Blocks the user specified in the id parameter as the authenticating user.
  622. *
  623. * @param string $id
  624. */
  625. public function blockUser($id)
  626. {
  627. return $this->doCall('blocks/create/'.urlencode($id), array('id' => $id), true);
  628. }
  629. /**
  630. * Un-blocks the user specified in the id parameter as the authenticating user.
  631. *
  632. * @param string $id
  633. */
  634. public function unblockUser($id)
  635. {
  636. return $this->doCall('blocks/destroy/'.urlencode($id), array('id' => $id), true);
  637. }
  638. /**
  639. * Test the connection to Twitter
  640. *
  641. * @return Boolean
  642. */
  643. public function test()
  644. {
  645. return '<ok>true</ok>' === $this->doCall('help/test');
  646. }
  647. /**
  648. * Returns the same text displayed on http://twitter.com/home when a maintenance window is scheduled.
  649. *
  650. * @return string
  651. */
  652. public function getDowntimeSchedule()
  653. {
  654. // make the call
  655. $response = $this->doCall('help/downtime_schedule');
  656. // convert into xml-object
  657. $xml = simplexml_load_string($response);
  658. // validate
  659. if ($xml == false) throw new TwitterApiClientException('invalid body');
  660. if (!isset($xml->error)) throw new TwitterApiClientException('invalid body');
  661. // return
  662. return (string) utf8_decode($xml->error);
  663. }
  664. /**
  665. * Get the useragent
  666. *
  667. * @return string
  668. */
  669. public function getUserAgent()
  670. {
  671. return $this->userAgent;
  672. }
  673. /**
  674. * Set the user-agent
  675. * It will be appended to ours
  676. *
  677. * @param string $userAgent
  678. */
  679. public function setUserAgent($userAgent)
  680. {
  681. $this->userAgent = $userAgent;
  682. }
  683. /**
  684. * Get the username
  685. *
  686. * @return string
  687. */
  688. public function getUsername()
  689. {
  690. return $this->server->getUsername();
  691. }
  692. /**
  693. * Set username
  694. *
  695. * @param string $username
  696. */
  697. public function setUsername($username)
  698. {
  699. $this->server->setUsername($username);
  700. }
  701. /**
  702. * Get the password
  703. *
  704. * @return string
  705. */
  706. public function getPassword()
  707. {
  708. return $this->server->getPassword();
  709. }
  710. /**
  711. * Set the password
  712. *
  713. * @param string $password
  714. */
  715. public function setPassword($password)
  716. {
  717. $this->server->setPassword($password);
  718. }
  719. }
  720. class TwitterApiClientException extends Exception
  721. {
  722. }