4 * Abraham Williams (abraham@abrah.am) http://abrah.am
\r
6 * The first PHP Library to support OAuth for Tumblr's REST API. (Originally for Twitter, modified for Tumblr by Lucas)
\r
9 /* Load OAuth lib. You can find it at http://oauth.net */
\r
10 //require_once('OAuth.php');
\r
13 * Tumblr OAuth class
\r
16 /* Contains the last HTTP status code returned. */
\r
18 /* Contains the last API call. */
\r
20 /* Set up the API root URL. */
\r
21 public $host = "http://api.tumblr.com/v2/";
\r
22 /* Set timeout default. */
\r
23 public $timeout = 30;
\r
24 /* Set connect timeout. */
\r
25 public $connecttimeout = 30;
\r
26 /* Verify SSL Cert. */
\r
27 public $ssl_verifypeer = FALSE;
\r
28 /* Respons format. */
\r
29 public $format = 'json';
\r
30 /* Decode returned json data. */
\r
31 public $decode_json = TRUE;
\r
32 /* Contains the last HTTP headers returned. */
\r
34 /* Set the useragnet. */
\r
35 public $useragent = 'TumblrOAuth v0.2.0-beta2';
\r
36 /* Immediately retry the API call if the response was not successful. */
\r
37 //public $retry = TRUE;
\r
45 function accessTokenURL() { return 'http://www.tumblr.com/oauth/access_token'; }
\r
46 function authenticateURL() { return 'http://www.tumblr.com/oauth/authorize'; }
\r
47 function authorizeURL() { return 'http://www.tumblr.com/oauth/authorize'; }
\r
48 function requestTokenURL() { return 'http://www.tumblr.com/oauth/request_token'; }
\r
53 function lastStatusCode() { return $this->http_status; }
\r
54 function lastAPICall() { return $this->last_api_call; }
\r
57 * construct TumblrOAuth object
\r
59 function __construct($consumer_key, $consumer_secret, $oauth_token = NULL, $oauth_token_secret = NULL) {
\r
60 $this->sha1_method = new OAuthSignatureMethod_HMAC_SHA1();
\r
61 $this->consumer = new OAuthConsumer($consumer_key, $consumer_secret);
\r
62 if (!empty($oauth_token) && !empty($oauth_token_secret)) {
\r
63 $this->token = new OAuthConsumer($oauth_token, $oauth_token_secret);
\r
65 $this->token = NULL;
\r
71 * Get a request_token from Tumblr
\r
73 * @returns a key/value array containing oauth_token and oauth_token_secret
\r
75 function getRequestToken($oauth_callback = NULL) {
\r
76 $parameters = array();
\r
77 if (!empty($oauth_callback)) {
\r
78 $parameters['oauth_callback'] = $oauth_callback;
\r
80 $request = $this->oAuthRequest($this->requestTokenURL(), 'GET', $parameters);
\r
81 $token = OAuthUtil::parse_parameters($request);
\r
82 $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
\r
87 * Get the authorize URL
\r
91 function getAuthorizeURL($token, $sign_in_with_tumblr = TRUE) {
\r
92 if (is_array($token)) {
\r
93 $token = $token['oauth_token'];
\r
95 if (empty($sign_in_with_tumblr)) {
\r
96 return $this->authorizeURL() . "?oauth_token={$token}";
\r
98 return $this->authenticateURL() . "?oauth_token={$token}";
\r
103 * Exchange request token and secret for an access token and
\r
104 * secret, to sign API calls.
\r
106 * @returns array("oauth_token" => "the-access-token",
\r
107 * "oauth_token_secret" => "the-access-secret",
\r
108 * "user_id" => "9436992",
\r
109 * "screen_name" => "abraham")
\r
111 function getAccessToken($oauth_verifier = FALSE) {
\r
112 $parameters = array();
\r
113 if (!empty($oauth_verifier)) {
\r
114 $parameters['oauth_verifier'] = $oauth_verifier;
\r
116 $request = $this->oAuthRequest($this->accessTokenURL(), 'GET', $parameters);
\r
117 $token = OAuthUtil::parse_parameters($request);
\r
118 $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
\r
123 * One time exchange of username and password for access token and secret.
\r
125 * @returns array("oauth_token" => "the-access-token",
\r
126 * "oauth_token_secret" => "the-access-secret",
\r
127 * "user_id" => "9436992",
\r
128 * "screen_name" => "abraham",
\r
129 * "x_auth_expires" => "0")
\r
131 function getXAuthToken($username, $password) {
\r
132 $parameters = array();
\r
133 $parameters['x_auth_username'] = $username;
\r
134 $parameters['x_auth_password'] = $password;
\r
135 $parameters['x_auth_mode'] = 'client_auth';
\r
136 $request = $this->oAuthRequest($this->accessTokenURL(), 'POST', $parameters);
\r
137 $token = OAuthUtil::parse_parameters($request);
\r
138 $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
\r
143 * GET wrapper for oAuthRequest.
\r
145 function get($url, $parameters = array()) {
\r
146 $response = $this->oAuthRequest($url, 'GET', $parameters);
\r
147 if ($this->format === 'json' && $this->decode_json) {
\r
148 return json_decode($response);
\r
154 * POST wrapper for oAuthRequest.
\r
156 function post($url, $parameters = array()) {
\r
157 $response = $this->oAuthRequest($url, 'POST', $parameters);
\r
158 if ($this->format === 'json' && $this->decode_json) {
\r
159 return json_decode($response);
\r
165 * DELETE wrapper for oAuthReqeust.
\r
167 function delete($url, $parameters = array()) {
\r
168 $response = $this->oAuthRequest($url, 'DELETE', $parameters);
\r
169 if ($this->format === 'json' && $this->decode_json) {
\r
170 return json_decode($response);
\r
176 * Format and sign an OAuth / API request
\r
178 function oAuthRequest($url, $method, $parameters) {
\r
179 if (strrpos($url, 'https://') !== 0 && strrpos($url, 'http://') !== 0) {
\r
180 $url = "{$this->host}{$url}";
\r
182 $request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $parameters);
\r
183 $request->sign_request($this->sha1_method, $this->consumer, $this->token);
\r
186 return $this->http($request->to_url(), 'GET');
\r
188 return $this->http($request->get_normalized_http_url(), $method, $request->to_postdata());
\r
193 * Make an HTTP request
\r
195 * @return API results
\r
197 function http($url, $method, $postfields = NULL) {
\r
198 $this->http_info = array();
\r
200 /* Curl settings */
\r
201 curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent);
\r
202 curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);
\r
203 curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);
\r
204 curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
\r
205 curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:'));
\r
206 curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);
\r
207 curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
\r
208 curl_setopt($ci, CURLOPT_HEADER, FALSE);
\r
212 curl_setopt($ci, CURLOPT_POST, TRUE);
\r
213 if (!empty($postfields)) {
\r
214 curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
\r
218 curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
\r
219 if (!empty($postfields)) {
\r
220 $url = "{$url}?{$postfields}";
\r
224 curl_setopt($ci, CURLOPT_URL, $url);
\r
225 $response = curl_exec($ci);
\r
226 $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
\r
227 $this->http_info = array_merge($this->http_info, curl_getinfo($ci));
\r
234 * Get the header info to store.
\r
236 function getHeader($ch, $header) {
\r
237 $i = strpos($header, ':');
\r
239 $key = str_replace('-', '_', strtolower(substr($header, 0, $i)));
\r
240 $value = trim(substr($header, $i + 2));
\r
241 $this->http_header[$key] = $value;
\r
243 return strlen($header);
\r