PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/http/HTTPClient.php

https://bitbucket.org/haps/c3s-php
PHP | 253 lines | 137 code | 61 blank | 55 comment | 8 complexity | a82c1b9267c6d6ce3ad138dabfa7cdc4 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. namespace c3s\lib\http;
  3. /**
  4. *
  5. * @author admin
  6. *
  7. */
  8. class HTTPClient {
  9. const METHOD_GET = 'GET';
  10. const METHOD_PUT = 'PUT';
  11. const METHOD_HEADER = 'HEADER';
  12. const METHOD_POST = 'POST';
  13. const METHOD_DELETE = 'DELETE';
  14. private $curl = null;
  15. public function __construct() {
  16. }
  17. /**
  18. * @var string
  19. */
  20. private $method = 'GET';
  21. /**
  22. * @return the $method
  23. */
  24. public function getMethod() {
  25. return $this->method;
  26. }
  27. /**
  28. * @param string $method
  29. */
  30. public function setMethod($method) {
  31. $this->method = $method;
  32. }
  33. private $headers = array();
  34. /**
  35. * @return the $headers
  36. */
  37. public function getHeaders() {
  38. return $this->headers;
  39. }
  40. /**
  41. * @param multitype: $headers
  42. */
  43. public function setHeaders($headers) {
  44. $this->headers = $headers;
  45. }
  46. /**
  47. * @param array|string $header
  48. */
  49. public function addHeader($header) {
  50. if (is_array($header)) {
  51. $this->headers = array_merge($this->headers, $header);
  52. } else {
  53. $this->headers[] = $header;
  54. }
  55. }
  56. /**
  57. * @var bool
  58. */
  59. private $responseHeader = false;
  60. /**
  61. * @return the $responseHeader
  62. */
  63. public function getResponseHeader() {
  64. return $this->responseHeader;
  65. }
  66. /**
  67. * @param boolean $responseHeader
  68. */
  69. public function setResponseHeader($responseHeader) {
  70. $this->responseHeader = $responseHeader?true:false;
  71. }
  72. /**
  73. * @var bool
  74. */
  75. private $autoRedirect = true;
  76. /**
  77. * @return the $autoRedirect
  78. */
  79. public function getAutoRedirect() {
  80. return $this->autoRedirect;
  81. }
  82. /**
  83. * @param boolean $autoRedirect
  84. */
  85. public function setAutoRedirect($autoRedirect) {
  86. $this->autoRedirect = $autoRedirect?true:false;
  87. }
  88. /**
  89. * @var string
  90. */
  91. private $content = '';
  92. /**
  93. * @return the $content
  94. */
  95. public function getContent() {
  96. return $this->content;
  97. }
  98. /**
  99. * @param string $content
  100. */
  101. public function setContent($content) {
  102. $this->content = $content;
  103. }
  104. private $otherOptions = array();
  105. public function setOption($curl_option, $value) {
  106. $this->otherOptions[$curl_option] = $value;
  107. //curl_setopt($this->curl, $curl_option, $value);
  108. }
  109. private $debugOutput = false;
  110. private $file = false;
  111. public function debug($filename) {
  112. $this->debugOutput = $filename;
  113. }
  114. private $cookieFile = false;
  115. public function setCookieFile($cookieFile) {
  116. $this->cookieFile = $cookieFile;
  117. }
  118. public function deleteCookieFile() {
  119. @unlink($this->cookieFile);
  120. }
  121. protected function makeCurlOptions() {
  122. $this->curl = curl_init();
  123. curl_setopt ($this->curl, CURLOPT_RETURNTRANSFER, 1);
  124. curl_setopt ($this->curl, CURLOPT_FOLLOWLOCATION, $this->autoRedirect?1:0);
  125. // NOT CHECK SSL
  126. curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, 0);
  127. curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, 0);
  128. //
  129. if ($this->headers && count($this->headers)) {
  130. curl_setopt($this->curl, CURLOPT_HTTPHEADER, $this->headers);
  131. }
  132. // clear special method headers
  133. curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, "GET");
  134. curl_setopt($this->curl, CURLOPT_POST, 0);
  135. foreach ($this->otherOptions as $curl_option => $value) {
  136. curl_setopt($this->curl, $curl_option, $value);
  137. }
  138. if ($this->cookieFile) {
  139. curl_setopt($this->curl, CURLOPT_COOKIEJAR, $this->cookieFile); //initiates cookie file if needed
  140. curl_setopt($this->curl, CURLOPT_COOKIEFILE, $this->cookieFile); // Uses cookies from previous session if exist
  141. }
  142. if ($this->debugOutput) {
  143. $this->file = fopen($this->debugOutput, 'w+');
  144. curl_setopt($this->curl, CURLOPT_HEADER, 1);
  145. curl_setopt($this->curl, CURLOPT_FILE, $this->file);
  146. curl_setopt($this->curl, CURLOPT_VERBOSE, true);
  147. //$verbose = fopen('php://temp', 'w+');
  148. curl_setopt($this->curl, CURLOPT_STDERR, $this->file);
  149. }
  150. }
  151. public function request($method, $url, $body = '', $headers = array()) {
  152. $this->method = $method;
  153. $old_headers = $this->headers;
  154. $this->addHeader($headers);
  155. $this->makeCurlOptions();
  156. if ($this->debugOutput) {
  157. fputs($this->file, is_array($body)?json_encode($body):$body);
  158. }
  159. switch ($this->method) {
  160. case self::METHOD_GET:
  161. curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, "GET");
  162. break;
  163. case self::METHOD_HEADER:
  164. curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, "HEADER");
  165. break;
  166. case self::METHOD_POST:
  167. curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, "POST");
  168. curl_setopt($this->curl, CURLOPT_POST, 1);
  169. curl_setopt($this->curl, CURLOPT_POSTFIELDS, $body);
  170. break;
  171. case self::METHOD_PUT:
  172. curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, "PUT");
  173. curl_setopt($this->curl, CURLOPT_POSTFIELDS, $body);
  174. break;
  175. case self::METHOD_DELETE:
  176. curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, "DELETE");
  177. curl_setopt($this->curl, CURLOPT_POSTFIELDS, $body);
  178. break;
  179. }
  180. curl_setopt($this->curl, CURLOPT_URL, $url);
  181. $this->content = curl_exec($this->curl);
  182. $this->headers = $old_headers;
  183. return $this->content;
  184. }
  185. public function getError() {
  186. return curl_error($this->curl);
  187. }
  188. public function getErrorCode() {
  189. return curl_errno($this->curl);
  190. }
  191. }
  192. ?>