class Twitter in Twitter 7.3
Same name and namespace in other branches
- 6.5 twitter.lib.php \Twitter
- 6.3 twitter.lib.php \Twitter
- 7.6 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
7 string references to 'Twitter'
- TwitterInputFilterTest::getInfo in tests/
input_filters.test - TwitterTest::getInfo in tests/
core.test - twitter_actions_rules_action_info in twitter_actions/
twitter_actions.rules.inc - Implements hook_rules_action_info() on behalf of the twitter module.
- twitter_actions_rules_condition_info in twitter_actions/
twitter_actions.rules.inc - Implements hook_rules_condition_info().
- twitter_menu in ./
twitter.module - Implements hook_menu().
File
- ./
twitter.lib.php, line 31 - 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) {
if (!empty($username) && !empty($password)) {
$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 a user's timeline
*
* @see https://dev.twitter.com/docs/api/1/get/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 https://dev.twitter.com/docs/api/1/get/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
*
* @throws TwitterException
*/
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, array(
'headers' => $headers,
'method' => $method,
'data' => $data,
));
if (!isset($response->error)) {
return $response->data;
}
else {
$error = 'Unknown error.';
if (isset($response->error)) {
$error = $response->error;
}
if (isset($response->data) && ($data = $this
->parse_response($response->data))) {
if (is_array($data)) {
if (isset($data['errors'][0]['message'])) {
$error = $data['errors'][0]['message'];
}
elseif (isset($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':
// http://drupal.org/node/985544 - json_decode large integer issue
$length = strlen(PHP_INT_MAX);
$response = preg_replace('/"(id|in_reply_to_status_id)":(\\d{' . $length . ',})/', '"\\1":"\\2"', $response);
return json_decode($response, TRUE);
}
}
protected function create_url($path, $format = NULL) {
if (is_null($format)) {
$format = $this->format;
}
$url = variable_get('twitter_api', TWITTER_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:: |
protected | function | Get an array of TwitterStatus objects from an API endpoint | |
Twitter:: |
public | function | ||
Twitter:: |
protected | function | ||
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 |