/controlador/curl_lib.php

https://gitlab.com/asosab/ws-geo · PHP · 219 lines · 109 code · 36 blank · 74 comment · 5 complexity · 2a335dffd351a7efaf0d982e023b559e MD5 · raw file

  1. <?php
  2. class Curl_lib
  3. {
  4. private $resource = NULL; // libcurb init() resource
  5. private $config = array(); // Construction config
  6. public $header = array(); // Response Header
  7. public $body = array(); // Response Body
  8. /**
  9. * Factory Method
  10. */
  11. public static function factory($data = array())
  12. {
  13. return new self($data);
  14. }
  15. /**
  16. * Constructor
  17. */
  18. public function __construct($data = array())
  19. {
  20. $config = array(
  21. CURLOPT_HEADER => false
  22. );
  23. // Apply any passed configuration
  24. $data += $config;
  25. $this->config = $data;
  26. $this->resource = curl_init();
  27. // Apply configuration settings
  28. foreach ($this->config as $key => $value)
  29. {
  30. $this->set_opt($key, $value);
  31. }
  32. }
  33. /**
  34. * Set option
  35. *
  36. * @param String Curl option to set
  37. * @param String Value for option
  38. * @chainable
  39. */
  40. public function set_opt($key, $value)
  41. {
  42. curl_setopt($this->resource, $key, $value);
  43. return $this;
  44. }
  45. /**
  46. * Execute the curl request and return the response
  47. *
  48. * @return String Returned output from the requested resource
  49. * @throws Kohana_User_Exception
  50. */
  51. public function exec()
  52. {
  53. $ret = curl_exec($this->resource);
  54. //Wrap the error reporting in an exception
  55. if ($ret === false){
  56. $ret = "error: " . curl_error($this->resource);
  57. //throw new Exception(curl_error($this->resource));
  58. }
  59. else{
  60. return $ret;
  61. }
  62. }
  63. /**
  64. * Get Error
  65. * Returns any current error for the curl request
  66. *
  67. * @return string The error
  68. */
  69. public function get_error()
  70. {
  71. return curl_error($this->resource);
  72. }
  73. /**
  74. * Destructor
  75. */
  76. function __destruct()
  77. {
  78. curl_close($this->resource);
  79. }
  80. /**
  81. * Get
  82. * Execute an HTTP GET request using curl
  83. *
  84. * @param String url to request
  85. * @param Array additional headers to send in the request
  86. * @param Bool flag to return only the headers
  87. * @param Array Additional curl options to instantiate curl with
  88. * @return Array array of 'header' and 'body'
  89. */
  90. public static function get($url,
  91. Array $headers = array(),
  92. $headers_only = FALSE,
  93. Array $curl_options = array())
  94. {
  95. $ch = self::factory($curl_options);
  96. $ch->set_opt(CURLOPT_URL, $url)
  97. ->set_opt(CURLOPT_RETURNTRANSFER, TRUE)
  98. ->set_opt(CURLOPT_NOBODY, $headers_only)
  99. ->set_opt(CURLOPT_HTTPHEADER, array("Expect:"))
  100. ->set_opt(CURLOPT_HEADERFUNCTION, array($ch, 'read_header'))
  101. ->set_opt(CURLOPT_WRITEFUNCTION, array($ch, 'read_body'));
  102. //Set any additional headers
  103. if(!empty($headers)) $ch->set_opt(CURLOPT_HTTPHEADER, $headers);
  104. $ch->exec();
  105. return array(
  106. 'header' => $ch->header,
  107. 'body' => $ch->body
  108. );
  109. }
  110. /**
  111. * Post
  112. * Execute an HTTP POST request, posting the past parameters
  113. *
  114. * @param String url to request
  115. * @param Array past data to post to $url
  116. * @param Array additional headers to send in the request
  117. * @param Bool flag to return only the headers
  118. * @param Array Additional curl options to instantiate curl with
  119. * @return Array array of 'header' and 'body'
  120. */
  121. public static function post($url,
  122. Array $data = array(),
  123. Array $headers = array(),
  124. $headers_only = FALSE,
  125. Array $curl_options = array())
  126. {
  127. $ch = self::factory($curl_options);
  128. $ch->set_opt(CURLOPT_URL, $url)
  129. ->set_opt(CURLOPT_NOBODY, $headers_only)
  130. ->set_opt(CURLOPT_RETURNTRANSFER, TRUE)
  131. ->set_opt(CURLOPT_POST, TRUE)
  132. ->set_opt(CURLOPT_POSTFIELDS, $data)
  133. ->set_opt(CURLOPT_HTTPHEADER, array("Expect:"))
  134. ->set_opt(CURLOPT_HEADERFUNCTION, array($ch, 'read_header'))
  135. ->set_opt(CURLOPT_WRITEFUNCTION, array($ch, 'read_body'));
  136. //Set any additional headers
  137. if(!empty($headers)) $ch->set_opt(CURLOPT_HTTPHEADER, $headers);
  138. $ch->exec();
  139. return array(
  140. 'header' => $ch->header,
  141. 'body' => $ch->body
  142. );
  143. }
  144. /**
  145. * Read Header
  146. * A private method to be used by libcurl when reading header.
  147. *
  148. * @param String Curl Binded Resource
  149. * @param String Header String
  150. * @return Integer Header String Length
  151. */
  152. private function read_header($ch, $string)
  153. {
  154. $length = strlen($string);
  155. // Trim Header String
  156. $string = trim($string);
  157. // If not empty, push into header array
  158. if (!empty($string))
  159. {
  160. array_push($this->header, $string);
  161. }
  162. return $length;
  163. }
  164. /**
  165. * Read Body
  166. * A private method to be used by libcurl when reading body content.
  167. *
  168. * @param String Curl Binded Resource
  169. * @param String Body String
  170. * @return Integer Body String Length
  171. */
  172. private function read_body($ch, $string)
  173. {
  174. $length = strlen($string);
  175. // If not empty, push into body array
  176. if (!empty($string))
  177. {
  178. array_push($this->body, $string);
  179. }
  180. return $length;
  181. }
  182. }
  183. ?>