Guest User

Untitled

a guest
Oct 21st, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. <?php
  2.  
  3. class HTTPClient {
  4. public $base;
  5. public $curl;
  6. public $format;
  7.  
  8. function __construct($base = null) {
  9. $this->base = $base;
  10. $this->curl_init();
  11. }
  12.  
  13. function absolute($url) {
  14. return preg_match('/^https?:/', $url) ? $url : $this->base . $url;
  15. }
  16.  
  17. /**
  18. * HTTP methods
  19. */
  20. function get($url, $params = array(), $format = null) {
  21. $this->format = $format;
  22.  
  23. if($params) $url .= '?' . http_build_query($params);
  24.  
  25. $this->curl_setopt_array(array(
  26. CURLOPT_URL => $this->absolute($url),
  27. CURLOPT_HTTPGET => true,
  28. ));
  29.  
  30. return $this->curl_exec();
  31. }
  32.  
  33. function post($url, $params = array(), $format = null) {
  34. $this->format = $format;
  35.  
  36. $this->curl_setopt_array(array(
  37. CURLOPT_URL => $this->absolute($url),
  38. CURLOPT_POSTFIELDS => http_build_query($params),
  39. ));
  40.  
  41. return $this->curl_exec();
  42. }
  43.  
  44. /**
  45. * cURL methods
  46. */
  47. function set_cookie_file($file = null) {
  48. if(!$file) $file = tempnam(sys_get_temp_dir(), 'cookies-');
  49.  
  50. $this->curl_setopt_array(array(
  51. CURLOPT_COOKIEJAR => $file,
  52. CURLOPT_COOKIEFILE => $file,
  53. CURLOPT_COOKIESESSION => true,
  54. ));
  55. }
  56.  
  57. function curl_setopt_array($options = array()) {
  58. curl_setopt_array($this->curl, $options);
  59. }
  60.  
  61. function curl_getinfo($field) {
  62. return curl_getinfo($this->curl, $field);
  63. }
  64.  
  65. private function curl_init() {
  66. $this->curl = curl_init();
  67.  
  68. $this->curl_setopt_array(array(
  69. CURLOPT_FOLLOWLOCATION => false,
  70. CURLOPT_RETURNTRANSFER => true,
  71. CURLOPT_HEADER => true,
  72. //CURLOPT_VERBOSE => true,
  73. ));
  74. }
  75.  
  76. function curl_exec() {
  77. return new HTTPResponse($this, curl_exec($this->curl));
  78. }
  79. }
  80.  
  81. class HTTPResponse {
  82. public $request;
  83. public $code;
  84. public $type;
  85. public $charset;
  86. public $headers;
  87. public $body;
  88. public $data;
  89. public $dom;
  90. public $xpath;
  91. public $format;
  92.  
  93. private $formats = array(
  94. 'application/xml' => 'xml',
  95. 'text/xml' => 'xml',
  96. 'application/json' => 'json',
  97. 'text/json' => 'json',
  98. 'text/plain' => 'text',
  99. 'text/html' => 'html',
  100. );
  101.  
  102. function __construct($request, $response) {
  103. $this->request = $request;
  104.  
  105. $this->code = $request->curl_getinfo(CURLINFO_HTTP_CODE);
  106.  
  107. list($this->type, $this->charset) = array_map('trim', explode(';', $request->curl_getinfo(CURLINFO_CONTENT_TYPE), 2));
  108.  
  109. $this->format = $request->format;
  110. if (!$this->format && isset($this->formats[$this->type])) {
  111. $this->format = $this->formats[$this->type];
  112. }
  113.  
  114. $header_size = $request->curl_getinfo(CURLINFO_HEADER_SIZE);
  115. $this->headers = $this->parse_headers(mb_substr($response, 0, $header_size));
  116. $this->body = mb_substr($response, $header_size);
  117.  
  118. //if (substr($this->code, 0, 1) == 2)
  119. $this->parse($this->body, $this->format);
  120. }
  121.  
  122. function parse_headers($input) {
  123. $items = array();
  124.  
  125. foreach(explode("\n", $input) as $row) {
  126. list($name, $value) = array_map('trim', explode(':', $row, 2));
  127. if($value) $items[strtolower($name)][] = $value;
  128. }
  129.  
  130. return $items;
  131. }
  132.  
  133. function parse($content, $format) {
  134. switch ($format) {
  135. case 'html':
  136. $this->dom = new DOMDocument;
  137. $this->dom->preserveWhiteSpace = false;
  138. $this->dom->documentURI = $this->request->curl_getinfo(CURLINFO_EFFECTIVE_URL);
  139.  
  140. $useErrors = libxml_use_internal_errors(true); // silence errors loading HTML
  141. $this->dom->loadHTML($content);
  142. libxml_use_internal_errors($useErrors); // restore
  143.  
  144. $this->xpath = new DOMXPath($this->dom);
  145. break;
  146.  
  147. case 'xml':
  148. $this->dom = new DOMDocument;
  149. $this->dom->preserveWhiteSpace = false;
  150. $this->dom->documentURI = $this->request->curl_getinfo(CURLINFO_EFFECTIVE_URL);
  151.  
  152. $this->dom->loadXML($content, LIBXML_DTDLOAD | LIBXML_NOENT | LIBXML_NOCDATA);
  153. $this->xpath = new DOMXPath($this->dom);
  154. break;
  155.  
  156. case 'json':
  157. $this->data = json_decode($content, true);
  158. break;
  159.  
  160. case 'text':
  161. default:
  162. $this->data = $content;
  163. break;
  164. }
  165. }
  166. }
Add Comment
Please, Sign In to add comment