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