/classes/rest/curl.php

https://github.com/magicmarkker/core · PHP · 288 lines · 133 code · 52 blank · 103 comment · 15 complexity · 182cd9fe299eda9bf140c88b7b6a665f MD5 · raw file

  1. <?php
  2. /**
  3. * Part of the Fuel framework.
  4. *
  5. * @package Fuel
  6. * @version 1.0
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2011 Fuel Development Team
  10. * @link http://fuelphp.com
  11. */
  12. namespace Fuel\Core;
  13. // ------------------------------------------------------------------------
  14. /**
  15. * Curl Class
  16. *
  17. * Generic driver for cURL requests. Requires libcurl to be available!
  18. *
  19. * @package Fuel
  20. * @category Core
  21. * @author Harro Verton
  22. * @based-on Phil Sturgeon's CodeIgniter cURL library
  23. */
  24. class Rest_Curl extends \Rest_Driver
  25. {
  26. /**
  27. * Class constructor
  28. *
  29. * @param void
  30. * @return void
  31. */
  32. public function __construct()
  33. {
  34. // check if we have libcurl available
  35. if ( ! function_exists('curl_init'))
  36. {
  37. throw new \RestException('Your PHP installation doesn\'t have cURL enabled. Rebuild PHP with --with-curl');
  38. }
  39. }
  40. /**
  41. * create a new connection
  42. *
  43. * @access public
  44. * @return void
  45. */
  46. public function create($url)
  47. {
  48. // If no a protocol in URL, assume its a local link
  49. ! preg_match('!^\w+://! i', $url) and $url = Uri::create($url);
  50. $this->url = $url;
  51. $this->session = curl_init($this->url);
  52. return $this;
  53. }
  54. // --------------------------------------------------------------------
  55. /**
  56. * authenticate to an http server
  57. *
  58. * @access public
  59. * @return void
  60. */
  61. public function http_login($username = '', $password = '', $type = 'any')
  62. {
  63. $this->option(CURLOPT_HTTPAUTH, constant('CURLAUTH_' . strtoupper($type)));
  64. $this->option(CURLOPT_USERPWD, $username . ':' . $password);
  65. return $this;
  66. }
  67. // --------------------------------------------------------------------
  68. /**
  69. * execute a request
  70. *
  71. * @access public
  72. * @return void
  73. */
  74. public function execute()
  75. {
  76. // Set two default options, and merge any extra ones in
  77. if ( ! isset($this->options[CURLOPT_TIMEOUT]))
  78. {
  79. $this->options[CURLOPT_TIMEOUT] = 30;
  80. }
  81. if ( ! isset($this->options[CURLOPT_RETURNTRANSFER]))
  82. {
  83. $this->options[CURLOPT_RETURNTRANSFER] = true;
  84. }
  85. if ( ! isset($this->options[CURLOPT_FAILONERROR]))
  86. {
  87. $this->options[CURLOPT_FAILONERROR] = true;
  88. }
  89. // Only set follow location if not running securely
  90. if ( ! ini_get('safe_mode') && !ini_get('open_basedir'))
  91. {
  92. // Ok, follow location is not set already so lets set it to true
  93. if ( ! isset($this->options[CURLOPT_FOLLOWLOCATION]))
  94. {
  95. $this->options[CURLOPT_FOLLOWLOCATION] = true;
  96. }
  97. }
  98. if ( ! empty($this->headers))
  99. {
  100. $this->option(CURLOPT_HTTPHEADER, $this->headers);
  101. }
  102. $this->options();
  103. // Execute the request & and hide all output
  104. $this->response = curl_exec($this->session);
  105. $this->info = curl_getinfo($this->session);
  106. // Request failed
  107. if ($this->response === false)
  108. {
  109. $this->error_code = curl_errno($this->session);
  110. $this->error_string = curl_error($this->session);
  111. curl_close($this->session);
  112. $this->set_defaults();
  113. return false;
  114. }
  115. else
  116. {
  117. // Request successful
  118. curl_close($this->session);
  119. $response = $this->response;
  120. $this->set_defaults();
  121. return $response;
  122. }
  123. }
  124. // --------------------------------------------------------------------
  125. /**
  126. * get request
  127. *
  128. * @access public
  129. * @return void
  130. */
  131. public function get($params = array(), array $options = array())
  132. {
  133. // If its an array (instead of a query string) then format it correctly
  134. if (is_array($params))
  135. {
  136. $params = http_build_query($params, null, '&');
  137. }
  138. // Add in the specific options provided
  139. $this->options($options);
  140. $this->http_method('get');
  141. }
  142. // --------------------------------------------------------------------
  143. /**
  144. * post request
  145. *
  146. * @access public
  147. * @return void
  148. */
  149. public function post($params = array(), array $options = array())
  150. {
  151. // If its an array (instead of a query string) then format it correctly
  152. if (is_array($params))
  153. {
  154. $params = http_build_query($params, null, '&');
  155. }
  156. // Add in the specific options provided
  157. $this->options($options);
  158. $this->http_method('post');
  159. $this->option(CURLOPT_POST, true);
  160. $this->option(CURLOPT_POSTFIELDS, $params);
  161. }
  162. // --------------------------------------------------------------------
  163. /**
  164. * put request
  165. *
  166. * @access public
  167. * @return void
  168. */
  169. public function put($params = array(), array $options = array())
  170. {
  171. // If its an array (instead of a query string) then format it correctly
  172. if (is_array($params))
  173. {
  174. $params = http_build_query($params, null, '&');
  175. }
  176. // Add in the specific options provided
  177. $this->options($options);
  178. $this->http_method('put');
  179. $this->option(CURLOPT_POSTFIELDS, $params);
  180. // Override method, I think this overrides $_POST with PUT data but... we'll see eh?
  181. $this->option(CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: PUT'));
  182. }
  183. // --------------------------------------------------------------------
  184. /**
  185. * delete request
  186. *
  187. * @access public
  188. * @return void
  189. */
  190. public function delete($params = array(), array $options = array())
  191. {
  192. // If its an array (instead of a query string) then format it correctly
  193. if (is_array($params))
  194. {
  195. $params = http_build_query($params, null, '&');
  196. }
  197. // Add in the specific options provided
  198. $this->options($options);
  199. $this->http_method('delete');
  200. $this->option(CURLOPT_POSTFIELDS, $params);
  201. }
  202. // --------------------------------------------------------------------
  203. /**
  204. * set driver options
  205. *
  206. * @access public
  207. * @return void
  208. */
  209. public function option($code, $value)
  210. {
  211. if (is_string($code) && !is_numeric($code))
  212. {
  213. $code = constant('CURLOPT_' . strtoupper($code));
  214. }
  215. $this->options[$code] = $value;
  216. return $this;
  217. }
  218. // --------------------------------------------------------------------
  219. private function http_method($method)
  220. {
  221. $this->options[CURLOPT_CUSTOMREQUEST] = strtoupper($method);
  222. return $this;
  223. }
  224. // --------------------------------------------------------------------
  225. private function options(array $options = array())
  226. {
  227. // Merge options in with the rest - done as array_merge() does not overwrite numeric keys
  228. foreach ($options as $option_code => $option_value)
  229. {
  230. $this->option($option_code, $option_value);
  231. }
  232. // Set all options provided
  233. curl_setopt_array($this->session, $this->options);
  234. return $this;
  235. }
  236. }