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 /* Set up the API root URL. */
15 public $host = "https://api.tumblr.com/v2/";
16 /* Set timeout default. */
18 /* Set connect timeout. */
19 public $connecttimeout = 30;
20 /* Verify SSL Cert. */
21 public $ssl_verifypeer = FALSE;
22 /* Response format. */
23 public $format = 'json';
24 /* Decode returned json data. */
25 public $decode_json = TRUE;
26 /* Set the useragent. */
27 public $useragent = 'TumblrOAuth v0.2.0-beta2';
29 /* Contains the last HTTP status code returned. */
31 /* Contains the last API call. */
34 * Contains the last HTTP headers returned.
39 * Contains the last HTTP request info
44 /** @var OAuthToken */
46 /** @var OAuthConsumer */
48 /** @var OAuthSignatureMethod_HMAC_SHA1 */
54 function accessTokenURL()
56 return 'https://www.tumblr.com/oauth/access_token';
59 function authenticateURL()
61 return 'https://www.tumblr.com/oauth/authorize';
64 function authorizeURL()
66 return 'https://www.tumblr.com/oauth/authorize';
69 function requestTokenURL()
71 return 'https://www.tumblr.com/oauth/request_token';
74 function __construct($consumer_key, $consumer_secret, $oauth_token = null, $oauth_token_secret = null)
76 $this->sha1_method = new OAuthSignatureMethod_HMAC_SHA1();
77 $this->consumer = new OAuthConsumer($consumer_key, $consumer_secret);
78 if (!empty($oauth_token) && !empty($oauth_token_secret)) {
79 $this->token = new OAuthToken($oauth_token, $oauth_token_secret);
86 * Get a request_token from Tumblr
88 * @param callback $oauth_callback
91 function getRequestToken($oauth_callback = null)
94 if (!empty($oauth_callback)) {
95 $parameters['oauth_callback'] = $oauth_callback;
98 $request = $this->oAuthRequest($this->requestTokenURL(), 'GET', $parameters);
99 $token = OAuthUtil::parse_parameters($request);
100 $this->token = new OAuthToken($token['oauth_token'], $token['oauth_token_secret']);
105 * Get the authorize URL
107 * @param array $token
108 * @param bool $sign_in_with_tumblr
111 function getAuthorizeURL($token, $sign_in_with_tumblr = TRUE)
113 if (is_array($token)) {
114 $token = $token['oauth_token'];
117 if (empty($sign_in_with_tumblr)) {
118 return $this->authorizeURL() . "?oauth_token={$token}";
120 return $this->authenticateURL() . "?oauth_token={$token}";
125 * Exchange request token and secret for an access token and
126 * secret, to sign API calls.
128 * @param bool $oauth_verifier
129 * @return 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)
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 OAuthToken($token['oauth_token'], $token['oauth_token_secret']);
149 * One time exchange of username and password for access token and secret.
151 * @param string $username
152 * @param string $password
153 * @return array ("oauth_token" => "the-access-token",
154 * "oauth_token_secret" => "the-access-secret",
155 * "user_id" => "9436992",
156 * "screen_name" => "abraham",
157 * "x_auth_expires" => "0")
159 function getXAuthToken($username, $password)
162 $parameters['x_auth_username'] = $username;
163 $parameters['x_auth_password'] = $password;
164 $parameters['x_auth_mode'] = 'client_auth';
165 $request = $this->oAuthRequest($this->accessTokenURL(), 'POST', $parameters);
166 $token = OAuthUtil::parse_parameters($request);
167 $this->token = new OAuthToken($token['oauth_token'], $token['oauth_token_secret']);
173 * GET wrapper for oAuthRequest.
176 * @param array $parameters
177 * @return mixed|string
179 function get($url, $parameters = [])
181 $response = $this->oAuthRequest($url, 'GET', $parameters);
182 if ($this->format === 'json' && $this->decode_json) {
183 return json_decode($response);
190 * POST wrapper for oAuthRequest.
193 * @param array $parameters
194 * @return mixed|string
196 function post($url, $parameters = [])
198 $response = $this->oAuthRequest($url, 'POST', $parameters);
199 if ($this->format === 'json' && $this->decode_json) {
200 return json_decode($response);
207 * DELETE wrapper for oAuthReqeust.
210 * @param array $parameters
211 * @return mixed|string
213 function delete($url, $parameters = [])
215 $response = $this->oAuthRequest($url, 'DELETE', $parameters);
216 if ($this->format === 'json' && $this->decode_json) {
217 return json_decode($response);
224 * Format and sign an OAuth / API request
227 * @param string $method
228 * @param array $parameters
229 * @return mixed|string
231 function oAuthRequest($url, $method, $parameters)
233 if (strrpos($url, 'https://') !== 0 && strrpos($url, 'http://') !== 0) {
234 $url = "{$this->host}{$url}";
237 $request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $parameters);
238 $request->sign_request($this->sha1_method, $this->consumer, $this->token);
241 return $this->http($request->to_url(), 'GET');
243 return $this->http($request->get_normalized_http_url(), $method, $request->to_postdata());
248 * Make an HTTP request
251 * @param string $method
252 * @param mixed $postfields
253 * @return string API results
255 function http($url, $method, $postfields = null)
257 $this->http_info = [];
260 curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent);
261 curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);
262 curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);
263 curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
264 curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:'));
265 curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);
266 curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
267 curl_setopt($ci, CURLOPT_HEADER, FALSE);
271 curl_setopt($ci, CURLOPT_POST, TRUE);
272 if (!empty($postfields)) {
273 curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
277 curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
278 if (!empty($postfields)) {
279 $url = "{$url}?{$postfields}";
283 curl_setopt($ci, CURLOPT_URL, $url);
284 $response = curl_exec($ci);
285 $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
286 $this->http_info = array_merge($this->http_info, curl_getinfo($ci));
294 * Get the header info to store.
296 * @param resource $ch
297 * @param string $header
300 function getHeader($ch, $header)
302 $i = strpos($header, ':');
304 $key = str_replace('-', '_', strtolower(substr($header, 0, $i)));
305 $value = trim(substr($header, $i + 2));
306 $this->http_header[$key] = $value;
309 return strlen($header);