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)
13 /* Contains the last HTTP status code returned. */
15 /* Contains the last API call. */
17 /* Set up the API root URL. */
18 public $host = "http://api.tumblr.com/v2/";
19 /* Set timeout default. */
21 /* Set connect timeout. */
22 public $connecttimeout = 30;
23 /* Verify SSL Cert. */
24 public $ssl_verifypeer = FALSE;
26 public $format = 'json';
27 /* Decode returned json data. */
28 public $decode_json = TRUE;
29 /* Contains the last HTTP headers returned. */
31 /* Set the useragnet. */
32 public $useragent = 'TumblrOAuth v0.2.0-beta2';
33 /* Immediately retry the API call if the response was not successful. */
34 //public $retry = TRUE;
42 function accessTokenURL() { return 'http://www.tumblr.com/oauth/access_token'; }
43 function authenticateURL() { return 'http://www.tumblr.com/oauth/authorize'; }
44 function authorizeURL() { return 'http://www.tumblr.com/oauth/authorize'; }
45 function requestTokenURL() { return 'http://www.tumblr.com/oauth/request_token'; }
50 function lastStatusCode() { return $this->http_status; }
51 function lastAPICall() { return $this->last_api_call; }
54 * construct TumblrOAuth object
56 function __construct($consumer_key, $consumer_secret, $oauth_token = NULL, $oauth_token_secret = NULL) {
57 $this->sha1_method = new OAuthSignatureMethod_HMAC_SHA1();
58 $this->consumer = new OAuthConsumer($consumer_key, $consumer_secret);
59 if (!empty($oauth_token) && !empty($oauth_token_secret)) {
60 $this->token = new OAuthConsumer($oauth_token, $oauth_token_secret);
68 * Get a request_token from Tumblr
70 * @returns a key/value array containing oauth_token and oauth_token_secret
72 function getRequestToken($oauth_callback = NULL) {
73 $parameters = array();
74 if (!empty($oauth_callback)) {
75 $parameters['oauth_callback'] = $oauth_callback;
77 $request = $this->oAuthRequest($this->requestTokenURL(), 'GET', $parameters);
78 $token = OAuthUtil::parse_parameters($request);
79 $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
84 * Get the authorize URL
88 function getAuthorizeURL($token, $sign_in_with_tumblr = TRUE) {
89 if (is_array($token)) {
90 $token = $token['oauth_token'];
92 if (empty($sign_in_with_tumblr)) {
93 return $this->authorizeURL() . "?oauth_token={$token}";
95 return $this->authenticateURL() . "?oauth_token={$token}";
100 * Exchange request token and secret for an access token and
101 * secret, to sign API calls.
103 * @returns array("oauth_token" => "the-access-token",
104 * "oauth_token_secret" => "the-access-secret",
105 * "user_id" => "9436992",
106 * "screen_name" => "abraham")
108 function getAccessToken($oauth_verifier = FALSE) {
109 $parameters = array();
110 if (!empty($oauth_verifier)) {
111 $parameters['oauth_verifier'] = $oauth_verifier;
113 $request = $this->oAuthRequest($this->accessTokenURL(), 'GET', $parameters);
114 $token = OAuthUtil::parse_parameters($request);
115 $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
120 * One time exchange of username and password for access token and secret.
122 * @returns array("oauth_token" => "the-access-token",
123 * "oauth_token_secret" => "the-access-secret",
124 * "user_id" => "9436992",
125 * "screen_name" => "abraham",
126 * "x_auth_expires" => "0")
128 function getXAuthToken($username, $password) {
129 $parameters = array();
130 $parameters['x_auth_username'] = $username;
131 $parameters['x_auth_password'] = $password;
132 $parameters['x_auth_mode'] = 'client_auth';
133 $request = $this->oAuthRequest($this->accessTokenURL(), 'POST', $parameters);
134 $token = OAuthUtil::parse_parameters($request);
135 $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
140 * GET wrapper for oAuthRequest.
142 function get($url, $parameters = array()) {
143 $response = $this->oAuthRequest($url, 'GET', $parameters);
144 if ($this->format === 'json' && $this->decode_json) {
145 return json_decode($response);
151 * POST wrapper for oAuthRequest.
153 function post($url, $parameters = array()) {
154 $response = $this->oAuthRequest($url, 'POST', $parameters);
155 if ($this->format === 'json' && $this->decode_json) {
156 return json_decode($response);
162 * DELETE wrapper for oAuthReqeust.
164 function delete($url, $parameters = array()) {
165 $response = $this->oAuthRequest($url, 'DELETE', $parameters);
166 if ($this->format === 'json' && $this->decode_json) {
167 return json_decode($response);
173 * Format and sign an OAuth / API request
175 function oAuthRequest($url, $method, $parameters) {
176 if (strrpos($url, 'https://') !== 0 && strrpos($url, 'http://') !== 0) {
177 $url = "{$this->host}{$url}";
179 $request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $parameters);
180 $request->sign_request($this->sha1_method, $this->consumer, $this->token);
183 return $this->http($request->to_url(), 'GET');
185 return $this->http($request->get_normalized_http_url(), $method, $request->to_postdata());
190 * Make an HTTP request
192 * @return API results
194 function http($url, $method, $postfields = NULL) {
195 $this->http_info = array();
198 curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent);
199 curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);
200 curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);
201 curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
202 curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:'));
203 curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);
204 curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
205 curl_setopt($ci, CURLOPT_HEADER, FALSE);
209 curl_setopt($ci, CURLOPT_POST, TRUE);
210 if (!empty($postfields)) {
211 curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
215 curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
216 if (!empty($postfields)) {
217 $url = "{$url}?{$postfields}";
221 curl_setopt($ci, CURLOPT_URL, $url);
222 $response = curl_exec($ci);
223 $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
224 $this->http_info = array_merge($this->http_info, curl_getinfo($ci));
231 * Get the header info to store.
233 function getHeader($ch, $header) {
234 $i = strpos($header, ':');
236 $key = str_replace('-', '_', strtolower(substr($header, 0, $i)));
237 $value = trim(substr($header, $i + 2));
238 $this->http_header[$key] = $value;
240 return strlen($header);