/Laravel/Composer/vendor/curl/curl/src/Curl/Curl.php

https://github.com/Yi-Chuan/class · PHP · 205 lines · 169 code · 35 blank · 1 comment · 9 complexity · dcbf7c3526d68741eff8ad81629b3095 MD5 · raw file

  1. <?php
  2. namespace Curl;
  3. class Curl
  4. {
  5. // The HTTP authentication method(s) to use.
  6. const AUTH_BASIC = CURLAUTH_BASIC;
  7. const AUTH_DIGEST = CURLAUTH_DIGEST;
  8. const AUTH_GSSNEGOTIATE = CURLAUTH_GSSNEGOTIATE;
  9. const AUTH_NTLM = CURLAUTH_NTLM;
  10. const AUTH_ANY = CURLAUTH_ANY;
  11. const AUTH_ANYSAFE = CURLAUTH_ANYSAFE;
  12. const USER_AGENT = 'PHP Curl/1.1 (+https://github.com/mod-php/curl)';
  13. private $_cookies = array();
  14. private $_headers = array();
  15. public $curl;
  16. public $error = false;
  17. public $error_code = 0;
  18. public $error_message = null;
  19. public $curl_error = false;
  20. public $curl_error_code = 0;
  21. public $curl_error_message = null;
  22. public $http_error = false;
  23. public $http_status_code = 0;
  24. public $http_error_message = null;
  25. public $request_headers = null;
  26. public $response_headers = null;
  27. public $response = null;
  28. public function __construct()
  29. {
  30. if (!extension_loaded('curl')) {
  31. throw new \ErrorException('cURL library is not loaded');
  32. }
  33. $this->init();
  34. }
  35. public function get($url, $data = array())
  36. {
  37. if (count($data) > 0) {
  38. $this->setopt(CURLOPT_URL, $url . '?' . http_build_query($data));
  39. } else {
  40. $this->setopt(CURLOPT_URL, $url);
  41. }
  42. $this->setopt(CURLOPT_HTTPGET, true);
  43. $this->_exec();
  44. }
  45. public function post($url, $data = array())
  46. {
  47. $this->setopt(CURLOPT_URL, $url);
  48. $this->setopt(CURLOPT_POST, true);
  49. if (is_array($data) || is_object($data))
  50. {
  51. $data = http_build_query($data);
  52. }
  53. $this->setopt(CURLOPT_POSTFIELDS, $data);
  54. $this->_exec();
  55. }
  56. public function put($url, $data = array())
  57. {
  58. $this->setopt(CURLOPT_URL, $url . '?' . http_build_query($data));
  59. $this->setopt(CURLOPT_CUSTOMREQUEST, 'PUT');
  60. $this->_exec();
  61. }
  62. public function patch($url, $data = array())
  63. {
  64. $this->setopt(CURLOPT_URL, $url);
  65. $this->setopt(CURLOPT_CUSTOMREQUEST, 'PATCH');
  66. $this->setopt(CURLOPT_POSTFIELDS, $data);
  67. $this->_exec();
  68. }
  69. public function delete($url, $data = array())
  70. {
  71. $this->setopt(CURLOPT_URL, $url . '?' . http_build_query($data));
  72. $this->setopt(CURLOPT_CUSTOMREQUEST, 'DELETE');
  73. $this->_exec();
  74. }
  75. public function setBasicAuthentication($username, $password)
  76. {
  77. $this->setHttpAuth(self::AUTH_BASIC);
  78. $this->setopt(CURLOPT_USERPWD, $username . ':' . $password);
  79. }
  80. protected function setHttpAuth($httpauth)
  81. {
  82. $this->setOpt(CURLOPT_HTTPAUTH, $httpauth);
  83. }
  84. public function setHeader($key, $value)
  85. {
  86. $this->_headers[$key] = $key . ': ' . $value;
  87. $this->setopt(CURLOPT_HTTPHEADER, array_values($this->_headers));
  88. }
  89. public function setUserAgent($user_agent)
  90. {
  91. $this->setopt(CURLOPT_USERAGENT, $user_agent);
  92. }
  93. public function setReferrer($referrer)
  94. {
  95. $this->setopt(CURLOPT_REFERER, $referrer);
  96. }
  97. public function setCookie($key, $value)
  98. {
  99. $this->_cookies[$key] = $value;
  100. $this->setopt(CURLOPT_COOKIE, http_build_query($this->_cookies, '', '; '));
  101. }
  102. public function setOpt($option, $value)
  103. {
  104. return curl_setopt($this->curl, $option, $value);
  105. }
  106. public function verbose($on = true)
  107. {
  108. $this->setopt(CURLOPT_VERBOSE, $on);
  109. }
  110. public function close()
  111. {
  112. if (is_resource($this->curl)) {
  113. curl_close($this->curl);
  114. }
  115. }
  116. public function reset()
  117. {
  118. $this->close();
  119. $this->_cookies = array();
  120. $this->_headers = array();
  121. $this->error = false;
  122. $this->error_code = 0;
  123. $this->error_message = null;
  124. $this->curl_error = false;
  125. $this->curl_error_code = 0;
  126. $this->curl_error_message = null;
  127. $this->http_error = false;
  128. $this->http_status_code = 0;
  129. $this->http_error_message = null;
  130. $this->request_headers = null;
  131. $this->response_headers = null;
  132. $this->response = null;
  133. $this->init();
  134. }
  135. public function _exec()
  136. {
  137. $this->response = curl_exec($this->curl);
  138. $this->curl_error_code = curl_errno($this->curl);
  139. $this->curl_error_message = curl_error($this->curl);
  140. $this->curl_error = !($this->curl_error_code === 0);
  141. $this->http_status_code = curl_getinfo($this->curl, CURLINFO_HTTP_CODE);
  142. $this->http_error = in_array(floor($this->http_status_code / 100), array(4, 5));
  143. $this->error = $this->curl_error || $this->http_error;
  144. $this->error_code = $this->error ? ($this->curl_error ? $this->curl_error_code : $this->http_status_code) : 0;
  145. $this->request_headers = preg_split('/\r\n/', curl_getinfo($this->curl, CURLINFO_HEADER_OUT), null, PREG_SPLIT_NO_EMPTY);
  146. $this->response_headers = '';
  147. if (!(strpos($this->response, "\r\n\r\n") === false)) {
  148. list($response_header, $this->response) = explode("\r\n\r\n", $this->response, 2);
  149. while (strtolower(trim($response_header)) === 'http/1.1 100 continue') {
  150. list($response_header, $this->response) = explode("\r\n\r\n", $this->response, 2);
  151. }
  152. $this->response_headers = preg_split('/\r\n/', $response_header, null, PREG_SPLIT_NO_EMPTY);
  153. }
  154. $this->http_error_message = $this->error ? (isset($this->response_headers['0']) ? $this->response_headers['0'] : '') : '';
  155. $this->error_message = $this->curl_error ? $this->curl_error_message : $this->http_error_message;
  156. return $this->error_code;
  157. }
  158. public function __destruct()
  159. {
  160. $this->close();
  161. }
  162. private function init()
  163. {
  164. $this->curl = curl_init();
  165. $this->setUserAgent(self::USER_AGENT);
  166. $this->setopt(CURLINFO_HEADER_OUT, true);
  167. $this->setopt(CURLOPT_HEADER, true);
  168. $this->setopt(CURLOPT_RETURNTRANSFER, true);
  169. }
  170. }