PageRenderTime 26ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/Website/OAuth/doubanOAuth.php

http://onlinedict.googlecode.com/
PHP | 159 lines | 79 code | 15 blank | 65 comment | 11 complexity | b6c58cdc5327a27afccc8a9517c71356 MD5 | raw file
  1. <?php
  2. /*
  3. * Abraham Williams (abraham@abrah.am) http://abrah.am
  4. *
  5. * Basic lib to work with Douban's OAuth beta. This is untested and should not
  6. * be used in production code. Douban's beta could change at anytime.
  7. *
  8. * Code based on:
  9. * Fire Eagle code - http://github.com/myelin/fireeagle-php-lib
  10. * twitterlibphp - http://github.com/poseurtech/twitterlibphp
  11. */
  12. /* Load OAuth lib. You can find it at http://oauth.net */
  13. //require_once('OAuth.php');
  14. /**
  15. * Douban OAuth class
  16. */
  17. class DoubanOAuth {/*{{{*/
  18. /* Contains the last HTTP status code returned */
  19. private $http_status;
  20. /* Contains the last API call */
  21. private $last_api_call;
  22. /* Set up the API root URL */
  23. public static $TO_API_ROOT = "http://www.douban.com/service";
  24. /**
  25. * Set API URLS
  26. */
  27. function requestTokenURL() { return self::$TO_API_ROOT.'/auth/request_token'; }
  28. function authorizeURL() { return self::$TO_API_ROOT.'/auth/authorize'; }
  29. function authenticateURL() { return self::$TO_API_ROOT.'/auth/authenticate'; }
  30. function accessTokenURL() { return self::$TO_API_ROOT.'/auth/access_token'; }
  31. /**
  32. * Debug helpers
  33. */
  34. function lastStatusCode() { return $this->http_status; }
  35. function lastAPICall() { return $this->last_api_call; }
  36. /**
  37. * construct DoubanOAuth object
  38. */
  39. function __construct($consumer_key, $consumer_secret, $oauth_token = NULL, $oauth_token_secret = NULL) {/*{{{*/
  40. $this->sha1_method = new OAuthSignatureMethod_HMAC_SHA1();
  41. $this->consumer = new OAuthConsumer($consumer_key, $consumer_secret);
  42. if (!empty($oauth_token) || !empty($oauth_token_secret)) {
  43. $this->token = new OAuthConsumer($oauth_token, $oauth_token_secret);
  44. } else {
  45. $this->token = NULL;
  46. }
  47. }/*}}}*/
  48. /**
  49. * Get a request_token from Douban
  50. *
  51. * @returns a key/value array containing oauth_token and oauth_token_secret
  52. */
  53. function getRequestToken() {/*{{{*/
  54. $r = $this->oAuthRequest($this->requestTokenURL());
  55. $token = $this->oAuthParseResponse($r);
  56. $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
  57. return $token;
  58. }/*}}}*/
  59. /**
  60. * Parse a URL-encoded OAuth response
  61. *
  62. * @return a key/value array
  63. */
  64. function oAuthParseResponse($responseString) {
  65. $r = array();
  66. foreach (explode('&', $responseString) as $param) {
  67. $pair = explode('=', $param, 2);
  68. if (count($pair) != 2) continue;
  69. $r[urldecode($pair[0])] = urldecode($pair[1]);
  70. }
  71. return $r;
  72. }
  73. /**
  74. * Get the authorize URL
  75. *
  76. * @returns a string
  77. */
  78. function getAuthorizeURL($token) {/*{{{*/
  79. if (is_array($token)) $token = $token['oauth_token'];
  80. return $this->authorizeURL() . '?oauth_token=' . $token;
  81. }/*}}}*/
  82. /**
  83. * Get the authenticate URL
  84. *
  85. * @returns a string
  86. */
  87. function getAuthenticateURL($token) {/*{{{*/
  88. if (is_array($token)) $token = $token['oauth_token'];
  89. return $this->authenticateURL() . '?oauth_token=' . $token;
  90. }/*}}}*/
  91. /**
  92. * Exchange the request token and secret for an access token and
  93. * secret, to sign API calls.
  94. *
  95. * @returns array("oauth_token" => the access token,
  96. * "oauth_token_secret" => the access secret)
  97. */
  98. function getAccessToken($token = NULL) {/*{{{*/
  99. $r = $this->oAuthRequest($this->accessTokenURL());
  100. $token = $this->oAuthParseResponse($r);
  101. $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
  102. return $token;
  103. }/*}}}*/
  104. /**
  105. * Format and sign an OAuth / API request
  106. */
  107. function oAuthRequest($url, $args = array(), $method = NULL) {/*{{{*/
  108. if (empty($method)) $method = empty($args) ? "GET" : "POST";
  109. $req = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $args);
  110. $req->sign_request($this->sha1_method, $this->consumer, $this->token);
  111. switch ($method) {
  112. case 'GET': return $this->http($req->to_url());
  113. case 'POST':
  114. echo $req->to_postdata();
  115. return $this->http($req->get_normalized_http_url(), $req->to_postdata());
  116. }
  117. }/*}}}*/
  118. /**
  119. * Make an HTTP request
  120. *
  121. * @return API results
  122. */
  123. function http($url, $post_data = null) {/*{{{*/
  124. $ch = curl_init();
  125. if (defined("CURL_CA_BUNDLE_PATH")) curl_setopt($ch, CURLOPT_CAINFO, CURL_CA_BUNDLE_PATH);
  126. curl_setopt($ch, CURLOPT_URL, $url);
  127. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
  128. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  129. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  130. //////////////////////////////////////////////////
  131. ///// Set to 1 to verify Douban's SSL Cert //////
  132. //////////////////////////////////////////////////
  133. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  134. if (isset($post_data)) {
  135. curl_setopt($ch, CURLOPT_POST, 1);
  136. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  137. }
  138. $response = curl_exec($ch);
  139. $this->http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  140. $this->last_api_call = $url;
  141. curl_close ($ch);
  142. return $response;
  143. }/*}}}*/
  144. }/*}}}*/