From bed8b879e5ed392d069126985fca17a9a00f8f6f Mon Sep 17 00:00:00 2001 From: Joseph Brower Date: Mon, 18 Mar 2013 11:12:40 -0600 Subject: [PATCH 1/3] Added the ability to fetch the response headers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This time, I used the right file.  You can now fetch response headers happily. --- libraries/Curl.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/libraries/Curl.php b/libraries/Curl.php index c53ae48..e1f1409 100644 --- a/libraries/Curl.php +++ b/libraries/Curl.php @@ -23,6 +23,7 @@ class Curl { public $error_code; // Error code returned as an int public $error_string; // Error message returned as a string public $info; // Returned after request (elapsed time, etc) + public $response_headers; // Array containing all response headers function __construct($url = '') { @@ -281,6 +282,10 @@ public function execute() { $this->options[CURLOPT_FAILONERROR] = TRUE; } + if ( ! isset($this->options[CURLOPT_HEADER])) + { + $this->options[CURLOPT_HEADER] = TRUE; + } // Only set follow location if not running securely if ( ! ini_get('safe_mode') && ! ini_get('open_basedir')) @@ -302,6 +307,8 @@ public function execute() // Execute the request & and hide all output $this->response = curl_exec($this->session); $this->info = curl_getinfo($this->session); + $this->response_headers = explode("\r\n\r\n", $this->response, $this->info['redirect_count'] + 2); + $this->body = array_pop($this->response_headers); // Request failed if ($this->response === FALSE) @@ -322,7 +329,7 @@ public function execute() else { curl_close($this->session); - $this->last_response = $this->response; + $this->last_response = $this->body; $this->set_defaults(); return $this->last_response; } @@ -376,4 +383,4 @@ public function set_defaults() } /* End of file Curl.php */ -/* Location: ./application/libraries/Curl.php */ \ No newline at end of file +/* Location: ./application/libraries/Curl.php */ From 89639f1a60c5256af97d1f7f767ec3afa228e7c1 Mon Sep 17 00:00:00 2001 From: Joseph Brower Date: Mon, 18 Mar 2013 11:42:03 -0600 Subject: [PATCH 2/3] Enhanced the handling of headers Now each redirect produces its own array of header name/value pairs. --- libraries/Curl.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/libraries/Curl.php b/libraries/Curl.php index e1f1409..09a592e 100644 --- a/libraries/Curl.php +++ b/libraries/Curl.php @@ -308,6 +308,17 @@ public function execute() $this->response = curl_exec($this->session); $this->info = curl_getinfo($this->session); $this->response_headers = explode("\r\n\r\n", $this->response, $this->info['redirect_count'] + 2); + foreach ($this->response_headers as $redirect => $headers) + { + $headers = explode("\r\n", $headers); + $this->response_headers[$redirect] = array(); + $this->response_headers[$redirect]['Status'] = array_shift($headers); + foreach ($headers as $header_line) + { + list($key, $value) = explode(':', $header_line, 2); + $this->response_headers[$redirect][$key] = trim($value); + } + } $this->body = array_pop($this->response_headers); // Request failed From 895c568235f2d5fc9bd43dbdca37e1fcdb30c48b Mon Sep 17 00:00:00 2001 From: Joseph Brower Date: Mon, 18 Mar 2013 13:30:32 -0600 Subject: [PATCH 3/3] Fixed an issue with the body pop order --- libraries/Curl.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Curl.php b/libraries/Curl.php index 09a592e..f8f4997 100644 --- a/libraries/Curl.php +++ b/libraries/Curl.php @@ -308,6 +308,7 @@ public function execute() $this->response = curl_exec($this->session); $this->info = curl_getinfo($this->session); $this->response_headers = explode("\r\n\r\n", $this->response, $this->info['redirect_count'] + 2); + $this->body = array_pop($this->response_headers); foreach ($this->response_headers as $redirect => $headers) { $headers = explode("\r\n", $headers); @@ -319,7 +320,6 @@ public function execute() $this->response_headers[$redirect][$key] = trim($value); } } - $this->body = array_pop($this->response_headers); // Request failed if ($this->response === FALSE)