curl_response.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. # CurlResponse
  3. #
  4. # Author Sean Huber - shuber@huberry.com
  5. # Date May 2008
  6. #
  7. # A basic CURL wrapper for PHP
  8. #
  9. # See the README for documentation/examples or http://php.net/curl for more information
  10. # about the libcurl extension for PHP -- http://github.com/shuber/curl/tree/master
  11. #
  12. class CurlResponse
  13. {
  14. public $body = '';
  15. public $headers = array();
  16. public function __construct($response)
  17. {
  18. # Extract headers from response
  19. $pattern = '#HTTP/\d\.\d.*?$.*?\r\n\r\n#ims';
  20. preg_match_all($pattern, $response, $matches);
  21. $headers = explode("\r\n", str_replace("\r\n\r\n", '', array_pop($matches[0])));
  22. # Extract the version and status from the first header
  23. $version_and_status = array_shift($headers);
  24. preg_match('#HTTP/(\d\.\d)\s(\d\d\d)\s(.*)#', $version_and_status, $matches);
  25. $this->headers['Http-Version'] = $matches[1];
  26. $this->headers['Status-Code'] = $matches[2];
  27. $this->headers['Status'] = $matches[2].' '.$matches[3];
  28. # Convert headers into an associative array
  29. foreach ($headers as $header) {
  30. preg_match('#(.*?)\:\s(.*)#', $header, $matches);
  31. $this->headers[$matches[1]] = $matches[2];
  32. }
  33. # Remove the headers from the response body
  34. $this->body = preg_replace($pattern, '', $response);
  35. }
  36. public function __toString()
  37. {
  38. return $this->body;
  39. }
  40. public function headers(){
  41. return $this->headers;
  42. }
  43. }