/oauth1.0/douban/curl.php

https://github.com/xiaoshenge/snsLogin · PHP · 256 lines · 91 code · 28 blank · 137 comment · 9 complexity · 58c2f16ce4316dc51b4c6d06adc3e5f3 MD5 · raw file

  1. <?php
  2. /**
  3. * A basic CURL wrapper
  4. *
  5. * See the README for documentation/examples or http://php.net/curl for more information about the libcurl extension for PHP
  6. *
  7. * @package curl
  8. * @author Sean Huber <shuber@huberry.com>
  9. **/
  10. class Curl {
  11. /**
  12. * The file to read and write cookies to for requests
  13. *
  14. * @var string
  15. **/
  16. public $cookie_file;
  17. /**
  18. * Determines whether or not requests should follow redirects
  19. *
  20. * @var boolean
  21. **/
  22. public $follow_redirects = true;
  23. /**
  24. * An associative array of headers to send along with requests
  25. *
  26. * @var array
  27. **/
  28. public $headers = array();
  29. /**
  30. * An associative array of CURLOPT options to send along with requests
  31. *
  32. * @var array
  33. **/
  34. public $options = array();
  35. /**
  36. * The referer header to send along with requests
  37. *
  38. * @var string
  39. **/
  40. public $referer;
  41. /**
  42. * The user agent to send along with requests
  43. *
  44. * @var string
  45. **/
  46. public $user_agent;
  47. /**
  48. * Stores an error string for the last request if one occurred
  49. *
  50. * @var string
  51. * @access protected
  52. **/
  53. protected $error = '';
  54. /**
  55. * Stores resource handle for the current CURL request
  56. *
  57. * @var resource
  58. * @access protected
  59. **/
  60. protected $request;
  61. /**
  62. * Initializes a Curl object
  63. *
  64. * Sets the $cookie_file to "curl_cookie.txt" in the current directory
  65. * Also sets the $user_agent to $_SERVER['HTTP_USER_AGENT'] if it exists, 'Curl/PHP '.PHP_VERSION.' (http://github.com/shuber/curl)' otherwise
  66. **/
  67. function __construct() {
  68. $this->cookie_file = dirname(__FILE__).DIRECTORY_SEPARATOR.'curl_cookie.txt';
  69. $this->user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'Curl/PHP '.PHP_VERSION.' (http://github.com/shuber/curl)';
  70. }
  71. /**
  72. * Makes an HTTP DELETE request to the specified $url with an optional array or string of $vars
  73. *
  74. * Returns a CurlResponse object if the request was successful, false otherwise
  75. *
  76. * @param string $url
  77. * @param array|string $vars
  78. * @return CurlResponse object
  79. **/
  80. function delete($url, $vars = array()) {
  81. return $this->request('DELETE', $url, $vars);
  82. }
  83. /**
  84. * Returns the error string of the current request if one occurred
  85. *
  86. * @return string
  87. **/
  88. function error() {
  89. return $this->error;
  90. }
  91. /**
  92. * Makes an HTTP GET request to the specified $url with an optional array or string of $vars
  93. *
  94. * Returns a CurlResponse object if the request was successful, false otherwise
  95. *
  96. * @param string $url
  97. * @param array|string $vars
  98. * @return CurlResponse
  99. **/
  100. function get($url, $vars = array()) {
  101. if (!empty($vars)) {
  102. $url .= (stripos($url, '?') !== false) ? '&' : '?';
  103. $url .= (is_string($vars)) ? $vars : http_build_query($vars, '', '&');
  104. }
  105. return $this->request('GET', $url);
  106. }
  107. /**
  108. * Makes an HTTP HEAD request to the specified $url with an optional array or string of $vars
  109. *
  110. * Returns a CurlResponse object if the request was successful, false otherwise
  111. *
  112. * @param string $url
  113. * @param array|string $vars
  114. * @return CurlResponse
  115. **/
  116. function head($url, $vars = array()) {
  117. return $this->request('HEAD', $url, $vars);
  118. }
  119. /**
  120. * Makes an HTTP POST request to the specified $url with an optional array or string of $vars
  121. *
  122. * @param string $url
  123. * @param array|string $vars
  124. * @return CurlResponse|boolean
  125. **/
  126. function post($url, $vars = array()) {
  127. return $this->request('POST', $url, $vars);
  128. }
  129. /**
  130. * Makes an HTTP PUT request to the specified $url with an optional array or string of $vars
  131. *
  132. * Returns a CurlResponse object if the request was successful, false otherwise
  133. *
  134. * @param string $url
  135. * @param array|string $vars
  136. * @return CurlResponse|boolean
  137. **/
  138. function put($url, $vars = array()) {
  139. return $this->request('PUT', $url, $vars);
  140. }
  141. /**
  142. * Makes an HTTP request of the specified $method to a $url with an optional array or string of $vars
  143. *
  144. * Returns a CurlResponse object if the request was successful, false otherwise
  145. *
  146. * @param string $method
  147. * @param string $url
  148. * @param array|string $vars
  149. * @return CurlResponse|boolean
  150. **/
  151. function request($method, $url, $vars = array()) {
  152. $this->error = '';
  153. $this->request = curl_init();
  154. if (is_array($vars)) $vars = http_build_query($vars, '', '&');
  155. $this->set_request_method($method);
  156. $this->set_request_options($url, $vars);
  157. $this->set_request_headers();
  158. $response = curl_exec($this->request);
  159. if ($response) {
  160. $response = new CurlResponse($response);
  161. } else {
  162. $this->error = curl_errno($this->request).' - '.curl_error($this->request);
  163. }
  164. curl_close($this->request);
  165. return $response;
  166. }
  167. /**
  168. * Formats and adds custom headers to the current request
  169. *
  170. * @return void
  171. * @access protected
  172. **/
  173. protected function set_request_headers() {
  174. $headers = array();
  175. foreach ($this->headers as $key => $value) {
  176. $headers[] = $key.': '.$value;
  177. }
  178. curl_setopt($this->request, CURLOPT_HTTPHEADER, $headers);
  179. }
  180. /**
  181. * Set the associated CURL options for a request method
  182. *
  183. * @param string $method
  184. * @return void
  185. * @access protected
  186. **/
  187. protected function set_request_method($method) {
  188. switch (strtoupper($method)) {
  189. case 'HEAD':
  190. curl_setopt($this->request, CURLOPT_NOBODY, true);
  191. break;
  192. case 'GET':
  193. curl_setopt($this->request, CURLOPT_HTTPGET, true);
  194. break;
  195. case 'POST':
  196. curl_setopt($this->request, CURLOPT_POST, true);
  197. break;
  198. default:
  199. curl_setopt($this->request, CURLOPT_CUSTOMREQUEST, $method);
  200. }
  201. }
  202. /**
  203. * Sets the CURLOPT options for the current request
  204. *
  205. * @param string $url
  206. * @param string $vars
  207. * @return void
  208. * @access protected
  209. **/
  210. protected function set_request_options($url, $vars) {
  211. curl_setopt($this->request, CURLOPT_URL, $url);
  212. if (!empty($vars)) curl_setopt($this->request, CURLOPT_POSTFIELDS, $vars);
  213. # Set some default CURL options
  214. curl_setopt($this->request, CURLOPT_HEADER, true);
  215. curl_setopt($this->request, CURLOPT_RETURNTRANSFER, true);
  216. curl_setopt($this->request, CURLOPT_USERAGENT, $this->user_agent);
  217. if ($this->cookie_file) {
  218. curl_setopt($this->request, CURLOPT_COOKIEFILE, $this->cookie_file);
  219. curl_setopt($this->request, CURLOPT_COOKIEJAR, $this->cookie_file);
  220. }
  221. if ($this->follow_redirects) curl_setopt($this->request, CURLOPT_FOLLOWLOCATION, true);
  222. if ($this->referer) curl_setopt($this->request, CURLOPT_REFERER, $this->referer);
  223. # Set any custom CURL options
  224. foreach ($this->options as $option => $value) {
  225. curl_setopt($this->request, constant('CURLOPT_'.str_replace('CURLOPT_', '', strtoupper($option))), $value);
  226. }
  227. }
  228. }