Campustream 1.0
A social network MQP for WPI
application/lib/twitteroauth/twitteroauth.php
Go to the documentation of this file.
00001 <?php
00002 
00003 /*
00004  * Abraham Williams (abraham@abrah.am) http://abrah.am
00005  *
00006  * The first PHP Library to support OAuth for Twitter's REST API.
00007  */
00008 
00009 /* Load OAuth lib. You can find it at http://oauth.net */
00010 require_once('OAuth.php');
00011 
00015 class TwitterOAuth {
00016   /* Contains the last HTTP status code returned. */
00017   public $http_code;
00018   /* Contains the last API call. */
00019   public $url;
00020   /* Set up the API root URL. */
00021   public $host = "https://api.twitter.com/1/";
00022   /* Set timeout default. */
00023   public $timeout = 30;
00024   /* Set connect timeout. */
00025   public $connecttimeout = 30; 
00026   /* Verify SSL Cert. */
00027   public $ssl_verifypeer = FALSE;
00028   /* Respons format. */
00029   public $format = 'json';
00030   /* Decode returned json data. */
00031   public $decode_json = TRUE;
00032   /* Contains the last HTTP headers returned. */
00033   public $http_info;
00034   /* Set the useragnet. */
00035   public $useragent = 'TwitterOAuth v0.2.0-beta2';
00036   /* Immediately retry the API call if the response was not successful. */
00037   //public $retry = TRUE;
00038 
00039 
00040 
00041 
00045   function accessTokenURL()  { return 'https://api.twitter.com/oauth/access_token'; }
00046   function authenticateURL() { return 'https://twitter.com/oauth/authenticate'; }
00047   function authorizeURL()    { return 'https://twitter.com/oauth/authorize'; }
00048   function requestTokenURL() { return 'https://api.twitter.com/oauth/request_token'; }
00049 
00053   function lastStatusCode() { return $this->http_status; }
00054   function lastAPICall() { return $this->last_api_call; }
00055 
00059   function __construct($consumer_key, $consumer_secret, $oauth_token = NULL, $oauth_token_secret = NULL) {
00060     $this->sha1_method = new OAuthSignatureMethod_HMAC_SHA1();
00061     $this->consumer = new OAuthConsumer($consumer_key, $consumer_secret);
00062     if (!empty($oauth_token) && !empty($oauth_token_secret)) {
00063       $this->token = new OAuthConsumer($oauth_token, $oauth_token_secret);
00064     } else {
00065       $this->token = NULL;
00066     }
00067   }
00068 
00069 
00075   function getRequestToken($oauth_callback = NULL) {
00076     $parameters = array();
00077     if (!empty($oauth_callback)) {
00078       $parameters['oauth_callback'] = $oauth_callback;
00079     } 
00080     $request = $this->oAuthRequest($this->requestTokenURL(), 'GET', $parameters);
00081     $token = OAuthUtil::parse_parameters($request);
00082     $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
00083     return $token;
00084   }
00085 
00091   function getAuthorizeURL($token, $sign_in_with_twitter = TRUE) {
00092     if (is_array($token)) {
00093       $token = $token['oauth_token'];
00094     }
00095     if (empty($sign_in_with_twitter)) {
00096       return $this->authorizeURL() . "?oauth_token={$token}";
00097     } else {
00098        return $this->authenticateURL() . "?oauth_token={$token}";
00099     }
00100   }
00101 
00111   function getAccessToken($oauth_verifier = FALSE) {
00112     $parameters = array();
00113     if (!empty($oauth_verifier)) {
00114       $parameters['oauth_verifier'] = $oauth_verifier;
00115     }
00116     $request = $this->oAuthRequest($this->accessTokenURL(), 'GET', $parameters);
00117     $token = OAuthUtil::parse_parameters($request);
00118     $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
00119     return $token;
00120   }
00121 
00131   function getXAuthToken($username, $password) {
00132     $parameters = array();
00133     $parameters['x_auth_username'] = $username;
00134     $parameters['x_auth_password'] = $password;
00135     $parameters['x_auth_mode'] = 'client_auth';
00136     $request = $this->oAuthRequest($this->accessTokenURL(), 'POST', $parameters);
00137     $token = OAuthUtil::parse_parameters($request);
00138     $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
00139     return $token;
00140   }
00141 
00145   function get($url, $parameters = array()) {
00146     $response = $this->oAuthRequest($url, 'GET', $parameters);
00147     if ($this->format === 'json' && $this->decode_json) {
00148       return json_decode($response);
00149     }
00150     return $response;
00151   }
00152   
00156   function post($url, $parameters = array()) {
00157     $response = $this->oAuthRequest($url, 'POST', $parameters);
00158     if ($this->format === 'json' && $this->decode_json) {
00159       return json_decode($response);
00160     }
00161     return $response;
00162   }
00163 
00167   function delete($url, $parameters = array()) {
00168     $response = $this->oAuthRequest($url, 'DELETE', $parameters);
00169     if ($this->format === 'json' && $this->decode_json) {
00170       return json_decode($response);
00171     }
00172     return $response;
00173   }
00174 
00178   function oAuthRequest($url, $method, $parameters) {
00179     if (strrpos($url, 'https://') !== 0 && strrpos($url, 'http://') !== 0) {
00180       $url = "{$this->host}{$url}.{$this->format}";
00181     }
00182     $request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $parameters);
00183     $request->sign_request($this->sha1_method, $this->consumer, $this->token);
00184     switch ($method) {
00185     case 'GET':
00186       return $this->http($request->to_url(), 'GET');
00187     default:
00188       return $this->http($request->get_normalized_http_url(), $method, $request->to_postdata());
00189     }
00190   }
00191 
00197   function http($url, $method, $postfields = NULL) {
00198     $this->http_info = array();
00199     $ci = curl_init();
00200     /* Curl settings */
00201     curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent);
00202     curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);
00203     curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);
00204     curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
00205     curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:'));
00206     curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);
00207     curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
00208     curl_setopt($ci, CURLOPT_HEADER, FALSE);
00209 
00210     switch ($method) {
00211       case 'POST':
00212         curl_setopt($ci, CURLOPT_POST, TRUE);
00213         if (!empty($postfields)) {
00214           curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
00215         }
00216         break;
00217       case 'DELETE':
00218         curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
00219         if (!empty($postfields)) {
00220           $url = "{$url}?{$postfields}";
00221         }
00222     }
00223 
00224     curl_setopt($ci, CURLOPT_URL, $url);
00225     $response = curl_exec($ci);
00226     $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
00227     $this->http_info = array_merge($this->http_info, curl_getinfo($ci));
00228     $this->url = $url;
00229     curl_close ($ci);
00230     return $response;
00231   }
00232 
00236   function getHeader($ch, $header) {
00237     $i = strpos($header, ':');
00238     if (!empty($i)) {
00239       $key = str_replace('-', '_', strtolower(substr($header, 0, $i)));
00240       $value = trim(substr($header, $i + 2));
00241       $this->http_header[$key] = $value;
00242     }
00243     return strlen($header);
00244   }
00245 }