class Twitter in Twitter 6.3
Same name and namespace in other branches
- 6.5 twitter.lib.php \Twitter
- 7.6 twitter.lib.php \Twitter
- 7.3 twitter.lib.php \Twitter
- 7.5 twitter.lib.php \Twitter
Primary Twitter API implementation class Supports the full REST API for twitter.
Hierarchy
- class \Twitter
Expanded class hierarchy of Twitter
8 string references to 'Twitter'
- TwitterInputFilterTest::getInfo in tests/
input_filters.test - TwitterOAuthTest::getInfo in tests/
core_oauth.test - TwitterTest::getInfo in tests/
core.test - twitter_actions_rules_action_info_alter in twitter_actions/
twitter_actions.rules.inc - Implementation of hook_rules_action_info_alter().
- twitter_menu in ./
twitter.module - Implementation of hook_meu()
File
- ./
twitter.lib.php, line 84 - Classes to implement the full Twitter API
View source
class Twitter {
/**
* @var $format API format to use: can be json or xml
*/
protected $format = 'json';
/**
* @var $source the twitter api 'source'
*/
protected $source = 'drupal';
/**
* @var $username Twitter username to use for authenticated requests
*/
protected $username;
/**
* @var $password Twitter password to use for authenticated requests
*/
protected $password;
/**
* Constructor for the Twitter class
*/
public function __construct($username = NULL, $password = NULL) {
$this
->set_auth($username, $password);
}
/**
* Set the username and password
*/
public function set_auth($username, $password) {
$this->username = $username;
$this->password = $password;
}
/**
* Get an array of TwitterStatus objects from an API endpoint
*/
protected function get_statuses($path, $params = array(), $use_auth = FALSE) {
$values = $this
->call($path, $params, 'GET', $use_auth);
// Check on successfull call
if ($values) {
$statuses = array();
foreach ($values as $status) {
$statuses[] = new TwitterStatus($status);
}
return $statuses;
}
else {
// As call allready throws an exception, we can return an empty array to
// break no code.
return array();
}
}
/**
* Fetch the public timeline
*
* @see http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-statuses-public_timeline
*/
public function public_timeline() {
return $this
->get_statuses('statuses/public_timeline');
}
/**
* Fetch the authenticated user's friends timeline
*
* @see http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-statuses-friends_timeline
*/
public function friends_timeline($params = array()) {
return $this
->get_statuses('statuses/friends_timeline', $params, TRUE);
}
/**
* Fetch a user's timeline
*
* @see http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-statuses-user_timeline
*/
public function user_timeline($id, $params = array(), $use_auth = FALSE) {
if (is_numeric($id)) {
$params['user_id'] = $id;
}
else {
$params['screen_name'] = $id;
}
return $this
->get_statuses('statuses/user_timeline', $params, $use_auth);
}
/**
*
* @see http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-statuses-mentions
*/
public function mentions($params = array()) {
return $this
->get_statuses('statuses/mentions', $params, TRUE);
}
/**
* Post a new status.
*
* @see https://dev.twitter.com/docs/api/1/post/statuses/update
*/
public function status_update($status, $params = array()) {
$params['status'] = $status;
if ($this->source) {
$params['source'] = $this->source;
}
if ($values = $this
->call('statuses/update', $params, 'POST', TRUE)) {
return new TwitterStatus($values);
}
}
/**
* Returns profile information about a user.
*
* @see https://dev.twitter.com/docs/api/1/get/users/show
*/
public function users_show($id, $use_auth = TRUE) {
$params = array();
if (is_numeric($id)) {
$params['user_id'] = $id;
}
else {
$params['screen_name'] = $id;
}
if ($values = $this
->call('users/show', $params, 'GET', $use_auth)) {
return new TwitterUser($values);
}
}
/**
*
* @see https://dev.twitter.com/docs/api/1/get/account/verify_credentials
*/
public function verify_credentials() {
if ($values = $this
->call('account/verify_credentials', array(), 'GET', TRUE)) {
return new TwitterUser($values);
}
}
/**
* Calls a twitter api resource
*
* @param $path
* string REST resource to be called
* @param $params
* array of settings to be sent along
* @param $method
* string method to be used (GET or POST)
* @param $use_oauth
* boolean indicating if the call should use OAuth authentication of not
*/
public function call($path, $params = array(), $method = 'GET', $use_auth = FALSE) {
$url = $this
->create_url($path);
try {
if ($use_auth) {
$response = $this
->auth_request($url, $params, $method);
}
else {
$response = $this
->request($url, $params, $method);
}
} catch (TwitterException $e) {
watchdog('twitter', '!message', array(
'!message' => $e
->__toString(),
), WATCHDOG_ERROR);
drupal_set_message('Twitter returned an error: ' . $e
->getMessage(), 'error');
return FALSE;
}
if (!$response) {
return FALSE;
}
return $this
->parse_response($response);
}
/**
* Perform an authentication required request.
*/
protected function auth_request($path, $params = array(), $method = 'GET') {
if (empty($this->username) || empty($this->password)) {
return false;
}
return $this
->request($path, $params, $method, TRUE);
}
/**
* Perform a request
*/
protected function request($url, $params = array(), $method = 'GET', $use_auth = FALSE) {
$data = '';
if (count($params) > 0) {
if ($method == 'GET') {
$url .= '?' . http_build_query($params, '', '&');
}
else {
$data = http_build_query($params, '', '&');
}
}
$headers = array();
if ($use_auth) {
$headers['Authorization'] = 'Basic ' . base64_encode($this->username . ':' . $this->password);
$headers['Content-type'] = 'application/x-www-form-urlencoded';
}
$response = drupal_http_request($url, $headers, $method, $data);
if (!property_exists($response, 'error')) {
return $response->data;
}
else {
$error = $response->error;
$data = $this
->parse_response($response->data);
if ($data['error']) {
$error = $data['error'];
}
throw new TwitterException($error);
}
}
protected function parse_response($response, $format = NULL) {
if (empty($format)) {
$format = $this->format;
}
switch ($format) {
case 'json':
$response_decoded = json_decode($response, TRUE);
if (isset($response_decoded['id_str'])) {
// if we're getting a single object back as JSON
$response_decoded['id'] = $response_decoded['id_str'];
}
else {
// if we're getting an array of objects back as JSON
foreach ($response_decoded as &$item) {
$item['id'] = $item['id_str'];
}
}
return $response_decoded;
}
}
protected function create_url($path, $format = NULL) {
if (is_null($format)) {
$format = $this->format;
}
$conf = TwitterConf::instance();
$url = 'http://' . $conf
->get('api') . '/1/' . $path;
if (!empty($format)) {
$url .= '.' . $this->format;
}
return $url;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Twitter:: |
protected | property | ||
Twitter:: |
protected | property | ||
Twitter:: |
protected | property | ||
Twitter:: |
protected | property | ||
Twitter:: |
protected | function | Perform an authentication required request. | 1 |
Twitter:: |
public | function | Calls a twitter api resource | |
Twitter:: |
protected | function | ||
Twitter:: |
public | function | Fetch the authenticated user's friends timeline | |
Twitter:: |
protected | function | Get an array of TwitterStatus objects from an API endpoint | |
Twitter:: |
public | function | ||
Twitter:: |
protected | function | ||
Twitter:: |
public | function | Fetch the public timeline | |
Twitter:: |
protected | function | Perform a request | |
Twitter:: |
public | function | Set the username and password | |
Twitter:: |
public | function | Post a new status. | |
Twitter:: |
public | function | Returns profile information about a user. | |
Twitter:: |
public | function | Fetch a user's timeline | |
Twitter:: |
public | function | ||
Twitter:: |
public | function | Constructor for the Twitter class | 1 |