4 * Abraham Williams (abraham@abrah.am) http://abrah.am
6 * The first PHP Library to support OAuth for Tumblr's REST API. (Originally for Twitter, modified for Tumblr by Lucas)
14 /* Contains the last HTTP status code returned. */
16 /* Contains the last API call. */
18 /* Set up the API root URL. */
19 public $host = "https://api.tumblr.com/v2/";
20 /* Set timeout default. */
22 /* Set connect timeout. */
23 public $connecttimeout = 30;
24 /* Verify SSL Cert. */
25 public $ssl_verifypeer = FALSE;
27 public $format = 'json';
28 /* Decode returned json data. */
29 public $decode_json = TRUE;
30 /* Contains the last HTTP headers returned. */
32 /* Set the useragnet. */
33 public $useragent = 'TumblrOAuth v0.2.0-beta2';
34 /* Immediately retry the API call if the response was not successful. */
35 //public $retry = TRUE;
41 function accessTokenURL()
43 return 'https://www.tumblr.com/oauth/access_token';
46 function authenticateURL()
48 return 'https://www.tumblr.com/oauth/authorize';
51 function authorizeURL()
53 return 'https://www.tumblr.com/oauth/authorize';
56 function requestTokenURL()
58 return 'https://www.tumblr.com/oauth/request_token';
64 function lastStatusCode()
66 return $this->http_status;
69 function lastAPICall()
71 return $this->last_api_call;
75 * construct TumblrOAuth object
77 function __construct($consumer_key, $consumer_secret, $oauth_token = null, $oauth_token_secret = null)
79 $this->sha1_method = new OAuthSignatureMethod_HMAC_SHA1();
80 $this->consumer = new OAuthConsumer($consumer_key, $consumer_secret);
81 if (!empty($oauth_token) && !empty($oauth_token_secret)) {
82 $this->token = new OAuthConsumer($oauth_token, $oauth_token_secret);
90 * Get a request_token from Tumblr
92 * @returns a key/value array containing oauth_token and oauth_token_secret
94 function getRequestToken($oauth_callback = null)
96 $parameters = array();
97 if (!empty($oauth_callback)) {
98 $parameters['oauth_callback'] = $oauth_callback;
101 $request = $this->oAuthRequest($this->requestTokenURL(), 'GET', $parameters);
102 $token = OAuthUtil::parse_parameters($request);
103 $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
108 * Get the authorize URL
112 function getAuthorizeURL($token, $sign_in_with_tumblr = TRUE)
114 if (is_array($token)) {
115 $token = $token['oauth_token'];
118 if (empty($sign_in_with_tumblr)) {
119 return $this->authorizeURL() . "?oauth_token={$token}";
121 return $this->authenticateURL() . "?oauth_token={$token}";
126 * Exchange request token and secret for an access token and
127 * secret, to sign API calls.
129 * @returns array("oauth_token" => "the-access-token",
130 * "oauth_token_secret" => "the-access-secret",
131 * "user_id" => "9436992",
132 * "screen_name" => "abraham")
134 function getAccessToken($oauth_verifier = FALSE)
136 $parameters = array();
137 if (!empty($oauth_verifier)) {
138 $parameters['oauth_verifier'] = $oauth_verifier;
141 $request = $this->oAuthRequest($this->accessTokenURL(), 'GET', $parameters);
142 $token = OAuthUtil::parse_parameters($request);
143 $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
149 * One time exchange of username and password for access token and secret.
151 * @returns array("oauth_token" => "the-access-token",
152 * "oauth_token_secret" => "the-access-secret",
153 * "user_id" => "9436992",
154 * "screen_name" => "abraham",
155 * "x_auth_expires" => "0")
157 function getXAuthToken($username, $password)
159 $parameters = array();
160 $parameters['x_auth_username'] = $username;
161 $parameters['x_auth_password'] = $password;
162 $parameters['x_auth_mode'] = 'client_auth';
163 $request = $this->oAuthRequest($this->accessTokenURL(), 'POST', $parameters);
164 $token = OAuthUtil::parse_parameters($request);
165 $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
171 * GET wrapper for oAuthRequest.
173 function get($url, $parameters = array())
175 $response = $this->oAuthRequest($url, 'GET', $parameters);
176 if ($this->format === 'json' && $this->decode_json) {
177 return json_decode($response);
184 * POST wrapper for oAuthRequest.
186 function post($url, $parameters = array())
188 $response = $this->oAuthRequest($url, 'POST', $parameters);
189 if ($this->format === 'json' && $this->decode_json) {
190 return json_decode($response);
197 * DELETE wrapper for oAuthReqeust.
199 function delete($url, $parameters = array())
201 $response = $this->oAuthRequest($url, 'DELETE', $parameters);
202 if ($this->format === 'json' && $this->decode_json) {
203 return json_decode($response);
210 * Format and sign an OAuth / API request
212 function oAuthRequest($url, $method, $parameters)
214 if (strrpos($url, 'https://') !== 0 && strrpos($url, 'http://') !== 0) {
215 $url = "{$this->host}{$url}";
218 $request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $parameters);
219 $request->sign_request($this->sha1_method, $this->consumer, $this->token);
222 return $this->http($request->to_url(), 'GET');
224 return $this->http($request->get_normalized_http_url(), $method, $request->to_postdata());
229 * Make an HTTP request
231 * @return API results
233 function http($url, $method, $postfields = null)
235 $this->http_info = array();
238 curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent);
239 curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);
240 curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);
241 curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
242 curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:'));
243 curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);
244 curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
245 curl_setopt($ci, CURLOPT_HEADER, FALSE);
249 curl_setopt($ci, CURLOPT_POST, TRUE);
250 if (!empty($postfields)) {
251 curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
255 curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
256 if (!empty($postfields)) {
257 $url = "{$url}?{$postfields}";
261 curl_setopt($ci, CURLOPT_URL, $url);
262 $response = curl_exec($ci);
263 $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
264 $this->http_info = array_merge($this->http_info, curl_getinfo($ci));
272 * Get the header info to store.
274 function getHeader($ch, $header)
276 $i = strpos($header, ':');
278 $key = str_replace('-', '_', strtolower(substr($header, 0, $i)));
279 $value = trim(substr($header, $i + 2));
280 $this->http_header[$key] = $value;
283 return strlen($header);