2019-07-17 20:08:50 +00:00
< ? php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE . txt .
* It is also available through the world - wide - web at this URL :
* http :// framework . zend . com / license / new - bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world - wide - web , please send an email
* to license @ zend . com so we can send you a copy immediately .
*
* @ category Zend
2019-07-17 20:16:19 +00:00
* @ package Zend_Service
* @ subpackage Twitter
2019-07-17 20:19:00 +00:00
* @ copyright Copyright ( c ) 2005 - 2009 Zend Technologies USA Inc . ( http :// www . zend . com )
2019-07-17 20:08:50 +00:00
* @ license http :// framework . zend . com / license / new - bsd New BSD License
2019-07-17 20:31:04 +00:00
* @ version $Id : Twitter . php 19096 2009 - 11 - 20 15 : 33 : 15 Z sidhighwind $
2019-07-17 20:08:50 +00:00
*/
/**
* @ see Zend_Rest_Client
*/
require_once 'Zend/Rest/Client.php' ;
/**
* @ see Zend_Rest_Client_Result
*/
require_once 'Zend/Rest/Client/Result.php' ;
/**
* @ category Zend
* @ package Zend_Service
* @ subpackage Twitter
2019-07-17 20:19:00 +00:00
* @ copyright Copyright ( c ) 2005 - 2009 Zend Technologies USA Inc . ( http :// www . zend . com )
2019-07-17 20:08:50 +00:00
* @ license http :// framework . zend . com / license / new - bsd New BSD License
*/
class Zend_Service_Twitter extends Zend_Rest_Client
{
2019-07-17 20:31:04 +00:00
/**
* 246 is the current limit for a status message , 140 characters are displayed
* initially , with the remainder linked from the web UI or client . The limit is
* applied to a html encoded UTF - 8 string ( i . e . entities are counted in the limit
* which may appear unusual but is a security measure ) .
*
* This should be reviewed in the future ...
*/
const STATUS_MAX_CHARACTERS = 246 ;
2019-07-17 20:08:50 +00:00
/**
* Whether or not authorization has been initialized for the current user .
* @ var bool
*/
protected $_authInitialized = false ;
/**
* @ var Zend_Http_CookieJar
*/
protected $_cookieJar ;
/**
* Date format for 'since' strings
* @ var string
*/
2019-07-17 20:16:19 +00:00
protected $_dateFormat = 'D, d M Y H:i:s T' ;
2019-07-17 20:08:50 +00:00
/**
* Username
* @ var string
*/
protected $_username ;
/**
* Password
* @ var string
*/
protected $_password ;
/**
* Current method type ( for method proxying )
* @ var string
*/
protected $_methodType ;
/**
* Types of API methods
* @ var array
*/
2019-07-17 20:31:04 +00:00
protected $_methodTypes = array ( 'status' , 'user' , 'directMessage' , 'friendship' , 'account' , 'favorite' , 'block' );
/**
* Local HTTP Client cloned from statically set client
* @ var Zend_Http_Client
*/
protected $_localHttpClient = null ;
2019-07-17 20:08:50 +00:00
/**
* Constructor
*
* @ param string $username
* @ param string $password
* @ return void
*/
2019-07-17 20:31:04 +00:00
public function __construct ( $username = null , $password = null )
2019-07-17 20:08:50 +00:00
{
2019-07-17 20:31:04 +00:00
$this -> setLocalHttpClient ( clone self :: getHttpClient ());
if ( is_array ( $username ) && is_null ( $password )) {
if ( isset ( $username [ 'username' ]) && isset ( $username [ 'password' ])) {
$this -> setUsername ( $username [ 'username' ]);
$this -> setPassword ( $username [ 'password' ]);
} elseif ( isset ( $username [ 0 ]) && isset ( $username [ 1 ])) {
$this -> setUsername ( $username [ 0 ]);
$this -> setPassword ( $username [ 1 ]);
}
} else if ( ! is_null ( $username )) {
$this -> setUsername ( $username );
$this -> setPassword ( $password );
}
2019-07-17 20:08:50 +00:00
$this -> setUri ( 'http://twitter.com' );
2019-07-17 20:31:04 +00:00
$this -> _localHttpClient -> setHeaders ( 'Accept-Charset' , 'ISO-8859-1,utf-8' );
}
/**
* Set local HTTP client as distinct from the static HTTP client
* as inherited from Zend_Rest_Client .
*
* @ param Zend_Http_Client $client
* @ return self
*/
public function setLocalHttpClient ( Zend_Http_Client $client )
{
$this -> _localHttpClient = $client ;
return $this ;
}
2019-07-17 20:08:50 +00:00
2019-07-17 20:31:04 +00:00
public function getLocalHttpClient ()
{
return $this -> _localHttpClient ;
2019-07-17 20:08:50 +00:00
}
/**
* Retrieve username
*
* @ return string
*/
public function getUsername ()
{
return $this -> _username ;
}
/**
* Set username
*
* @ param string $value
* @ return Zend_Service_Twitter
*/
public function setUsername ( $value )
{
$this -> _username = $value ;
$this -> _authInitialized = false ;
return $this ;
}
/**
* Retrieve password
*
* @ return string
*/
public function getPassword ()
{
return $this -> _password ;
}
/**
* Set password
*
* @ param string $value
* @ return Zend_Service_Twitter
*/
public function setPassword ( $value )
{
$this -> _password = $value ;
$this -> _authInitialized = false ;
return $this ;
}
/**
* Proxy service methods
*
* @ param string $type
* @ return Zend_Service_Twitter
* @ throws Zend_Service_Twitter_Exception if method is not in method types list
*/
public function __get ( $type )
{
if ( ! in_array ( $type , $this -> _methodTypes )) {
include_once 'Zend/Service/Twitter/Exception.php' ;
throw new Zend_Service_Twitter_Exception ( 'Invalid method type "' . $type . '"' );
}
$this -> _methodType = $type ;
return $this ;
}
/**
* Method overloading
*
* @ param string $method
* @ param array $params
* @ return mixed
* @ throws Zend_Service_Twitter_Exception if unable to find method
*/
public function __call ( $method , $params )
{
if ( empty ( $this -> _methodType )) {
include_once 'Zend/Service/Twitter/Exception.php' ;
throw new Zend_Service_Twitter_Exception ( 'Invalid method "' . $method . '"' );
}
$test = $this -> _methodType . ucfirst ( $method );
if ( ! method_exists ( $this , $test )) {
include_once 'Zend/Service/Twitter/Exception.php' ;
throw new Zend_Service_Twitter_Exception ( 'Invalid method "' . $test . '"' );
}
return call_user_func_array ( array ( $this , $test ), $params );
}
/**
* Initialize HTTP authentication
*
* @ return void
*/
protected function _init ()
{
2019-07-17 20:31:04 +00:00
$client = $this -> _localHttpClient ;
2019-07-17 20:08:50 +00:00
$client -> resetParameters ();
if ( null == $this -> _cookieJar ) {
$client -> setCookieJar ();
$this -> _cookieJar = $client -> getCookieJar ();
} else {
$client -> setCookieJar ( $this -> _cookieJar );
}
2019-07-17 20:31:04 +00:00
if ( ! $this -> _authInitialized && $this -> getUsername () !== null ) {
2019-07-17 20:08:50 +00:00
$client -> setAuth ( $this -> getUsername (), $this -> getPassword ());
$this -> _authInitialized = true ;
}
}
/**
* Set date header
*
* @ param int | string $value
2019-07-17 20:31:04 +00:00
* @ deprecated Not supported by Twitter since April 08 , 2009
2019-07-17 20:08:50 +00:00
* @ return void
*/
protected function _setDate ( $value )
{
if ( is_int ( $value )) {
$date = date ( $this -> _dateFormat , $value );
} else {
$date = date ( $this -> _dateFormat , strtotime ( $value ));
}
2019-07-17 20:31:04 +00:00
$this -> _localHttpClient -> setHeaders ( 'If-Modified-Since' , $date );
2019-07-17 20:08:50 +00:00
}
/**
* Public Timeline status
*
2019-07-17 20:31:04 +00:00
* @ throws Zend_Http_Client_Exception if HTTP request fails or times out
2019-07-17 20:08:50 +00:00
* @ return Zend_Rest_Client_Result
*/
public function statusPublicTimeline ()
{
$this -> _init ();
$path = '/statuses/public_timeline.xml' ;
2019-07-17 20:31:04 +00:00
$response = $this -> _get ( $path );
2019-07-17 20:08:50 +00:00
return new Zend_Rest_Client_Result ( $response -> getBody ());
}
/**
* Friend Timeline Status
*
* $params may include one or more of the following keys
* - id : ID of a friend whose timeline you wish to receive
2019-07-17 20:16:19 +00:00
* - count : how many statuses to return
* - since_id : return results only after the specific tweet
2019-07-17 20:08:50 +00:00
* - page : return page X of results
*
* @ param array $params
2019-07-17 20:31:04 +00:00
* @ throws Zend_Http_Client_Exception if HTTP request fails or times out
2019-07-17 20:08:50 +00:00
* @ return void
*/
public function statusFriendsTimeline ( array $params = array ())
{
$this -> _init ();
$path = '/statuses/friends_timeline' ;
2019-07-17 20:16:19 +00:00
$_params = array ();
2019-07-17 20:08:50 +00:00
foreach ( $params as $key => $value ) {
switch ( strtolower ( $key )) {
2019-07-17 20:16:19 +00:00
case 'count' :
$count = ( int ) $value ;
if ( 0 >= $count ) {
$count = 1 ;
} elseif ( 200 < $count ) {
$count = 200 ;
}
$_params [ 'count' ] = ( int ) $count ;
break ;
case 'since_id' :
2019-07-17 20:31:04 +00:00
$_params [ 'since_id' ] = $this -> _validInteger ( $value );
2019-07-17 20:08:50 +00:00
break ;
case 'page' :
2019-07-17 20:16:19 +00:00
$_params [ 'page' ] = ( int ) $value ;
2019-07-17 20:08:50 +00:00
break ;
default :
break ;
}
}
2019-07-17 20:31:04 +00:00
$path .= '.xml' ;
$response = $this -> _get ( $path , $_params );
2019-07-17 20:08:50 +00:00
return new Zend_Rest_Client_Result ( $response -> getBody ());
}
/**
* User Timeline status
*
* $params may include one or more of the following keys
* - id : ID of a friend whose timeline you wish to receive
2019-07-17 20:31:04 +00:00
* - since_id : return results only after the tweet id specified
2019-07-17 20:08:50 +00:00
* - page : return page X of results
* - count : how many statuses to return
2019-07-17 20:31:04 +00:00
* - max_id : returns only statuses with an ID less than or equal to the specified ID
* - user_id : specfies the ID of the user for whom to return the user_timeline
* - screen_name : specfies the screen name of the user for whom to return the user_timeline
2019-07-17 20:08:50 +00:00
*
2019-07-17 20:31:04 +00:00
* @ throws Zend_Http_Client_Exception if HTTP request fails or times out
2019-07-17 20:08:50 +00:00
* @ return Zend_Rest_Client_Result
*/
public function statusUserTimeline ( array $params = array ())
{
$this -> _init ();
$path = '/statuses/user_timeline' ;
$_params = array ();
foreach ( $params as $key => $value ) {
switch ( strtolower ( $key )) {
case 'id' :
$path .= '/' . $value ;
break ;
case 'page' :
$_params [ 'page' ] = ( int ) $value ;
break ;
case 'count' :
$count = ( int ) $value ;
if ( 0 >= $count ) {
$count = 1 ;
} elseif ( 200 < $count ) {
$count = 200 ;
}
$_params [ 'count' ] = $count ;
break ;
2019-07-17 20:31:04 +00:00
case 'user_id' :
$_params [ 'user_id' ] = $this -> _validInteger ( $value );
break ;
case 'screen_name' :
$_params [ 'screen_name' ] = $this -> _validateScreenName ( $value );
break ;
case 'since_id' :
$_params [ 'since_id' ] = $this -> _validInteger ( $value );
break ;
case 'max_id' :
$_params [ 'max_id' ] = $this -> _validInteger ( $value );
break ;
2019-07-17 20:08:50 +00:00
default :
break ;
}
}
2019-07-17 20:31:04 +00:00
$path .= '.xml' ;
$response = $this -> _get ( $path , $_params );
2019-07-17 20:08:50 +00:00
return new Zend_Rest_Client_Result ( $response -> getBody ());
}
/**
* Show a single status
*
* @ param int $id Id of status to show
2019-07-17 20:31:04 +00:00
* @ throws Zend_Http_Client_Exception if HTTP request fails or times out
2019-07-17 20:08:50 +00:00
* @ return Zend_Rest_Client_Result
*/
public function statusShow ( $id )
{
$this -> _init ();
2019-07-17 20:31:04 +00:00
$path = '/statuses/show/' . $this -> _validInteger ( $id ) . '.xml' ;
$response = $this -> _get ( $path );
2019-07-17 20:08:50 +00:00
return new Zend_Rest_Client_Result ( $response -> getBody ());
}
/**
* Update user ' s current status
*
* @ param string $status
* @ param int $in_reply_to_status_id
* @ return Zend_Rest_Client_Result
2019-07-17 20:31:04 +00:00
* @ throws Zend_Http_Client_Exception if HTTP request fails or times out
2019-07-17 20:08:50 +00:00
* @ throws Zend_Service_Twitter_Exception if message is too short or too long
*/
2019-07-17 20:31:04 +00:00
public function statusUpdate ( $status , $inReplyToStatusId = null )
2019-07-17 20:08:50 +00:00
{
$this -> _init ();
$path = '/statuses/update.xml' ;
2019-07-17 20:31:04 +00:00
$len = iconv_strlen ( htmlspecialchars ( $status , ENT_QUOTES , 'UTF-8' ), 'UTF-8' );
if ( $len > self :: STATUS_MAX_CHARACTERS ) {
2019-07-17 20:08:50 +00:00
include_once 'Zend/Service/Twitter/Exception.php' ;
2019-07-17 20:31:04 +00:00
throw new Zend_Service_Twitter_Exception ( 'Status must be no more than ' . self :: STATUS_MAX_CHARACTERS . ' characters in length' );
2019-07-17 20:08:50 +00:00
} elseif ( 0 == $len ) {
include_once 'Zend/Service/Twitter/Exception.php' ;
throw new Zend_Service_Twitter_Exception ( 'Status must contain at least one character' );
}
2019-07-17 20:31:04 +00:00
$data = array ( 'status' => $status );
if ( is_numeric ( $inReplyToStatusId ) && ! empty ( $inReplyToStatusId )) {
$data [ 'in_reply_to_status_id' ] = $inReplyToStatusId ;
2019-07-17 20:08:50 +00:00
}
//$this->status = $status;
2019-07-17 20:31:04 +00:00
$response = $this -> _post ( $path , $data );
2019-07-17 20:08:50 +00:00
return new Zend_Rest_Client_Result ( $response -> getBody ());
}
/**
* Get status replies
*
* $params may include one or more of the following keys
* - since_id : return results only after the specified tweet id
* - page : return page X of results
*
2019-07-17 20:31:04 +00:00
* @ throws Zend_Http_Client_Exception if HTTP request fails or times out
2019-07-17 20:08:50 +00:00
* @ return Zend_Rest_Client_Result
*/
public function statusReplies ( array $params = array ())
{
$this -> _init ();
$path = '/statuses/replies.xml' ;
$_params = array ();
foreach ( $params as $key => $value ) {
switch ( strtolower ( $key )) {
case 'since_id' :
2019-07-17 20:31:04 +00:00
$_params [ 'since_id' ] = $this -> _validInteger ( $value );
2019-07-17 20:08:50 +00:00
break ;
case 'page' :
$_params [ 'page' ] = ( int ) $value ;
break ;
default :
break ;
}
}
2019-07-17 20:31:04 +00:00
$response = $this -> _get ( $path , $_params );
2019-07-17 20:08:50 +00:00
return new Zend_Rest_Client_Result ( $response -> getBody ());
}
/**
* Destroy a status message
*
* @ param int $id ID of status to destroy
2019-07-17 20:31:04 +00:00
* @ throws Zend_Http_Client_Exception if HTTP request fails or times out
2019-07-17 20:08:50 +00:00
* @ return Zend_Rest_Client_Result
*/
public function statusDestroy ( $id )
{
$this -> _init ();
2019-07-17 20:31:04 +00:00
$path = '/statuses/destroy/' . $this -> _validInteger ( $id ) . '.xml' ;
$response = $this -> _post ( $path );
2019-07-17 20:08:50 +00:00
return new Zend_Rest_Client_Result ( $response -> getBody ());
}
/**
* User friends
*
* @ param int | string $id Id or username of user for whom to fetch friends
2019-07-17 20:31:04 +00:00
* @ throws Zend_Http_Client_Exception if HTTP request fails or times out
2019-07-17 20:08:50 +00:00
* @ return Zend_Rest_Client_Result
*/
public function userFriends ( array $params = array ())
{
$this -> _init ();
$path = '/statuses/friends' ;
$_params = array ();
2019-07-17 20:31:04 +00:00
2019-07-17 20:08:50 +00:00
foreach ( $params as $key => $value ) {
switch ( strtolower ( $key )) {
case 'id' :
$path .= '/' . $value ;
break ;
case 'page' :
$_params [ 'page' ] = ( int ) $value ;
break ;
default :
break ;
}
}
2019-07-17 20:31:04 +00:00
$path .= '.xml' ;
2019-07-17 20:08:50 +00:00
2019-07-17 20:31:04 +00:00
$response = $this -> _get ( $path , $_params );
2019-07-17 20:08:50 +00:00
return new Zend_Rest_Client_Result ( $response -> getBody ());
}
/**
* User Followers
*
* @ param bool $lite If true , prevents inline inclusion of current status for followers ; defaults to false
2019-07-17 20:31:04 +00:00
* @ throws Zend_Http_Client_Exception if HTTP request fails or times out
2019-07-17 20:08:50 +00:00
* @ return Zend_Rest_Client_Result
*/
public function userFollowers ( $lite = false )
{
$this -> _init ();
$path = '/statuses/followers.xml' ;
if ( $lite ) {
$this -> lite = 'true' ;
}
2019-07-17 20:31:04 +00:00
$response = $this -> _get ( $path );
2019-07-17 20:08:50 +00:00
return new Zend_Rest_Client_Result ( $response -> getBody ());
}
/**
* Get featured users
*
2019-07-17 20:31:04 +00:00
* @ throws Zend_Http_Client_Exception if HTTP request fails or times out
2019-07-17 20:08:50 +00:00
* @ return Zend_Rest_Client_Result
*/
public function userFeatured ()
{
$this -> _init ();
$path = '/statuses/featured.xml' ;
2019-07-17 20:31:04 +00:00
$response = $this -> _get ( $path );
2019-07-17 20:08:50 +00:00
return new Zend_Rest_Client_Result ( $response -> getBody ());
}
/**
* Show extended information on a user
*
* @ param int | string $id User ID or name
2019-07-17 20:31:04 +00:00
* @ throws Zend_Http_Client_Exception if HTTP request fails or times out
2019-07-17 20:08:50 +00:00
* @ return Zend_Rest_Client_Result
*/
public function userShow ( $id )
{
$this -> _init ();
$path = '/users/show/' . $id . '.xml' ;
2019-07-17 20:31:04 +00:00
$response = $this -> _get ( $path );
2019-07-17 20:08:50 +00:00
return new Zend_Rest_Client_Result ( $response -> getBody ());
}
/**
* Retrieve direct messages for the current user
*
* $params may include one or more of the following keys
* - since_id : return statuses only greater than the one specified
* - page : return page X of results
*
* @ param array $params
2019-07-17 20:31:04 +00:00
* @ throws Zend_Http_Client_Exception if HTTP request fails or times out
2019-07-17 20:08:50 +00:00
* @ return Zend_Rest_Client_Result
*/
public function directMessageMessages ( array $params = array ())
{
$this -> _init ();
$path = '/direct_messages.xml' ;
2019-07-17 20:16:19 +00:00
$_params = array ();
2019-07-17 20:08:50 +00:00
foreach ( $params as $key => $value ) {
switch ( strtolower ( $key )) {
case 'since_id' :
2019-07-17 20:31:04 +00:00
$_params [ 'since_id' ] = $this -> _validInteger ( $value );
2019-07-17 20:08:50 +00:00
break ;
case 'page' :
2019-07-17 20:16:19 +00:00
$_params [ 'page' ] = ( int ) $value ;
2019-07-17 20:08:50 +00:00
break ;
default :
break ;
}
}
2019-07-17 20:31:04 +00:00
$response = $this -> _get ( $path , $_params );
2019-07-17 20:08:50 +00:00
return new Zend_Rest_Client_Result ( $response -> getBody ());
}
/**
* Retrieve list of direct messages sent by current user
*
* $params may include one or more of the following keys
* - since_id : return statuses only greater than the one specified
* - page : return page X of results
*
* @ param array $params
2019-07-17 20:31:04 +00:00
* @ throws Zend_Http_Client_Exception if HTTP request fails or times out
2019-07-17 20:08:50 +00:00
* @ return Zend_Rest_Client_Result
*/
public function directMessageSent ( array $params = array ())
{
$this -> _init ();
$path = '/direct_messages/sent.xml' ;
2019-07-17 20:16:19 +00:00
$_params = array ();
2019-07-17 20:08:50 +00:00
foreach ( $params as $key => $value ) {
switch ( strtolower ( $key )) {
case 'since_id' :
2019-07-17 20:31:04 +00:00
$_params [ 'since_id' ] = $this -> _validInteger ( $value );
2019-07-17 20:08:50 +00:00
break ;
case 'page' :
2019-07-17 20:16:19 +00:00
$_params [ 'page' ] = ( int ) $value ;
2019-07-17 20:08:50 +00:00
break ;
default :
break ;
}
}
2019-07-17 20:31:04 +00:00
$response = $this -> _get ( $path , $_params );
2019-07-17 20:08:50 +00:00
return new Zend_Rest_Client_Result ( $response -> getBody ());
}
/**
* Send a direct message to a user
*
* @ param int | string $user User to whom to send message
* @ param string $text Message to send to user
* @ return Zend_Rest_Client_Result
* @ throws Zend_Service_Twitter_Exception if message is too short or too long
2019-07-17 20:31:04 +00:00
* @ throws Zend_Http_Client_Exception if HTTP request fails or times out
2019-07-17 20:08:50 +00:00
*/
public function directMessageNew ( $user , $text )
{
$this -> _init ();
$path = '/direct_messages/new.xml' ;
2019-07-17 20:16:19 +00:00
$len = iconv_strlen ( $text , 'UTF-8' );
2019-07-17 20:08:50 +00:00
if ( 0 == $len ) {
throw new Zend_Service_Twitter_Exception ( 'Direct message must contain at least one character' );
} elseif ( 140 < $len ) {
throw new Zend_Service_Twitter_Exception ( 'Direct message must contain no more than 140 characters' );
}
2019-07-17 20:31:04 +00:00
$data = array ( 'user' => $user , 'text' => $text );
$response = $this -> _post ( $path , $data );
2019-07-17 20:08:50 +00:00
return new Zend_Rest_Client_Result ( $response -> getBody ());
}
/**
* Destroy a direct message
*
* @ param int $id ID of message to destroy
2019-07-17 20:31:04 +00:00
* @ throws Zend_Http_Client_Exception if HTTP request fails or times out
2019-07-17 20:08:50 +00:00
* @ return Zend_Rest_Client_Result
*/
public function directMessageDestroy ( $id )
{
$this -> _init ();
2019-07-17 20:31:04 +00:00
$path = '/direct_messages/destroy/' . $this -> _validInteger ( $id ) . '.xml' ;
$response = $this -> _post ( $path );
2019-07-17 20:08:50 +00:00
return new Zend_Rest_Client_Result ( $response -> getBody ());
}
/**
* Create friendship
*
* @ param int | string $id User ID or name of new friend
2019-07-17 20:31:04 +00:00
* @ throws Zend_Http_Client_Exception if HTTP request fails or times out
2019-07-17 20:08:50 +00:00
* @ return Zend_Rest_Client_Result
*/
public function friendshipCreate ( $id )
{
$this -> _init ();
$path = '/friendships/create/' . $id . '.xml' ;
2019-07-17 20:31:04 +00:00
$response = $this -> _post ( $path );
2019-07-17 20:08:50 +00:00
return new Zend_Rest_Client_Result ( $response -> getBody ());
}
/**
* Destroy friendship
*
* @ param int | string $id User ID or name of friend to remove
2019-07-17 20:31:04 +00:00
* @ throws Zend_Http_Client_Exception if HTTP request fails or times out
2019-07-17 20:08:50 +00:00
* @ return Zend_Rest_Client_Result
*/
public function friendshipDestroy ( $id )
{
$this -> _init ();
$path = '/friendships/destroy/' . $id . '.xml' ;
2019-07-17 20:31:04 +00:00
$response = $this -> _post ( $path );
2019-07-17 20:08:50 +00:00
return new Zend_Rest_Client_Result ( $response -> getBody ());
}
/**
* Friendship exists
*
* @ param int | string $id User ID or name of friend to see if they are your friend
2019-07-17 20:31:04 +00:00
* @ throws Zend_Http_Client_Exception if HTTP request fails or times out
2019-07-17 20:08:50 +00:00
* @ return Zend_Rest_Client_result
*/
public function friendshipExists ( $id )
{
$this -> _init ();
$path = '/friendships/exists.xml' ;
2019-07-17 20:31:04 +00:00
$data = array ( 'user_a' => $this -> getUsername (), 'user_b' => $id );
$response = $this -> _get ( $path , $data );
2019-07-17 20:08:50 +00:00
return new Zend_Rest_Client_Result ( $response -> getBody ());
}
/**
* Verify Account Credentials
2019-07-17 20:31:04 +00:00
* @ throws Zend_Http_Client_Exception if HTTP request fails or times out
2019-07-17 20:08:50 +00:00
*
* @ return Zend_Rest_Client_Result
*/
public function accountVerifyCredentials ()
{
$this -> _init ();
2019-07-17 20:31:04 +00:00
$response = $this -> _get ( '/account/verify_credentials.xml' );
2019-07-17 20:08:50 +00:00
return new Zend_Rest_Client_Result ( $response -> getBody ());
}
/**
* End current session
*
2019-07-17 20:31:04 +00:00
* @ throws Zend_Http_Client_Exception if HTTP request fails or times out
2019-07-17 20:08:50 +00:00
* @ return true
*/
public function accountEndSession ()
{
$this -> _init ();
2019-07-17 20:31:04 +00:00
$this -> _get ( '/account/end_session' );
2019-07-17 20:08:50 +00:00
return true ;
}
/**
* Returns the number of api requests you have left per hour .
*
2019-07-17 20:31:04 +00:00
* @ throws Zend_Http_Client_Exception if HTTP request fails or times out
2019-07-17 20:08:50 +00:00
* @ return Zend_Rest_Client_Result
*/
public function accountRateLimitStatus ()
{
$this -> _init ();
2019-07-17 20:31:04 +00:00
$response = $this -> _get ( '/account/rate_limit_status.xml' );
2019-07-17 20:08:50 +00:00
return new Zend_Rest_Client_Result ( $response -> getBody ());
}
/**
* Fetch favorites
*
* $params may contain one or more of the following :
* - 'id' : Id of a user for whom to fetch favorites
* - 'page' : Retrieve a different page of resuls
*
* @ param array $params
2019-07-17 20:31:04 +00:00
* @ throws Zend_Http_Client_Exception if HTTP request fails or times out
2019-07-17 20:08:50 +00:00
* @ return Zend_Rest_Client_Result
*/
public function favoriteFavorites ( array $params = array ())
{
$this -> _init ();
$path = '/favorites' ;
2019-07-17 20:16:19 +00:00
$_params = array ();
2019-07-17 20:08:50 +00:00
foreach ( $params as $key => $value ) {
switch ( strtolower ( $key )) {
case 'id' :
2019-07-17 20:31:04 +00:00
$path .= '/' . $this -> _validInteger ( $value );
2019-07-17 20:08:50 +00:00
break ;
case 'page' :
2019-07-17 20:16:19 +00:00
$_params [ 'page' ] = ( int ) $value ;
2019-07-17 20:08:50 +00:00
break ;
default :
break ;
}
}
$path .= '.xml' ;
2019-07-17 20:31:04 +00:00
$response = $this -> _get ( $path , $_params );
2019-07-17 20:08:50 +00:00
return new Zend_Rest_Client_Result ( $response -> getBody ());
}
/**
* Mark a status as a favorite
*
* @ param int $id Status ID you want to mark as a favorite
2019-07-17 20:31:04 +00:00
* @ throws Zend_Http_Client_Exception if HTTP request fails or times out
2019-07-17 20:08:50 +00:00
* @ return Zend_Rest_Client_Result
*/
public function favoriteCreate ( $id )
{
$this -> _init ();
2019-07-17 20:31:04 +00:00
$path = '/favorites/create/' . $this -> _validInteger ( $id ) . '.xml' ;
$response = $this -> _post ( $path );
2019-07-17 20:08:50 +00:00
return new Zend_Rest_Client_Result ( $response -> getBody ());
}
/**
* Remove a favorite
*
* @ param int $id Status ID you want to de - list as a favorite
2019-07-17 20:31:04 +00:00
* @ throws Zend_Http_Client_Exception if HTTP request fails or times out
2019-07-17 20:08:50 +00:00
* @ return Zend_Rest_Client_Result
*/
public function favoriteDestroy ( $id )
{
$this -> _init ();
2019-07-17 20:31:04 +00:00
$path = '/favorites/destroy/' . $this -> _validInteger ( $id ) . '.xml' ;
$response = $this -> _post ( $path );
return new Zend_Rest_Client_Result ( $response -> getBody ());
}
2019-07-17 20:08:50 +00:00
2019-07-17 20:31:04 +00:00
/**
* Blocks the user specified in the ID parameter as the authenticating user .
* Destroys a friendship to the blocked user if it exists .
*
* @ param integer | string $id The ID or screen name of a user to block .
* @ return Zend_Rest_Client_Result
*/
public function blockCreate ( $id )
{
$this -> _init ();
$path = '/blocks/create/' . $id . '.xml' ;
$response = $this -> _post ( $path );
2019-07-17 20:08:50 +00:00
return new Zend_Rest_Client_Result ( $response -> getBody ());
}
2019-07-17 20:31:04 +00:00
/**
* Un - blocks the user specified in the ID parameter for the authenticating user
*
* @ param integer | string $id The ID or screen_name of the user to un - block .
* @ return Zend_Rest_Client_Result
*/
public function blockDestroy ( $id )
{
$this -> _init ();
$path = '/blocks/destroy/' . $id . '.xml' ;
$response = $this -> _post ( $path );
return new Zend_Rest_Client_Result ( $response -> getBody ());
}
/**
* Returns if the authenticating user is blocking a target user .
*
* @ param string | integer $id The ID or screen_name of the potentially blocked user .
* @ param boolean $returnResult Instead of returning a boolean return the rest response from twitter
* @ return Boolean | Zend_Rest_Client_Result
*/
public function blockExists ( $id , $returnResult = false )
{
$this -> _init ();
$path = '/blocks/exists/' . $id . '.xml' ;
$response = $this -> _get ( $path );
$cr = new Zend_Rest_Client_Result ( $response -> getBody ());
if ( $returnResult === true )
return $cr ;
if ( ! empty ( $cr -> request )) {
return false ;
}
return true ;
}
/**
* Returns an array of user objects that the authenticating user is blocking
*
* @ param integer $page Optional . Specifies the page number of the results beginning at 1. A single page contains 20 ids .
* @ param boolean $returnUserIds Optional . Returns only the userid ' s instead of the whole user object
* @ return Zend_Rest_Client_Result
*/
public function blockBlocking ( $page = 1 , $returnUserIds = false )
{
$this -> _init ();
$path = '/blocks/blocking' ;
if ( $returnUserIds === true ) {
$path .= '/ids' ;
}
$path .= '.xml' ;
$response = $this -> _get ( $path , array ( 'page' => $page ));
return new Zend_Rest_Client_Result ( $response -> getBody ());
}
/**
* Protected function to validate that the integer is valid or return a 0
* @ param $int
* @ throws Zend_Http_Client_Exception if HTTP request fails or times out
* @ return integer
*/
protected function _validInteger ( $int )
{
if ( preg_match ( " /( \ d+)/ " , $int )) {
return $int ;
}
return 0 ;
}
/**
* Validate a screen name using Twitter rules
*
* @ param string $name
* @ throws Zend_Service_Twitter_Exception
* @ return string
*/
protected function _validateScreenName ( $name )
{
if ( ! preg_match ( '/^[a-zA-Z0-9_]{0,20}$/' , $name )) {
require_once 'Zend/Service/Twitter/Exception.php' ;
throw new Zend_Service_Twitter_Exception ( 'Screen name, "' . $name . '" should only contain alphanumeric characters and' . ' underscores, and not exceed 15 characters.' );
}
return $name ;
}
/**
* Call a remote REST web service URI and return the Zend_Http_Response object
*
* @ param string $path The path to append to the URI
* @ throws Zend_Rest_Client_Exception
* @ return void
*/
protected function _prepare ( $path )
{
// Get the URI object and configure it
if ( ! $this -> _uri instanceof Zend_Uri_Http ) {
require_once 'Zend/Rest/Client/Exception.php' ;
throw new Zend_Rest_Client_Exception ( 'URI object must be set before performing call' );
}
$uri = $this -> _uri -> getUri ();
if ( $path [ 0 ] != '/' && $uri [ strlen ( $uri ) - 1 ] != '/' ) {
$path = '/' . $path ;
}
$this -> _uri -> setPath ( $path );
/**
* Get the HTTP client and configure it for the endpoint URI . Do this each time
* because the Zend_Http_Client instance is shared among all Zend_Service_Abstract subclasses .
*/
$this -> _localHttpClient -> resetParameters () -> setUri ( $this -> _uri );
}
/**
* Performs an HTTP GET request to the $path .
*
* @ param string $path
* @ param array $query Array of GET parameters
* @ throws Zend_Http_Client_Exception
* @ return Zend_Http_Response
*/
protected function _get ( $path , array $query = null )
{
$this -> _prepare ( $path );
$this -> _localHttpClient -> setParameterGet ( $query );
return $this -> _localHttpClient -> request ( 'GET' );
}
/**
* Performs an HTTP POST request to $path .
*
* @ param string $path
* @ param mixed $data Raw data to send
* @ throws Zend_Http_Client_Exception
* @ return Zend_Http_Response
*/
protected function _post ( $path , $data = null )
{
$this -> _prepare ( $path );
return $this -> _performPost ( 'POST' , $data );
}
/**
* Perform a POST or PUT
*
* Performs a POST or PUT request . Any data provided is set in the HTTP
* client . String data is pushed in as raw POST data ; array or object data
* is pushed in as POST parameters .
*
* @ param mixed $method
* @ param mixed $data
* @ return Zend_Http_Response
*/
protected function _performPost ( $method , $data = null )
{
$client = $this -> _localHttpClient ;
if ( is_string ( $data )) {
$client -> setRawData ( $data );
} elseif ( is_array ( $data ) || is_object ( $data )) {
$client -> setParameterPost (( array ) $data );
}
return $client -> request ( $method );
}
2019-07-17 20:08:50 +00:00
}