PageRenderTime 26ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/curl.php

https://github.com/theshock/curl
PHP | 515 lines | 250 code | 57 blank | 208 comment | 23 complexity | fb04accc083bdabe5c2be2b2f8383fd6 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. * @author Fabian Grassl
  10. **/
  11. class Curl
  12. {
  13. /**
  14. * The file to read and write cookies to for requests
  15. *
  16. * @var string
  17. **/
  18. protected $cookie_file = null;
  19. /**
  20. * Determines whether or not requests should follow redirects
  21. *
  22. * @var boolean
  23. **/
  24. protected $follow_redirects = true;
  25. /**
  26. * An associative array of headers to send along with requests
  27. *
  28. * @var array
  29. **/
  30. protected $headers = array();
  31. /**
  32. * An associative array of CURLOPT options to send along with requests
  33. *
  34. * @var array
  35. **/
  36. protected $options = array();
  37. /**
  38. * The referer header to send along with requests
  39. *
  40. * @var string
  41. **/
  42. protected $referer = null;
  43. /**
  44. * The user agent to send along with requests
  45. *
  46. * @var string
  47. **/
  48. protected $user_agent = null;
  49. /**
  50. * Stores an error string for the last request if one occurred
  51. *
  52. * @var string
  53. * @access protected
  54. **/
  55. protected $error = '';
  56. /**
  57. * Whether to validate SSL certificates
  58. *
  59. * @var boolean
  60. * @access protected
  61. **/
  62. protected $validate_ssl = false;
  63. /**
  64. * Stores resource handle for the current CURL request
  65. *
  66. * @var resource
  67. * @access protected
  68. **/
  69. protected $request = null;
  70. /**
  71. * Initializes a Curl object
  72. *
  73. * Sets the $cookie_file to "curl_cookie.txt" in the current directory
  74. * Also sets the $user_agent to $_SERVER['HTTP_USER_AGENT'] if it exists, 'Curl/PHP '.PHP_VERSION.' (http://github.com/shuber/curl)' otherwise
  75. **/
  76. public function __construct()
  77. {
  78. $this->user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'Curl/PHP '.PHP_VERSION.' (http://github.com/shuber/curl)';
  79. }
  80. /**
  81. * Weather to validate ssl certificates
  82. *
  83. * @param bool $val whether to validate SSL certificates
  84. * @return void
  85. **/
  86. public function setValidateSsl($val)
  87. {
  88. return $this->validate_ssl = $val;
  89. }
  90. /**
  91. * Get the user agent to send along with requests
  92. **/
  93. public function getUserAgent()
  94. {
  95. return $this->user_agent;
  96. }
  97. /**
  98. * Set the user agent to send along with requests
  99. **/
  100. public function setUserAgent($user_agent)
  101. {
  102. $this->user_agent = $user_agent;
  103. }
  104. /**
  105. * Get the referer header to send along with requests
  106. **/
  107. public function getReferer()
  108. {
  109. return $this->referer;
  110. }
  111. /**
  112. * Set the referer header to send along with requests
  113. **/
  114. public function setReferer($referer)
  115. {
  116. $this->referer = $referer;
  117. }
  118. /**
  119. * Get HTTP-Headers as associative array
  120. **/
  121. public function getHeaders()
  122. {
  123. return $this->headers;
  124. }
  125. /**
  126. * Set HTTP-Headers as associative array
  127. **/
  128. public function setHeaders($headers)
  129. {
  130. $this->headers = $headers;
  131. }
  132. /**
  133. * Set HTTP-Header value
  134. **/
  135. public function setHeader($name, $value)
  136. {
  137. $this->headers[$name] = $value;
  138. }
  139. /**
  140. * Get an associative array of CURLOPT options
  141. **/
  142. public function getOptions()
  143. {
  144. return $this->options;
  145. }
  146. /**
  147. * Set an associative array of CURLOPT options
  148. **/
  149. public function setOptions($options)
  150. {
  151. foreach ($options as $name => $value)
  152. {
  153. $this->setOption($name, $value);
  154. }
  155. }
  156. /**
  157. * Set a CURLOPT option
  158. **/
  159. public function setOption($name, $value)
  160. {
  161. if (is_string($name))
  162. {
  163. $name = constant('CURLOPT_'.str_replace('CURLOPT_', '', strtoupper($name)));
  164. }
  165. $this->options[$name] = $value;
  166. }
  167. /**
  168. * Set whether or not requests should follow redirects
  169. *
  170. * @param bool $follow_redirects
  171. * @return void
  172. **/
  173. public function setFollowRedirects($follow_redirects)
  174. {
  175. $this->follow_redirects = $follow_redirects;
  176. }
  177. /**
  178. * Get whether or not requests should follow redirects
  179. *
  180. * @return bool
  181. **/
  182. public function getFollowRedirects()
  183. {
  184. return $this->follow_redirects;
  185. }
  186. /**
  187. * Set the file to read and write cookies to for requests
  188. *
  189. * @param string|bool $cookie_file path string or true (default location) | false (no cookie file)
  190. * @return void
  191. **/
  192. public function setCookieFile($cookie_file = null)
  193. {
  194. if ($cookie_file === true)
  195. {
  196. $this->cookie_file = dirname(__FILE__).DIRECTORY_SEPARATOR.'curl_cookie.txt';
  197. }
  198. elseif (empty($cookie_file))
  199. {
  200. $this->cookie_file = null;
  201. }
  202. else
  203. {
  204. $this->cookie_file = $cookie_file;
  205. }
  206. }
  207. /**
  208. * Get the file to read and write cookies to for requests
  209. *
  210. * @return string file-location
  211. **/
  212. public function getCookieFile()
  213. {
  214. return $this->cookie_file;
  215. }
  216. /**
  217. * Makes an HTTP DELETE request to the specified $url with an optional array or string of $vars
  218. *
  219. * Returns a CurlResponse object if the request was successful, false otherwise
  220. *
  221. * @param string $url
  222. * @param array|string $vars
  223. * @return CurlResponse object
  224. **/
  225. public function delete($url, $vars = array())
  226. {
  227. return $this->request('DELETE', $this->create_get_url($url, $vars));
  228. }
  229. /**
  230. * Returns the error string of the current request if one occurred
  231. *
  232. * @return string
  233. **/
  234. public function error()
  235. {
  236. return $this->error;
  237. }
  238. /**
  239. * Makes an HTTP GET request to the specified $url with an optional array or string of $vars
  240. *
  241. * Returns a CurlResponse object if the request was successful, false otherwise
  242. *
  243. * @param string $url
  244. * @param array|string $vars
  245. * @return CurlResponse
  246. **/
  247. public function get($url, $vars = array())
  248. {
  249. return $this->request('GET', $this->create_get_url($url, $vars));
  250. }
  251. /**
  252. * Modify the given $url with an optional array or string of $vars
  253. *
  254. * Returns the modified $url string
  255. *
  256. * @param string $url
  257. * @param array|string $vars
  258. * @return string
  259. **/
  260. protected function create_get_url($url, $vars = array())
  261. {
  262. if (!empty($vars))
  263. {
  264. $url .= (stripos($url, '?') !== false) ? '&' : '?';
  265. $url .= (is_string($vars)) ? $vars : http_build_query($vars, '', '&');
  266. }
  267. return $url;
  268. }
  269. /**
  270. * Makes an HTTP HEAD request to the specified $url with an optional array or string of $vars
  271. *
  272. * Returns a CurlResponse object if the request was successful, false otherwise
  273. *
  274. * @param string $url
  275. * @param array|string $vars
  276. * @return CurlResponse
  277. **/
  278. public function head($url, $vars = array())
  279. {
  280. return $this->request('HEAD', $this->create_get_url($url, $vars));
  281. }
  282. /**
  283. * Makes an HTTP POST request to the specified $url with an optional array or string of $vars
  284. *
  285. * @param string $url
  286. * @param array|string $vars
  287. * @return CurlResponse|boolean
  288. **/
  289. public function post($url, $vars)
  290. {
  291. return $this->request('POST', $url, $vars);
  292. }
  293. /**
  294. * Makes an HTTP PUT request to the specified $url with an optional array or string of $vars
  295. *
  296. * Returns a CurlResponse object if the request was successful, false otherwise
  297. *
  298. * @param string $url
  299. * @param CurlPutData|string $put_data
  300. * @param array|string $vars
  301. * @return CurlResponse|boolean
  302. **/
  303. public function put($url, $put_data, $vars = array())
  304. {
  305. return $this->request('PUT', $this->create_get_url($url, $vars), array(), $put_data);
  306. }
  307. /**
  308. * Makes an HTTP request of the specified $method to a $url with an optional array or string of $vars
  309. *
  310. * Returns a CurlResponse object if the request was successful, false otherwise
  311. *
  312. * @param string $method
  313. * @param string $url
  314. * @param array|string $post_vars
  315. * @param CurlPutData|string $put_data
  316. * @return CurlResponse|boolean
  317. **/
  318. public function request($method, $url, $post_vars = array(), $put_data = null)
  319. {
  320. if (null !== $put_data && is_string($put_data))
  321. {
  322. $put_data = CurlPutData::fromString($put_data);
  323. }
  324. $this->error = '';
  325. $this->request = curl_init();
  326. if (is_array($post_vars))
  327. {
  328. $post_vars = http_build_query($post_vars, '', '&');
  329. }
  330. if (is_array($put_data))
  331. {
  332. $put_data = http_build_query($put_data, '', '&');
  333. }
  334. $this->setRequestOptions($url, $method, $post_vars, $put_data);
  335. $this->setRequestHeaders();
  336. $response = curl_exec($this->request);
  337. if ($response)
  338. {
  339. $response = new CurlResponse($response);
  340. }
  341. else
  342. {
  343. $this->error = curl_errno($this->request).' - '.curl_error($this->request);
  344. }
  345. curl_close($this->request);
  346. return $response;
  347. }
  348. /**
  349. * Formats and adds custom headers to the current request
  350. *
  351. * @return void
  352. * @access protected
  353. **/
  354. protected function setRequestHeaders()
  355. {
  356. $headers = array();
  357. foreach ($this->headers as $key => $value)
  358. {
  359. $headers[] = $key.': '.$value;
  360. }
  361. curl_setopt($this->request, CURLOPT_HTTPHEADER, $headers);
  362. }
  363. /**
  364. * Sets the CURLOPT options for the current request
  365. *
  366. * @param string $url
  367. * @param string $method
  368. * @param string $vars
  369. * @param string $put_data
  370. * @return void
  371. * @access protected
  372. **/
  373. protected function setRequestOptions($url, $method, $vars, $put_data)
  374. {
  375. $purl = parse_url($url);
  376. if ($purl['scheme'] == 'https')
  377. {
  378. curl_setopt($this->request, CURLOPT_PORT , empty($purl['port'])?443:$purl['port']);
  379. if ($this->validate_ssl)
  380. {
  381. curl_setopt($this->request,CURLOPT_SSL_VERIFYPEER, true);
  382. curl_setopt($this->request, CURLOPT_CAINFO, dirname(__FILE__).'/cacert.pem');
  383. }
  384. else
  385. {
  386. curl_setopt($this->request, CURLOPT_SSL_VERIFYPEER, false);
  387. curl_setopt($this->request, CURLOPT_SSL_VERIFYHOST, 2);
  388. }
  389. }
  390. $method = strtoupper($method);
  391. switch ($method)
  392. {
  393. case 'HEAD':
  394. curl_setopt($this->request, CURLOPT_NOBODY, true);
  395. break;
  396. case 'GET':
  397. curl_setopt($this->request, CURLOPT_HTTPGET, true);
  398. break;
  399. case 'POST':
  400. curl_setopt($this->request, CURLOPT_POST, true);
  401. break;
  402. case 'PUT':
  403. curl_setopt($this->request, CURLOPT_PUT, true);
  404. break;
  405. default:
  406. curl_setopt($this->request, CURLOPT_CUSTOMREQUEST, $method);
  407. }
  408. curl_setopt($this->request, CURLOPT_URL, $url);
  409. if (!empty($vars))
  410. {
  411. if ('POST' != $method)
  412. {
  413. throw new InvalidArgumentException('POST-vars may only be set for a POST-Request.');
  414. }
  415. curl_setopt($this->request, CURLOPT_POSTFIELDS, $vars);
  416. }
  417. elseif ('POST' == $method)
  418. {
  419. throw new InvalidArgumentException('POST-vars must be set for a POST-Request.');
  420. }
  421. if (null !== $put_data)
  422. {
  423. if ('PUT' != $method)
  424. {
  425. throw new InvalidArgumentException('PUT-data may only be set for a PUT-Request.');
  426. }
  427. curl_setopt($this->request, CURLOPT_INFILE, $put_data->getResource());
  428. curl_setopt($this->request, CURLOPT_INFILESIZE, $put_data->getResourceSize());
  429. }
  430. elseif ('PUT' == $method)
  431. {
  432. throw new InvalidArgumentException('PUT-data must be set for a PUT-Request.');
  433. }
  434. # Set some default CURL options
  435. curl_setopt($this->request, CURLOPT_HEADER, true);
  436. curl_setopt($this->request, CURLOPT_RETURNTRANSFER, true);
  437. curl_setopt($this->request, CURLOPT_USERAGENT, $this->user_agent);
  438. curl_setopt($this->request, CURLOPT_TIMEOUT, 30);
  439. if ($this->cookie_file)
  440. {
  441. curl_setopt($this->request, CURLOPT_COOKIEFILE, $this->cookie_file);
  442. curl_setopt($this->request, CURLOPT_COOKIEJAR, $this->cookie_file);
  443. }
  444. if ($this->follow_redirects)
  445. {
  446. curl_setopt($this->request, CURLOPT_FOLLOWLOCATION, true);
  447. }
  448. if ($this->referer)
  449. {
  450. curl_setopt($this->request, CURLOPT_REFERER, $this->referer);
  451. }
  452. # Set any custom CURL options
  453. foreach ($this->options as $option => $value)
  454. {
  455. curl_setopt($this->request, $option, $value);
  456. }
  457. }
  458. }