Created
October 8, 2012 08:45
-
-
Save renan/3851459 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| diff --git a/CurlResponse.php b/CurlResponse.php | |
| index dda2e62..6095fda 100644 | |
| --- a/CurlResponse.php | |
| +++ b/CurlResponse.php | |
| @@ -38,26 +38,47 @@ class CurlResponse { | |
| # Headers regex | |
| $pattern = '#HTTP/\d\.\d.*?$.*?\r\n\r\n#ims'; | |
| + # Start the body | |
| + $this->body = $response; | |
| + | |
| # Extract headers from response | |
| preg_match_all($pattern, $response, $matches); | |
| - $headers_string = array_pop($matches[0]); | |
| - $headers = explode("\r\n", str_replace("\r\n\r\n", '', $headers_string)); | |
| - # Remove headers from the response body | |
| - $this->body = str_replace($headers_string, '', $response); | |
| + # Go though all the headers (maybe a redirect prior the 200) but only get the last headers | |
| + foreach ($matches[0] as $match) { | |
| + $headers = explode("\r\n", str_replace("\r\n\r\n", '', $match)); | |
| + | |
| + # Remove headers from the response body | |
| + $this->body = str_replace($match, '', $this->body); | |
| + } | |
| + | |
| + # Extract the header | |
| + $this->headers = $this->_extract($headers); | |
| + } | |
| + /** | |
| + * Extract the header information from an string | |
| + * | |
| + * @param string $headers | |
| + */ | |
| + function _extract($headers) { | |
| # Extract the version and status from the first header | |
| $version_and_status = array_shift($headers); | |
| preg_match('#HTTP/(\d\.\d)\s(\d\d\d)\s(.*)#', $version_and_status, $matches); | |
| - $this->headers['Http-Version'] = $matches[1]; | |
| - $this->headers['Status-Code'] = $matches[2]; | |
| - $this->headers['Status'] = $matches[2].' '.$matches[3]; | |
| + | |
| + $_headers = array( | |
| + 'Http-Version' => $matches[1], | |
| + 'Status-Code' => $matches[2], | |
| + 'Status' => $matches[2].' '.$matches[3] | |
| + ); | |
| # Convert headers into an associative array | |
| foreach ($headers as $header) { | |
| preg_match('#(.*?)\:\s(.*)#', $header, $matches); | |
| - $this->headers[$matches[1]] = $matches[2]; | |
| + $_headers[$matches[1]] = $matches[2]; | |
| } | |
| + | |
| + return $_headers; | |
| } | |
| /** |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| HTTP/1.1 301 Moved Permanently | |
| Server: nginx/1.1.19 | |
| Date: Mon, 08 Oct 2012 08:44:04 GMT | |
| Content-Type: text/html | |
| Content-Length: 185 | |
| Connection: keep-alive | |
| Location: https://newlocation.com | |
| HTTP/1.1 200 OK | |
| Server: nginx/1.1.19 | |
| Date: Mon, 08 Oct 2012 08:44:05 GMT | |
| Content-Type: application/json | |
| Content-Length: 1744 | |
| Connection: keep-alive | |
| body content |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment