PageRenderTime 41ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/plugin/box-api/class.curl.php

https://bitbucket.org/chamilo/chamilo-ext-repo-box-dev/
PHP | 361 lines | 128 code | 50 blank | 183 comment | 11 complexity | 46b4486d8c7d16fedfd68536df5f4e63 MD5 | raw file
  1. <?php
  2. /**
  3. * @author Dick Munroe (munroe@csworks.com)
  4. * @copyright copyright @ 2004, Dick Munroe, released under the GPL.
  5. *
  6. * The cURL class is a thin wrapper around the procedural interface
  7. * to cURL provided by PHP. I use it mostly as a base class for
  8. * web services whose low level interface is, literally, web pages.
  9. *
  10. * There are a few differences (value added, I guess) between the interface
  11. * provided by this class and the procedural cURL interface. Most
  12. * noticable are:
  13. *
  14. * 1. The curl::exec function (when returning data to the caller rather
  15. * than simply outputing it) always parses the HTTP header and returns
  16. * only the body portion of the reqeust. The header is available via
  17. * the curl::getHeader method.
  18. * 2. The status of the last curl::exec is always maintained. It is
  19. * available via the curl::getStatus method. In addition to the information
  20. * returned by curl_getinfo, that of curl_error and curl_errno is folded
  21. * in as well.
  22. *
  23. * @example ./example.class.curl.php
  24. */
  25. //
  26. // Edit History:
  27. //
  28. // Dick Munroe munroe@csworks.com 30-Nov-2004
  29. // Initial Version Created.
  30. //
  31. // Dick Munroe munroe@csworks.com 01-Dec-2004
  32. // Forgot to check for cURL actually being in this instance of PHP.
  33. //
  34. class curl
  35. {
  36. /**
  37. * The mapping to caseless header names.
  38. *
  39. * @access private
  40. * @var array
  41. */
  42. var $m_caseless;
  43. /**
  44. * The handle for the current curl session.
  45. *
  46. * @access private
  47. * @var resource
  48. */
  49. var $m_handle;
  50. /**
  51. * The parsed contents of the HTTP header if one happened in the
  52. * message. All repeated elements appear as arrays.
  53. *
  54. * The headers are stored as an associative array, the key of which
  55. * is the name of the header, e.g., Set-Cookie, and the values of which
  56. * are the bodies of the header in the order in which they occurred.
  57. *
  58. * Some headers can be repeated in a single header, e.g., Set-Cookie and
  59. * pragma, so each type of header has an array containing one or more
  60. * headers of the same type.
  61. *
  62. * The names of the headers can, potentially, vary in spelling from
  63. * server to server and client to client. No attempt to regulate this
  64. * is made, i.e., the curl class does not force all headers to lower
  65. * or upper class, but it DOES collect all headers of the same type
  66. * under the spelling of the type of header used by the FIRST header
  67. * of that type.
  68. *
  69. * For example, two headers:
  70. *
  71. * 1. Set-Cookie: ...
  72. * 2. set-cookie: ...
  73. *
  74. * Would appear as $this->m_header['Set-Cookie'][0] and ...[1]
  75. *
  76. * @access private
  77. * @var mixed
  78. */
  79. var $m_header;
  80. /**
  81. * Current setting of the curl options.
  82. *
  83. * @access private
  84. * @var mixed
  85. */
  86. var $m_options;
  87. /**
  88. * Status information for the last executed http request. Includes the errno and error
  89. * in addition to the information returned by curl_getinfo.
  90. *
  91. * The keys defined are those returned by curl_getinfo with two additional
  92. * ones specified, 'error' which is the value of curl_error and 'errno' which
  93. * is the value of curl_errno.
  94. *
  95. * @link http://www.php.net/curl_getinfo
  96. * @link http://www.php.net/curl_errno
  97. * @link http://www.php.net/curl_error
  98. * @access private
  99. * @var mixed
  100. */
  101. var $m_status;
  102. /**
  103. * curl class constructor
  104. *
  105. * Initializes the curl class for it's default behavior:
  106. * o no HTTP headers.
  107. * o return the transfer as a string.
  108. * o URL to access.
  109. * By default, the curl class will simply read the URL provided
  110. * in the constructor.
  111. *
  112. * @link http://www.php.net/curl_init
  113. * @param string $theURL [optional] the URL to be accessed by this instance of the class.
  114. */
  115. function curl($theURL = null)
  116. {
  117. if (! function_exists('curl_init'))
  118. {
  119. trigger_error('PHP was not built with --with-curl, rebuild PHP to use the curl class.', E_USER_ERROR);
  120. }
  121. $this->m_handle = curl_init();
  122. $this->m_caseless = null;
  123. $this->m_header = null;
  124. $this->m_options = null;
  125. $this->m_status = null;
  126. if (! empty($theURL))
  127. {
  128. $this->setopt(CURLOPT_URL, $theURL);
  129. }
  130. $this->setopt(CURLOPT_HEADER, false);
  131. $this->setopt(CURLOPT_RETURNTRANSFER, true);
  132. }
  133. /**
  134. * Free the resources associated with the curl session.
  135. *
  136. * @link http://www.php.net/curl_close
  137. */
  138. function close()
  139. {
  140. curl_close($this->m_handle);
  141. $this->m_handle = null;
  142. }
  143. /**
  144. * Execute the curl request and return the result.
  145. *
  146. * @link http://www.php.net/curl_exec
  147. * @link http://www.php.net/curl_getinfo
  148. * @link http://www.php.net/curl_errno
  149. * @link http://www.php.net/curl_error
  150. * @return string The contents of the page (or other interaction as defined by the
  151. * settings of the various curl options).
  152. */
  153. function exec()
  154. {
  155. $theReturnValue = curl_exec($this->m_handle);
  156. $this->m_status = curl_getinfo($this->m_handle);
  157. $this->m_status['errno'] = curl_errno($this->m_handle);
  158. $this->m_status['error'] = curl_error($this->m_handle);
  159. //
  160. // Parse out the http header (if any).
  161. //
  162. $this->m_header = null;
  163. if ($this->getOption(CURLOPT_HEADER))
  164. {
  165. $theArray = preg_split("/(\r\n){2,2}/", $theReturnValue, 2);
  166. $this->parseHeader($theArray[0]);
  167. return $theArray[1];
  168. }
  169. return $theReturnValue;
  170. }
  171. /**
  172. * Returns the parsed http header.
  173. *
  174. * @param string $theHeader [optional] the name of the header to be returned.
  175. * The name of the header is case insensitive. If
  176. * the header name is omitted the parsed header is
  177. * returned. If the requested header doesn't exist
  178. * false is returned.
  179. * @returns mixed
  180. */
  181. function getHeader($theHeader = null)
  182. {
  183. if (empty($theHeader))
  184. {
  185. return $this->m_header;
  186. }
  187. else
  188. {
  189. $theHeader = strtoupper($theHeader);
  190. if (isset($this->m_caseless[$theHeader]))
  191. {
  192. return $this->m_header[$this->m_caseless[$theHeader]];
  193. }
  194. else
  195. {
  196. return false;
  197. }
  198. }
  199. }
  200. /**
  201. * Returns the current setting of the request option. If no
  202. * option has been set, it return null.
  203. *
  204. * @param integer the requested CURLOPT.
  205. * @returns mixed
  206. */
  207. function getOption($theOption)
  208. {
  209. if (isset($this->m_options[$theOption]))
  210. {
  211. return $this->m_options[$theOption];
  212. }
  213. return null;
  214. }
  215. /**
  216. * Did the last curl exec operation have an error?
  217. *
  218. * @return mixed The error message associated with the error if an error
  219. * occurred, false otherwise.
  220. */
  221. function hasError()
  222. {
  223. if (isset($this->m_status['error']))
  224. {
  225. return (empty($this->m_status['error']) ? false : $this->m_status['error']);
  226. }
  227. else
  228. {
  229. return false;
  230. }
  231. }
  232. /**
  233. * Parse an HTTP header.
  234. *
  235. * As a side effect it stores the parsed header in the
  236. * m_header instance variable. The header is stored as
  237. * an associative array and the case of the headers
  238. * as provided by the server is preserved and all
  239. * repeated headers (pragma, set-cookie, etc) are grouped
  240. * with the first spelling for that header
  241. * that is seen.
  242. *
  243. * All headers are stored as if they COULD be repeated, so
  244. * the headers are really stored as an array of arrays.
  245. *
  246. * @param string $theHeader The HTTP data header.
  247. */
  248. function parseHeader($theHeader)
  249. {
  250. $this->m_caseless = array();
  251. $theArray = preg_split("/(\r\n)+/", $theHeader);
  252. //
  253. // Ditch the HTTP status line.
  254. //
  255. if (preg_match('/^HTTP/', $theArray[0]))
  256. {
  257. $theArray = array_slice($theArray, 1);
  258. }
  259. foreach ($theArray as $theHeaderString)
  260. {
  261. $theHeaderStringArray = preg_split("/\s*:\s*/", $theHeaderString, 2);
  262. $theCaselessTag = strtoupper($theHeaderStringArray[0]);
  263. if (! isset($this->m_caseless[$theCaselessTag]))
  264. {
  265. $this->m_caseless[$theCaselessTag] = $theHeaderStringArray[0];
  266. }
  267. $this->m_header[$this->m_caseless[$theCaselessTag]][] = $theHeaderStringArray[1];
  268. }
  269. }
  270. /**
  271. * Return the status information of the last curl request.
  272. *
  273. * @param string $theField [optional] the particular portion
  274. * of the status information desired.
  275. * If omitted the array of status
  276. * information is returned. If a non-existant
  277. * status field is requested, false is returned.
  278. * @returns mixed
  279. */
  280. function getStatus($theField = null)
  281. {
  282. if (empty($theField))
  283. {
  284. return $this->m_status;
  285. }
  286. else
  287. {
  288. if (isset($this->m_status[$theField]))
  289. {
  290. return $this->m_status[$theField];
  291. }
  292. else
  293. {
  294. return false;
  295. }
  296. }
  297. }
  298. /**
  299. * Set a curl option.
  300. *
  301. * @link http://www.php.net/curl_setopt
  302. * @param mixed $theOption One of the valid CURLOPT defines.
  303. * @param mixed $theValue the value of the curl option.
  304. */
  305. function setopt($theOption, $theValue)
  306. {
  307. curl_setopt($this->m_handle, $theOption, $theValue);
  308. $this->m_options[$theOption] = $theValue;
  309. }
  310. }
  311. ?>