Posted By

jonniespratley on 08/21/09


Tagged

php jquery twitter rest


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

jonniespratley
legacye
umang_nine
voove


Twitter API Proxy Class


 / Published in: PHP
 

http://code.google.com/p/jquery-jtwitter/ for examples.

  1. <?php
  2. /**
  3.  * @author Jonnie Spratley
  4.  * @copywrite 2009 Jonnie Spratley
  5.  * @url http://jonniespratley.com
  6.  * @classDescription - PHP Proxy wrapper for the Twitter API. For use with jquery.jTwitter.js
  7.  * @projectDescription - http://code.google.com/p/jquery-jtwitter/
  8.  * @example - View the bottom of this class for all php examples.
  9.  */
  10. class TwitterService {
  11. /**
  12.   * @var - Twitter Username
  13.   */
  14. private $twitterUser = null;
  15. /**
  16.   * @var - Twitter Password
  17.   */
  18. private $twitterPass = null;
  19. /**
  20.   * @var - Twitter User Agent
  21.   */
  22. private $twitterUserAgent = null;
  23. /**
  24.   * @var - Twitter URL
  25.   */
  26. private $twitterUrl = 'http://twitter.com/';
  27. /**
  28.   * @var - Twitter Result Object
  29.   */
  30. private $twitterResult = null;
  31. /**
  32.   * @var - Twitter Fault Object
  33.   */
  34. private $twitterFault = null;
  35. /**
  36.   * @var - Twitter Result Headers
  37.   */
  38. private $twitterResultHeaders = null;
  39. /**
  40.   * @var - Twitter Result Format
  41.   */
  42. private $twitterResultFormat = 'json';
  43. /**
  44.   * @var - Bool - Request loggin on or off
  45.   */
  46. private $apiRequestLogging = false;
  47. /**
  48. * @var - String - Request log filename, must have full read/write perms
  49. */
  50. private $apiRequestFilename = 'logs/jtwitter_requests.log';
  51. /**
  52. * @var - Bool - Result loggin on or off
  53. */
  54. private $apiResultLogging = false;
  55. /**
  56. * @var - String - Result log filename, must have full read/write perms
  57. */
  58. private $apiResultFilename = 'logs/jtwitter_results.log';
  59.  
  60. /**
  61.   * TwitterService instance that allows fully ability to utilitize twitter.com api.
  62.   *
  63.   * @example
  64.   * <code>
  65.   *
  66.   * $service = new TwitterService();
  67.   * $service->set_twitterUser('USERNAME');
  68.   * $service->set_twitterPass('PASSWORD');
  69.   * $service->set_twitterResultFormat('json');
  70.   *
  71.   * </code>
  72.   * @return Instance of class
  73.   */
  74. public function __construct() {
  75. # error_reporting(0);
  76. }
  77.  
  78. /* ****************************************
  79.   * Getters/Setters
  80.   * **************************************** */
  81.  
  82. /**
  83.   * @param object $str
  84.   */
  85. public function set_twitterUser($str) {
  86. $this->twitterUser = $str;
  87. }
  88. /**
  89.   * @param object $str
  90.   */
  91. public function set_twitterPass($str) {
  92. $this->twitterPass = $str;
  93. }
  94. /**
  95.   * @param object $str
  96.   */
  97. public function set_twitterUserAgent($str) {
  98. $this->twitterAgent = $str;
  99. }
  100. /**
  101.   * @param object $str
  102.   */
  103. public function set_twitterResultFormat($str) {
  104. $this->twitterResultFormat = $str;
  105. }
  106. /**
  107.   *
  108.   * @return
  109.   * @param object $str
  110.   */
  111. public function set_twitterResultHeaders($str) {
  112. $this->twitterResultHeaders = $str;
  113. }
  114. /**
  115.   *
  116.   * @return
  117.   * @param object $str
  118.   */
  119. public function set_apiRequestLogging($bool) {
  120. $this->apiRequestLogging = $bool;
  121. }
  122. /**
  123.   *
  124.   * @return
  125.   * @param object $str
  126.   */
  127. public function set_apiResultLogging($bool) {
  128. $this->apiResultLogging = $bool;
  129. }
  130.  
  131. /**
  132.   *
  133.   * @return
  134.   * @param object $str
  135.   */
  136. public function set_apiResultFilename($str) {
  137. $this->apiResultFilename = $str;
  138. }
  139. /**
  140.   *
  141.   * @return
  142.   * @param object $str
  143.   */
  144. public function set_apiRequestFilename($str) {
  145. $this->apiRequestFilename = $str;
  146. }
  147.  
  148. /**
  149.   *
  150.   * @return
  151.   */
  152. public function get_apiResultLogging() {
  153. return $this->apiResultLogging;
  154. }
  155. /**
  156.   *
  157.   * @return
  158.   */
  159. public function get_apiRequestLogging() {
  160. return $this->apiRequestLogging;
  161. }
  162. /**
  163.   *
  164.   * @return
  165.   */
  166. public function get_apiRequestFilename() {
  167. return $this->apiRequestFilename;
  168. }
  169. /**
  170.   *
  171.   * @return
  172.   */
  173. public function get_apiResultFilename() {
  174. return $this->apiResultFilename;
  175. }
  176. /**
  177.   *
  178.   * @return
  179.   */
  180. public function get_twitterResultHeaders() {
  181. return $this->twitterResultHeaders;
  182. }
  183. /**
  184.   *
  185.   * @return
  186.   */
  187. public function get_twitterUser() {
  188. return $this->twitterUser;
  189. }
  190. /**
  191.   *
  192.   * @return
  193.   */
  194. public function get_twitterPass() {
  195. return $this->twitterPass;
  196. }
  197. /**
  198.   *
  199.   * @return
  200.   */
  201. public function get_twitterUserAgent() {
  202. return $this->twitterAgent;
  203. }
  204. /**
  205.   *
  206.   * @return
  207.   */
  208. public function get_twitterResultFormat() {
  209. return $this->twitterResultFormat;
  210. }
  211.  
  212. /* ****************************************
  213.   * Status Methods
  214.   * **************************************** */
  215. /**
  216.   * Removes a single status update from the Twitter timeline.
  217.   * @return
  218.   * @param object $id
  219.   */
  220. public function status_deleteTweet($id) {
  221. #statuses/destroy/id.json
  222. $data = array('id'=>$id);
  223. return $this->_POST('statuses/destroy/'.$id.'.'.$this->twitterResultFormat, http_build_query($data));
  224. }
  225. /**
  226.   * Returns a list of people who follow you.
  227.   * @return
  228.   * @param string $id[optional]
  229.   * @param int $page[optional]
  230.   */
  231. public function status_getFollowers($page = 1) {
  232. $data = array('page'=>$page);
  233.  
  234. $query = http_build_query($data, '', '&');
  235.  
  236. #statuses/followers.json/statuses/followers.xml
  237. return $this->_GET('statuses/followers.'.$this->twitterResultFormat, $query);
  238. }
  239. /**
  240.   * Returns a list of people you follow.
  241.   * @return
  242.   * @param object $id[optional]
  243.   * @param object $page[optional]
  244.   */
  245. public function status_getFriendsFollowers($id = null, $page = 1) {
  246. $data = array('id'=>$id, 'page'=>$page);
  247. $query = http_build_query($data, '', '&');
  248.  
  249. #statuses/friends.json
  250. return $this->_GET('statuses/friends/'.$id.'.'.$this->twitterResultFormat, $query);
  251. }
  252. /**
  253.   * Returns the most recent status updates made by people you follow.
  254.   * @return
  255.   * @param object $since[optional]
  256.   * @param object $sinceid[optional]
  257.   * @param object $count[optional]
  258.   * @param object $page[optional]
  259.   */
  260. public function status_getFriendsTimeline($count = 40, $page = 1) {
  261. $data = array('count'=>$count, 'page'=>$page);
  262. $query = http_build_query($data, '', '&');
  263. #statuses/friends_timeline.json
  264. return $this->_GET('statuses/friends_timeline.'.$this->twitterResultFormat, $query);
  265. }
  266. /**
  267.   * Returns the most recent status updates from public accounts with custom pictures.
  268.   * @return
  269.   */
  270. public function status_getPublicTimeline() {
  271. #statuses/public_timeline.json
  272. return $this->_GET('statuses/public_timeline.'.$this->twitterResultFormat);
  273. }
  274. /**
  275.   * Returns the most recent status updates for a specific account.
  276.   * @return
  277.   * @param object $id[optional]
  278.   * @param object $since[optional]
  279.   * @param object $since_id[optional]
  280.   * @param object $count[optional]
  281.   * @param object $page[optional]
  282.   */
  283. public function status_getUserTimeline($id = null, $count = 40, $page = 1) {
  284. $data = array('count'=>$count, 'page'=>$page);
  285. $query = http_build_query($data, '', '&');
  286. #statuses/user_timeline/id.json
  287. return $this->_GET('statuses/user_timeline/'.$id.'.'.$this->twitterResultFormat);
  288. }
  289. /**
  290.   * Returns the most recent status updates from people who have replied to you.
  291.   * @return
  292.   * @param object $since[optional]
  293.   * @param object $since_id[optional]
  294.   * @param object $count[optional]
  295.   * @param object $page[optional]
  296.   */
  297. public function status_getReplies($count = 10, $page = 1) {
  298. $data = array('count'=>$count, 'page'=>$page);
  299. $query = http_build_query($data, '', '&');
  300. #statuses/replies.json
  301. return $this->_GET('statuses/replies.'.$this->twitterResultFormat);
  302. }
  303. /**
  304.   * Returns a single status update with the given ID.
  305.   * @return
  306.   * @param object $id
  307.   */
  308. public function status_showTweet($id) {
  309. #statuses/show/id.json
  310. return $this->_GET('statuses/show/'.$id.'.'.$this->twitterResultFormat);
  311. }
  312. /**
  313.   * Creates a new status update authored by you.
  314.   * @return
  315.   * @param object $text
  316.   */
  317. public function status_postTweet($text) {
  318. $data = array('status'=>$text);
  319.  
  320. #statuses/update.json
  321. return $this->_POST('statuses/update.'.$this->twitterResultFormat, http_build_query($data));
  322. }
  323.  
  324. /* ****************************************
  325.   * User Methods
  326.   * **************************************** */
  327.  
  328. /**
  329.   * Returns your profile and statistical details.
  330.   * @return
  331.   * @param object $id
  332.   */
  333. public function user_showProfile($id) {
  334. #users/show/id.json
  335. return $this->_GET('users/show/'.$id.'.'.$this->twitterResultFormat);
  336. }
  337.  
  338. /* ****************************************
  339.   * Message Methods
  340.   * **************************************** */
  341.  
  342. /**
  343.   * Returns the most recent direct messages you have received.
  344.   * @return
  345.   * @param object $since[optional]
  346.   * @param object $since_id[optional]
  347.   * @param object $page[optional]
  348.   * @param object $count[optional]
  349.   */
  350. public function message_getMessages($count = 30, $page = 1) {
  351. $data = array('count'=>$count, 'page'=>$page);
  352. $query = http_build_query($data, '', '&');
  353.  
  354. #direct_messages.json
  355. return $this->_GET('direct_messages.'.$this->twitterResultFormat);
  356. }
  357.  
  358. /**
  359.   * Deletes an existing direct message received by you.
  360.   * @return
  361.   * @param object $id
  362.   */
  363. public function message_deleteMessage($id) {
  364. #direct_messages/destroy/id.json
  365. $data = array('id'=>$id);
  366.  
  367. return $this->_POST('direct_messages/destroy/'.$id.'.'.$this->twitterResultFormat, http_build_query($data));
  368. }
  369.  
  370. /**
  371.   * Creates a new direct message sent from you to another user.
  372.   * @return
  373.   * @param object $user
  374.   * @param object $text
  375.   */
  376. public function message_createMessage($user, $text) {
  377. #direct_messages/new.json
  378. $data = array('user'=>$user, 'text'=>$text);
  379.  
  380. return $this->_POST('direct_messages/new.'.$this->twitterResultFormat, http_build_query($data));
  381. }
  382.  
  383. /**
  384.   * Returns the most recent direct messages you have sent.
  385.   * @return
  386.   * @param object $since[optional]
  387.   * @param object $since_id[optional]
  388.   * @param object $page[optional]
  389.   * @param object $count[optional]
  390.   */
  391. public function message_getSentMessages($count = 30, $page = 1) {
  392. $data = array('count'=>$count, 'page'=>$page);
  393. $query = http_build_query($data, '', '&');
  394.  
  395. #direct_messages/sent.json
  396. return $this->_GET('direct_messages/sent.'.$this->twitterResultFormat);
  397. }
  398.  
  399. /* ****************************************
  400.   * Friendship Methods
  401.   * **************************************** */
  402.  
  403. /**
  404.   * Creates a new follow relationship between you and another Twitter member.
  405.   * @return
  406.   * @param object $id
  407.   */
  408. public function friendship_followMember($id) {
  409. #/friendships/create/id.json
  410. $data = array('id'=>$id, 'follow'=>$id);
  411.  
  412. return $this->_POST('friendships/create/'.$id.'.'.$this->twitterResultFormat, http_build_query($data));
  413. }
  414.  
  415. /**
  416.   * Removes an existing follow relationship with another Twitter member.
  417.   * @return
  418.   * @param object $id
  419.   */
  420. public function friendship_unfollowMember($id) {
  421. #/friendships/destroy/id.json
  422. $data = array('id'=>$id);
  423.  
  424. return $this->_POST('friendships/destroy/'.$id.'.'.$this->twitterResultFormat, http_build_query($data));
  425. }
  426.  
  427. /**
  428.   * Verifies whether one Twitter member is following another.
  429.   * @return
  430.   * @param object $userA
  431.   * @param object $userB
  432.   */
  433. public function friendship_confirmFollow($userA, $userB) {
  434. #/friendships/exists.json
  435. $data = array('user_a'=>$userA, 'user_b'=>$userB);
  436.  
  437. return $this->_GET('friendships/exists.'.$this->twitterResultFormat, http_build_query($data));
  438. }
  439.  
  440. /**
  441.   *
  442.   * @return
  443.   * @param object $id
  444.   */
  445. public function status_getFollowing($id) {
  446. #friends/ids.json
  447. return $this->_GET('friends/ids.'.$this->twitterResultFormat);
  448. }
  449.  
  450. /* ****************************************
  451.   * Favorite Methods
  452.   * **************************************** */
  453.  
  454. /**
  455.   * Returns a list of all status updates you’ve flagged as favorites.
  456.   * @return
  457.   * @param object $id[optional]
  458.   * @param object $page[optional]
  459.   */
  460. public function favorite_getFavorites($id = null, $page = 1) {
  461. $data = array('page'=>$page);
  462. $query = http_build_query($data, '', '&');
  463.  
  464. #favorites.json
  465. return $this->_GET('favorites.'.$this->twitterResultFormat);
  466. }
  467.  
  468. /**
  469.   * Flags a status update as a favorite.
  470.   * @return
  471.   * @param object $id
  472.   */
  473. public function favorite_createFavorite($id) {
  474. #favorites/create/id.json
  475. $data = array('id'=>$id);
  476.  
  477. return $this->_POST('favorites/create/'.$id.'.'.$this->twitterResultFormat, http_build_query($data));
  478. }
  479.  
  480. /**
  481.   * Removes an existing flag on one of the authenticating member’s favorite status updates.
  482.   * @return
  483.   * @param object $id
  484.   */
  485. public function favorite_deleteFavorite($id) {
  486. #favorites/destroy/id.json
  487. $data = array('id'=>$id);
  488.  
  489. return $this->_POST('favorites/destroy/'.$id.'.'.$this->twitterResultFormat, http_build_query($data));
  490. }
  491.  
  492.  
  493. /* ****************************************
  494.   * Block Methods
  495.   * **************************************** */
  496.  
  497. /**
  498.   * Keeps another member from following your updates.
  499.   * @return
  500.   * @param object $id
  501.   */
  502. public function block_blockMember($id) {
  503. #blocks/create/id.json
  504. $data = array('id'=>$id);
  505.  
  506. return $this->_POST('blocks/create/'.$id.'.'.$this->twitterResultFormat, http_build_query($data));
  507. }
  508.  
  509. /**
  510.   * Allows another member to once again follow your updates.
  511.   * @return
  512.   * @param object $id
  513.   */
  514. public function block_unBlockMember($id) {
  515. #blocks/destroy/id.json
  516. $data = array('id'=>$id);
  517.  
  518. return $this->_POST('blocks/destroy/'.$id.'.'.$this->twitterResultFormat, http_build_query($data));
  519. }
  520.  
  521.  
  522. /* ****************************************
  523.   * Social Methods
  524.   * **************************************** */
  525.  
  526. /**
  527.   * Gets the full list of people you follow.
  528.   * @return - array
  529.   */
  530. public function social_getAllFriends() {
  531. #/friends/ids.xml
  532. return $this->_GET('friends/ids.'.$this->twitterResultFormat);
  533. }
  534.  
  535. /**
  536.   * Gets the full list of people who follow you
  537.   * @return - array
  538.   */
  539. public function social_getAllFollowers() {
  540. #/followers/ids.xml
  541. return $this->_GET('followers/ids.'.$this->twitterResultFormat);
  542. }
  543.  
  544.  
  545. /* ****************************************
  546.   * Account Methods
  547.   * **************************************** */
  548.  
  549. /**
  550.   * Checks to see how many hourly hits on the API are left for your account
  551.   * @return
  552.   */
  553. public function account_rateLimit() {
  554. #account/rate_limit_status.format
  555. return $this->_GET('account/rate_limit_status.'.$this->twitterResultFormat);
  556. }
  557.  
  558. /**
  559.   * Tells Twitter that your application is finished using your access credentials
  560.   * @return
  561.   */
  562. public function account_endSession() {
  563. #http://twitter.com/account/end_session.format
  564. return $this->_GET('account/end_session.'.$this->twitterResultFormat);
  565. }
  566.  
  567. /**
  568.   * Confirms that the supplied user account credentials are valid.
  569.   *
  570.   * @example
  571.   * <code>
  572.   * $service->account_verifyCredentials();
  573.   * </code>
  574.   * @return
  575.   */
  576. public function account_verifyCredentials() {
  577. #/account/verify_credentials.format
  578. return $this->_GET('account/verify_credentials.'.$this->twitterResultFormat);
  579. }
  580.  
  581. /**
  582.   * Selects a device for you to use to receive updates.
  583.   * @return
  584.   * @param object $device
  585.   */
  586. public function account_updateDevice($device) {
  587. #/account/update_delivery_device.xml
  588. $data = array('device'=>$device);
  589.  
  590. return $this->_POST('account/update_delivery_device.'.$this->twitterResultFormat, http_build_query($data));
  591. }
  592.  
  593. /**
  594.   * Changes the location information stored in your profile.
  595.   * @return
  596.   * @param object $location
  597.   */
  598. public function account_updateLocation($location) {
  599. #/account/update_location.xml
  600. $data = array('location'=>$location);
  601.  
  602. return $this->_POST('account/update_location.'.$this->twitterResultFormat, http_build_query($data));
  603. }
  604.  
  605. /**
  606.   * Sets the values of selected fields found in the “Account” tab under the settings on the Twitter website.
  607.   * @return
  608.   * @param object $name[optional]
  609.   * @param object $email[optional]
  610.   * @param object $url[optional]
  611.   * @param object $location[optional]
  612.   * @param object $description[optional]
  613.   */
  614. public function account_updateProfile($name = null, $email = null, $url = null, $location = null, $description = null) {
  615. #/account/update_profile.xml
  616. $data = array('name'=>$name, 'email'=>$email, 'url'=>$url, 'location'=>$location, 'description'=>$description);
  617.  
  618. return $this->_POST('account/update_profile.'.$this->twitterResultFormat, http_build_query($data));
  619. }
  620.  
  621. /**
  622.   * Changes the background image on the authenticating user’s member profile web page.
  623.   * @return
  624.   * @param object $image
  625.   */
  626. public function account_updateBackgroundImage($image) {
  627. #/account/update_profile_background_image.xml
  628. $data = array('image'=>$image);
  629.  
  630. return $this->_POST('account/update_profile_background_image.'.$this->twitterResultFormat, $data);
  631. }
  632.  
  633.  
  634. /**
  635.   * Changes the color scheme applied to the authenticating member’s profile page.
  636.   * @return
  637.   * @param object $profile_background_color[optional] - The background color, visible only if no background image is used for the member profile.
  638.   * @param object $profile_text_color[optional] - The color of the primary text in the profile.
  639.   * @param object $profile_link_color[optional] - The color of the links used on the page.
  640.   * @param object $profile_sidebar_fill_color[optional] - The shading used in the righthand sidebar.
  641.   * @param object $profile_sidebar_border_color[optional] - The border colors used for lines in the sidebar.
  642.   */
  643. public function account_updateProfileColors($profile_background_color = null, $profile_text_color = null, $profile_link_color = null, $profile_sidebar_fill_color = null, $profile_sidebar_border_color = null) {
  644.  
  645. #/account/update_profile_colors.xml
  646. $data = array('profile_background_color'=>$profile_background_color, 'profile_text_color'=>$profile_text_color, 'profile_link_color'=>$profile_link_color, 'profile_sidebar_fill_color'=>$profile_sidebar_fill_color, 'profile_sidebar_border_color'=>$profile_sidebar_border_color);
  647.  
  648. return $this->_POST('account/update_profile_colors.'.$this->twitterResultFormat, http_build_query($data));
  649. }
  650.  
  651. /**
  652.   * Changes the picture associated with the authenticating member’s account and displayed with that user’s tweets
  653.   * @return
  654.   * @param object $image
  655.   */
  656. public function account_updateProfileImage($image) {
  657. #/account/update_profile_image.xml
  658. $data = array('image'=>$image);
  659.  
  660. return $this->_POST('account/update_profile_image.'.$this->twitterResultFormat, $data);
  661. }
  662.  
  663.  
  664. /* ****************************************
  665.   * Notification Methods
  666.   * **************************************** */
  667. /**
  668.   * Tells Twitter to start sending an author’s updates to the preferred device.
  669.   * @return
  670.   * @param object $id
  671.   */
  672. public function notification_turnOn($id) {
  673. #/notifications/follow/id.xml
  674. $data = array('id'=>$id);
  675.  
  676. return $this->_POST('notifications/follow/'.$id.'.'.$this->twitterResultFormat, http_build_query($data));
  677. }
  678. /**
  679.   * Tells Twitter to stop sending an author’s updates to the specified device.
  680.   * @return
  681.   * @param object $id
  682.   */
  683. public function notification_turnOff($id) {
  684. #/notifications/leave/id.xml
  685. $data = array('id'=>$id);
  686.  
  687. return $this->_POST('notifications/leave/'.$id.'.'.$this->twitterResultFormat, http_build_query($data));
  688. }
  689.  
  690.  
  691. /* ****************************************
  692.   * Search Methods
  693.   * **************************************** */
  694. /**
  695.   * Searches for keyword matches in tweet content.
  696.   * @return
  697.   * @param object $query
  698.   * @param object $since[optional]
  699.   * @param object $since_id[optional]
  700.   * @param object $page[optional]
  701.   * @param object $rpp[optional]
  702.   * @param object $geocode[optional]
  703.   * @param object $lang[optional]
  704.   * @param object $show_user[optional]
  705.   * @param object $callback[optional]
  706.   */
  707. public function search_keywords($query, $since = null, $since_id = null, $max_id = null, $page = null, $callback = null) {
  708. #search.twitter.com/search.atom?q=query
  709. $this->twitterUrl = 'search.twitter.com/search.'.$this->twitterResultFormat;
  710.  
  711. $data = array('q'=>$query, 'page' => $page, 'since_id'=>$since_id, 'max_id' => $max_id, 'since' => $since, 'callback'=>$callback);
  712.  
  713. return $this->_GET('?'.http_build_query($data));
  714. }
  715. /**
  716.   * Returns the current top keyword trends in the public timeline.
  717.   * @return
  718.   */
  719. public function search_trends() {
  720. #search.twitter.com/trends.json
  721. $this->twitterUrl = 'search.twitter.com/';
  722. return $this->_GET('trends.'.$this->twitterResultFormat);
  723. }
  724.  
  725.  
  726. /* ****************************************
  727.   * Help Methods
  728.   * **************************************** */
  729. /**
  730.   * Verifies whether your application’s connection to the Twitter API is working.
  731.   * @return
  732.   */
  733. public function help_test() {
  734. #help/test.json
  735. return $this->_GET('help/test.'.$this->twitterResultFormat);
  736. }
  737.  
  738.  
  739. /* ****************************************
  740.   * Call Helper Methods
  741.   * **************************************** */
  742. /**
  743.   *
  744.   * @return
  745.   * @param object $mode
  746.   * @param object $data[optional]
  747.   */
  748. private function _GET($mode, $data = null) {
  749.  
  750. $url = $this->twitterUrl.$mode;
  751. ($data != null) ? $url .= '?'.$data : '';
  752.  
  753. $tp_curlHandle = curl_init($url);
  754.  
  755. curl_setopt($tp_curlHandle, CURLOPT_FOLLOWLOCATION, false);
  756. curl_setopt($tp_curlHandle, CURLOPT_RETURNTRANSFER, true);
  757.  
  758. if ($mode != 'statuses/public_timeline.'.$this->get_twitterResultFormat() || $mode != 'account/verify_credentials.'.$this->get_twitterResultFormat()) {
  759. if ($this->get_twitterUser() != '' && $this->get_twitterPass() != '') {
  760. curl_setopt($tp_curlHandle, CURLOPT_USERPWD, $this->get_twitterUser().':'.$this->get_twitterPass());
  761. }
  762. }
  763.  
  764. if ($this->get_apiRequestLogging()) {
  765. $fh = fopen($file = $this->get_apiRequestFilename(), 'a');
  766.  
  767. curl_setopt($tp_curlHandle, CURLOPT_STDERR, $fh);
  768. curl_setopt($tp_curlHandle, CURLOPT_WRITEHEADER, $fh);
  769.  
  770. $this->_LOG($mode.'GET QUERY PARAMS', $data);
  771. }
  772.  
  773. $result = curl_exec($tp_curlHandle);
  774. $headerResult = curl_getinfo($tp_curlHandle);
  775. curl_close($tp_curlHandle);
  776.  
  777. if ($this->get_apiResultLogging()) {
  778. $this->_LOG($mode, $result);
  779. }
  780.  
  781. if ($headerResult['http_code'] === 200) {
  782. return $result;
  783. } else {
  784. return 'false';
  785. }
  786.  
  787. }
  788.  
  789. /**
  790.   * I post a request to the Twitter API
  791.   * @return
  792.   * @param object $mode - The mode in the rest api to post to.
  793.   * @param object $data - The assoc array of name/value data to the server
  794.   */
  795. private function _POST($mode, $data) {
  796. $tp_curlHandle = curl_init($this->twitterUrl.$mode);
  797.  
  798. //If request logging is on
  799. if ($this->get_apiRequestLogging()) {
  800. $fh = fopen($file = $this->get_apiRequestFilename(), 'a');
  801. curl_setopt($tp_curlHandle, CURLOPT_STDERR, $fh);
  802. curl_setopt($tp_curlHandle, CURLOPT_WRITEHEADER, $fh);
  803. $this->_LOG($mode, $data);
  804. }
  805.  
  806. curl_setopt($tp_curlHandle, CURLOPT_RETURNTRANSFER, true);
  807. curl_setopt($tp_curlHandle, CURLOPT_POST, true);
  808. curl_setopt($tp_curlHandle, CURLOPT_POSTFIELDS, $data);
  809.  
  810. //FileSize sending when updating image or bg 800kb
  811. /*To send a file append @FILENAME to the post fields
  812.   if ( $mode == 'account/update_profile_image.'.$this->twitterResultFormat || 'account/update_profile_background_image.'.$this->twitterResultFormat)
  813.   {
  814.   $file_to_upload = array( 'image'=>'@'.$data['image'] );
  815.   $fp = fopen($data['image'],'r'); // Open the file
  816.   curl_setopt($tp_curlHandle, CURLOPT_POSTFIELDS, $file_to_upload);
  817.   curl_setopt($tp_curlHandle, CURLOPT_UPLOAD,true);
  818.   curl_setopt($tp_curlHandle, CURLOPT_INFILE, $fp);
  819.   curl_setopt($tp_curlHandle, CURLOPT_INFILESIZE, filesize($data['image']));
  820.   }
  821.   */
  822.  
  823. if ($this->get_twitterUser() != '' && $this->get_twitterPass() != '') {
  824. curl_setopt($tp_curlHandle, CURLOPT_USERPWD, $this->get_twitterUser().':'.$this->get_twitterPass());
  825. } else {
  826. throw new Exception('You must provide a username and/or password.');
  827. exit();
  828. }
  829.  
  830. $result = curl_exec($tp_curlHandle);
  831. $headerResult = curl_getinfo($tp_curlHandle);
  832.  
  833. curl_close($tp_curlHandle);
  834.  
  835. //Check if logging is on
  836. if ($this->get_apiResultLogging()) {
  837. $this->_LOG($mode, $result);
  838. }
  839.  
  840. //If the result is successfull, return it
  841. if ($headerResult['http_code'] === 200) {
  842. return $result;
  843. } else {
  844. return 'false';
  845. }
  846. }
  847.  
  848.  
  849. /* ****************************************
  850.   * Utility Methods
  851.   * **************************************** */
  852. /**
  853.   *
  854.   * @return
  855.   * @param object $type
  856.   * @param object $var
  857.   */
  858. private function _LOG($type, $var) {
  859.  
  860. //write to log file
  861. $file = $this->get_apiResultFilename();
  862.  
  863. //append to end of content in log fil
  864. $fp = fopen($file, "a+");
  865.  
  866. //Date [Sat Jul 11 02:04:15 2009] [error] [client 127.0.0.1]
  867. $date = date('[D M j Y H:i:s] ', mktime());
  868.  
  869. $contents = "\n".$date.'['.$type.'] '.$var;
  870.  
  871. fwrite($fp, $contents);
  872.  
  873. fclose($fp);
  874. }
  875.  
  876. private function _tinyUrl($long)
  877. {
  878. // Using is.gd because it's good
  879. $request = 'http://is.gd/api.php?longurl='.$url;
  880. return $this->process($request);
  881. }
  882.  
  883. //TODO: Implement twitt pic api methods
  884.  
  885.  
  886. }//Ends Twitter Service
  887.  
  888.  
  889. header('Content-type: text/plain');
  890.  
  891. /* ****************************************
  892.  * REST Type Service Proxy
  893.  * **************************************** */
  894.  
  895. $tp_requestData = null;
  896.  
  897. //My Dogs account - Dont change anything, but you can update and follow as many people as you want.
  898. //$tp_user = 'rubybabylove';
  899. //$tp_pass = 'laurellie';
  900.  
  901. $tp_user = '';
  902. $tp_pass = '';
  903. $tp_mode = '';
  904.  
  905. $service = new TwitterService();
  906. $service->set_twitterUser($tp_user);
  907. $service->set_twitterPass($tp_pass);
  908. $service->set_twitterResultFormat('json');
  909. $service->set_apiRequestLogging(true);
  910. $service->set_apiResultLogging(true);
  911.  
  912. //ALL WORKING CALLS
  913. //echo $service->account_verifyCredentials();
  914.  
  915. //STATUS
  916. //echo $service->status_getFriendsFollowers($tp_user);
  917. //echo $service->status_getFollowing($tp_user);
  918. //echo $service->status_getReplies();
  919. //echo $service->status_getFollowers();
  920. //echo $service->status_getUserTimeline($tp_user);
  921. //echo $service->status_getPublicTimeline();
  922. //echo $service->status_getFriendsTimeline();
  923.  
  924. //$statustext = 'Momma take me potty, Poppa is outside.';
  925. //echo $service->status_postTweet($statustext);
  926.  
  927. //$statusId = '3120240498';
  928. //echo $service->status_showTweet($statusId);
  929. //$statusId = '3120240498';
  930. //echo $service->status_deleteTweet($statusId);
  931.  
  932. //SEARCH
  933. //echo $service->search_trends();
  934. //$query = 'Flex';
  935. //echo $service->search_keywords($query);
  936.  
  937. //MESSAGES
  938. //echo $service->message_getSentMessages();
  939. //echo $service->message_getMessages();
  940.  
  941. //ACCOUNT
  942. //echo $service->account_rateLimit();
  943. //$location = 'New York';
  944. //echo $service->account_updateLocation($location);
  945. //$device = 'sms';
  946. //echo $service->account_updateDevice($device);
  947.  
  948. //$profile_background_color = 'ffff00';
  949. //$profile_text_color = '212121';
  950. //$profile_link_color = 'ff0000';
  951. //$profile_sidebar_fill_color = '555555';
  952. //$profile_sidebar_border_color = 'b9b9b9';
  953. //echo $service->account_updateProfileColors($profile_background_color,$profile_text_color,$profile_link_color,$profile_link_color,$profile_sidebar_border_color);
  954.  
  955. //$name = 'Ruby Poopy';
  956. //$email = 'rubyspratley@gmail.com';
  957. //$url = 'http://ruby.com';
  958. //$location = 'Fair Oaks, Ca';
  959. //$description = 'I am the cutest and best little puppy in the whole wide world.';
  960. //echo $service->account_updateProfile($name, $email, $url, $location, $description);
  961.  
  962. //FRIENDSHIP
  963. //$followid = '34638894';
  964. //echo $service->friendship_followMember($followid);
  965. //echo $service->friendship_confirmFollow('rubybabylove', '34638894');
  966. //echo $service->friendship_unfollowMember($followid);
  967.  
  968. //BLOCK
  969. //$memId = '34638894';
  970. //echo $service->block_blockMember($memId);
  971. //echo $service->block_unBlockMember($memId);
  972.  
  973. //MESSAGE
  974. //$msgid = '278902230';
  975. //echo $service->message_deleteMessage($msgid);
  976. //$msguser = 'jonniespratley';
  977. //$msgtext = 'I love you poppa';
  978. //echo $service->message_createMessage($msguser, $msgtext);
  979.  
  980. //FAVORITE
  981. //$favid = '3120872696';
  982. //echo $service->favorite_createFavorite($favid);
  983. //echo $service->favorite_deleteFavorite($favid);
  984. //echo $service->favorite_getFavorites();
  985.  
  986. //SOCIAL
  987. //echo $service->social_getAllFollowers();
  988. //Result: [49924398,7774922,50263171,49895883,49155319,48947861,9115522,9102382]
  989.  
  990. //echo $service->social_getAllFriends();
  991. //Result: [50263171,49155319,49895883,48947861,9115522,9303572,7774922,9102382]
  992.  
  993.  
  994.  
  995.  
  996. //echo $service->account_endSession();
  997.  
  998.  
  999.  
  1000.  
  1001.  
  1002.  
  1003.  
  1004. /* NOT WORKING CALLS
  1005.  $image = '/Volumes/WWW/JonnieSpratleysProjects/TwitterPod/ruby.jpg';
  1006.  echo $service->account_updateBackgroundImage($image);
  1007.  $image = 'IMAGE_PATH';
  1008.  echo $service->account_updateProfileImage($image);
  1009.  */
  1010.  
  1011.  
  1012. switch ($_SERVER['REQUEST_METHOD']) {
  1013. case 'GET':
  1014. $tp_requestData = $_GET;
  1015. if (isset($_GET['u'])) {
  1016. $tp_user = $_GET['u'];
  1017. $service->set_twitterUser($tp_user);
  1018. unset($tp_requestData['u']);
  1019. }
  1020. if (isset($_GET['p'])) {
  1021. $tp_pass = $_GET['p'];
  1022. $service->set_twitterPass($tp_pass);
  1023. unset($tp_requestData['p']);
  1024. }
  1025. if (isset($_GET['m'])) {
  1026. $tp_mode = $_GET['m'];
  1027. unset($tp_requestData['m']);
  1028. }
  1029.  
  1030. switch ($tp_mode) {
  1031. case 'help_test':
  1032. echo $service->help_test();
  1033. break;
  1034. case 'getFollowers':
  1035. echo $service->status_getFollowers($tp_requestData['page']);
  1036. break;
  1037. case 'getFriendsFollowers':
  1038. echo $service->status_getFriendsFollowers($tp_user);
  1039. break;
  1040. case 'getFriendsTimeline':
  1041. echo $service->status_getFriendsTimeline($tp_requestData['count'], $tp_requestData['page']);
  1042. break;
  1043. case 'getPublicTimeline':
  1044. echo $service->status_getPublicTimeline();
  1045. break;
  1046. case 'getUserTimeline':
  1047. echo $service->status_getUserTimeline($tp_requestData['count'], $tp_requestData['page']);
  1048. break;
  1049. case 'getReplies':
  1050. echo $service->status_getReplies($tp_requestData['count'], $tp_requestData['page']);
  1051. break;
  1052. case 'showTweet':
  1053. echo $service->status_showTweet($tp_requestData['id']);
  1054. break;
  1055. case 'showProfile':
  1056. echo $service->user_showProfile($tp_user);
  1057. break;
  1058. case 'getMessages':
  1059. echo $service->message_getMessages($tp_requestData['count'], $tp_requestData['page']);
  1060. break;
  1061. case 'getSentMessages':
  1062. echo $service->message_getSentMessages($tp_requestData['count'], $tp_requestData['page']);
  1063. break;
  1064. case 'getFollowing':
  1065. echo $service->status_getFollowing($tp_user,$tp_requestData['count'], $tp_requestData['page']);
  1066. break;
  1067. case 'getFavorites':
  1068. echo $service->favorite_getFavorites($tp_requestData['count'], $tp_requestData['page']);
  1069. break;
  1070. case 'getAllFriends':
  1071. echo $service->social_getAllFriends();
  1072. break;
  1073. case 'getAllFollowers':
  1074. echo $service->social_getAllFollowers();
  1075. break;
  1076. case 'getRateLimit':
  1077. echo $service->account_rateLimit();
  1078. break;
  1079. case 'verifyCredentials':
  1080. echo $service->account_verifyCredentials();
  1081. break;
  1082. case 'searchKeywords':
  1083. //$query, $since = null, $since_id = null, $max_id = null, $page = null, $callback = null
  1084. echo $service->search_keywords(isset($tp_requestData['q']),isset($tp_requestData['since']),isset($tp_requestData['sinceid']),isset($tp_requestData['maxid']),isset($tp_requestData['page']) );
  1085. break;
  1086. case 'searchTrends':
  1087. echo $service->search_trends();
  1088. break;
  1089.  
  1090. /*
  1091.   default:
  1092.   throw new Exception('Error, You must specify a mode when calling via GET. Please use ?m=YOUR MODE.');
  1093.   exit ();
  1094.   break;
  1095.   */
  1096. }
  1097.  
  1098.  
  1099. break;//ends if GET
  1100. case 'POST':
  1101. $tp_requestData = $_POST;
  1102. if (isset($_POST['u'])) {
  1103. $tp_user = $_POST['u'];
  1104. $service->set_twitterUser($tp_user);
  1105. unset($tp_requestData['u']);
  1106. }
  1107. if (isset($_POST['p'])) {
  1108. $tp_pass = $_POST['p'];
  1109. $service->set_twitterPass($tp_pass);
  1110. unset($tp_requestData['p']);
  1111. }
  1112. if (isset($_POST['m'])) {
  1113. $tp_mode = $_POST['m'];
  1114. unset($tp_requestData['m']);
  1115. }
  1116. switch ($tp_mode) {
  1117. case 'createFavorite':
  1118. echo $service->favorite_createFavorite($tp_requestData['id']);
  1119. break;
  1120. case 'deleteFavorite':
  1121. echo $service->favorite_deleteFavorite($tp_requestData['id']);
  1122. break;
  1123. case 'blockMember':
  1124. echo $service->block_blockMember($tp_requestData['id']);
  1125. break;
  1126. case 'unblockMember':
  1127. echo $service->block_unBlockMember($tp_requestData['id']);
  1128. break;
  1129. case 'followMember':
  1130. echo $service->friendship_followMember($tp_requestData['id']);
  1131. break;
  1132. case 'confirmFollow':
  1133. echo $service->friendship_confirmFollow($tp_requestData['a'], $tp_requestData['b']);
  1134. break;
  1135. case 'unfollowMember':
  1136. echo $service->friendship_unfollowMember($tp_requestData['id']);
  1137. break;
  1138. case 'createMessage':
  1139. echo $service->message_createMessage($tp_requestData['user'], $tp_requestData['text']);
  1140. break;
  1141. case 'postTweet':
  1142. echo $service->status_postTweet($tp_requestData['status']);
  1143. break;
  1144. case 'deleteTweet':
  1145. echo $service->status_deleteTweet($tp_requestData['id']);
  1146. break;
  1147. case 'endSession':
  1148. echo $service->account_endSession();
  1149. break;
  1150. case 'updateDevice':
  1151. echo $service->account_updateDevice($tp_requestData['device']);
  1152. break;
  1153. case 'updateLocation':
  1154. echo $service->account_updateLocation($tp_requestData['location']);
  1155. break;
  1156. case 'updateProfile':
  1157. echo $service->account_updateProfile($tp_requestData['n'], $tp_requestData['e'], $tp_requestData['url'], $tp_requestData['l'], $tp_requestData['d']);
  1158. break;
  1159. case 'updateBackgroundImage':
  1160. echo $service->account_updateBackgroundImage($tp_requestData['image']);
  1161. break;
  1162. case 'updateProfileColors':
  1163. echo $service->account_updateProfileColors($tp_requestData['bg'], $tp_requestData['t'], $tp_requestData['sbg'], $tp_requestData['sb']);
  1164. break;
  1165. case 'updateProfileImage':
  1166. echo $service->account_updateProfileImage($tp_requestData['image']);
  1167. break;
  1168. case 'notification_turnOn':
  1169. echo $service->notification_turnOn($tp_requestData['id']);
  1170. break;
  1171. case 'notification_turnOff':
  1172. echo $service->notification_turnOff($tp_requestData['id']);
  1173. break;
  1174.  
  1175.  
  1176. /*
  1177.   default:
  1178.   throw new Exception('Error, You must specify a mode when calling via POST. Please use ?m=YOUR MODE.');
  1179.   exit ();
  1180.   break;
  1181.   */
  1182. }
  1183. break;//ends if POST
  1184. }
  1185.  
  1186.  
  1187. ?>

Report this snippet  

You need to login to post a comment.