/inc/class.friends.php

http://php-twitter.googlecode.com/ · PHP · 170 lines · 75 code · 14 blank · 81 comment · 12 complexity · 853021af7ba1cbe5d16a3d26425594bb MD5 · raw file

  1. <?php
  2. /**
  3. * Twitter_Friends
  4. *
  5. * @package php-twitter 2.0
  6. * @subpackage friends
  7. * @author Aaron Brazell
  8. **/
  9. class Twitter_Friends extends Twitter {
  10. /**
  11. * Setup Twitter client connection details
  12. *
  13. * Ensure you set a user agent, whether via the constructor or by assigning a value to the property directly.
  14. * Also, if you are not running this from the Eastern timezone, be sure to set your proper timezone.
  15. *
  16. * @access public
  17. * @since 2.0
  18. * @return Twitter_Friends
  19. */
  20. public function __construct( $username = null, $password = null, $user_agent = null, $headers = null, $timezone = 'America/New_York')
  21. {
  22. parent::__construct($username, $password, $user_agent, $headers, $timezone);
  23. }
  24. /**
  25. * Request to follow a designated user
  26. *
  27. * Authenticating user follows another twitter user.
  28. *
  29. * @access public
  30. * @since 2.0
  31. * @param integer/string $user_id. Required the ID or screen name of the Twitter user to follow
  32. * @param boolean $notifications. Optional. Whether to recieve notifications from the followed user. Default is false.
  33. * @return object
  34. **/
  35. public function follow( $user_id, $notifications = false)
  36. {
  37. $data = array( 'notifications' => false );
  38. if( is_int( $user_id) )
  39. $data['user_id'] = $user_id;
  40. else
  41. $data['screen_name'] = (string) $user_id;
  42. if( $notifications )
  43. $data['notifications'] = true;
  44. $this->api_url = 'http://twitter.com/friendships/create.' . $this->type . $this->_glue( $data );
  45. return $this->_post( $this->api_url );
  46. }
  47. /**
  48. * Request to unfollow a designated user
  49. *
  50. * Authenticating user unfollows another twitter user.
  51. *
  52. * @access public
  53. * @since 2.0
  54. * @param integer/string $user_id. Required the ID or screen name of the Twitter user to follow
  55. * @return object
  56. **/
  57. public function unfollow( $user_id )
  58. {
  59. if( is_int( $user_id) )
  60. $data['user_id'] = $user_id;
  61. else
  62. $data['screen_name'] = (string) $user_id;
  63. $this->api_url = 'http://twitter.com/friendships/destroy.' . $this->type . $this->_glue( $data );
  64. return $this->_post( $this->api_url );
  65. }
  66. /**
  67. * Returns information about a relationship between two twitter users
  68. *
  69. * @access public
  70. * @since 2.0
  71. * @param string/integer $target_user. Required
  72. * @param string/integer/boolean $source_user. Optional. If omitted or false, the $source user will be the authenticating user
  73. * @return object
  74. **/
  75. public function show( $target_user, $source_user = false )
  76. {
  77. if( !$source_user )
  78. $source_user = $this->username;
  79. if( is_int( $source_user ) )
  80. $data['source_id'] = $source_user;
  81. else
  82. $data['source_sceen_name'] = (string) $source_user;
  83. if( is_int( $target_user) )
  84. $data['target_id'] = $target_user;
  85. else
  86. $data['target_screen_name'] = (string) $target_user;
  87. $this->api_url = 'http://twitter.com/friendships/show.' . $this->type . $this->_glue( $data );
  88. return $this->_get( $data );
  89. }
  90. /**
  91. * Return true if User A is following User B. Wrapper around show() method
  92. *
  93. * @access public
  94. * @since 2.0
  95. * @param string/integer $user_a. Required.
  96. * @param string/integer $user_b. Required.
  97. * @return boolean
  98. **/
  99. public function is_friend( $user_a, $user_b )
  100. {
  101. $relationship = $this->show( $user_a, $user_b );
  102. return $relationship->source->following;
  103. }
  104. /**
  105. * Returns a list of all Twitter user IDs that the specified user follows
  106. *
  107. * @access public
  108. * @since 2.0
  109. * @param string/integer $twitter_user
  110. * @param boolean/integer $page
  111. * @return object
  112. */
  113. public function user_follows( $twitter_user, $page = false )
  114. {
  115. $data = array();
  116. if( is_int( $twitter_user ) )
  117. $data['user_id'] = $twitter_user;
  118. else
  119. $data['screen_name'] = $twitter_user;
  120. if( $page && is_int($page) )
  121. $data['cursor'] = $page;
  122. $this->api_url = 'http://twitter.com/friends/ids.' . $this->type;
  123. $this->api_url .= $this->_glue( $data );
  124. return $this->_get( $this->api_url );
  125. }
  126. /**
  127. * Returns a list of all Twitter user IDs that follows the specified user
  128. *
  129. * @access public
  130. * @since 2.0
  131. * @param string/integer $twitter_user
  132. * @param boolean/integer $page
  133. * @return object
  134. */
  135. public function user_followers( $twitter_user, $page = false )
  136. {
  137. $data = array();
  138. if( is_int( $twitter_user ) )
  139. $data['user_id'] = $twitter_user;
  140. else
  141. $data['screen_name'] = $twitter_user;
  142. if( $page && is_int($page) )
  143. $data['cursor'] = $page;
  144. $this->api_url = 'http://twitter.com/followers/ids.' . $this->type;
  145. $this->api_url .= $this->_glue( $data );
  146. return $this->_get( $this->api_url );
  147. }
  148. /**
  149. * Destroys the object
  150. *
  151. * @access public
  152. * @since 2.0
  153. * @return null
  154. */
  155. public function __destruct() {}
  156. }