note 42009 modified in function.curl-setopt by vrana
| From: | vrana@php.net | Date: | Tue, 17 Aug 2004 14:56:06 +0000 |
| Subject: | note 42009 modified in function.curl-setopt by vrana | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-80059@lists.php.net to get a copy of this message | ||
I found it very annoying that cURL did not offer any easy way to extract the response headers. If
you do
curl_setopt($ch, CURLOPT_HEADER, 1);
then you only get the headers output inline with the rest of your response. Here's a nice
little function to parse the cURL response when you use CURLOPT_HEADER. I took it from an example
on another site and turned it into a function. It assumes you're using CURLOPT_RETURNTRANSFER
to return the response to some variable that is then passed to the function. You just pass it a
curl_exec result and it returns the result parsed into an array with headers broken up into their
correct parts.
<?php
function parse_response($response){
/*
***original code extracted from examples at
***http://www.webreference.com/programming
/php/cookbook/chap11/1/3.html
***returns an array in the following format which varies depending on headers returned
[0] => the HTTP error or response code such as 404
[1] => Array
(
[Server] => Microsoft-IIS/5.0
[Date] => Wed, 28 Apr 2004 23:29:20 GMT
[X-Powered-By] => ASP.NET
[Connection] => close
[Set-Cookie] => COOKIESTUFF
[Expires] => Thu, 01 Dec 1994 16:00:00 GMT
[Content-Type] => text/html
[Content-Length] => 4040
)
[2] => Response body (string)
*/
list($response_headers,$response_body) = explode("\r\n\r\n",$response,2);
$response_header_lines = explode("\r\n",$response_headers);
// first line of headers is the HTTP response code
$http_response_line = array_shift($response_header_lines);
if (preg_match('@^HTTP/[0-9]\.[0-9] ([0-9]{3})@',$http_response_line,
$matches)) {
$response_code = $matches[1];
}
// put the rest of the headers in an array
$response_header_array = array();
foreach ($response_header_lines as $header_line) {
list($header,$value) = explode(': ',$header_line,2);
$response_header_array[$header] = $value;
}
return array($response_code,$response_header_array,$response_body);
}
?>
--was--
I found it very annoying that cURL did not offer any easy way to extract the response headers. If
you do
curl_setopt($ch, CURLOPT_HEADER, 1);
then you only get the headers output inline with the rest of your response. Here's a nice
little function to parse the cURL response when you use CURLOPT_HEADER. I took it from an example
on another site and turned it into a function. It assumes you're using CURLOPT_RETURNTRANSFER
to return the response to some variable that is then passed to the function. You just pass it a
curl_exec result and it returns the result parsed into an array with headers broken up into their
correct parts.
function parse_response($response){
/*
***original code extracted from examples at
***http://www.webreference.com/programming
/php/cookbook/chap11/1/3.html
***returns an array in the following format which varies depending on headers returned
[0] => the HTTP error or response code such as 404
[1] => Array
(
[Server] => Microsoft-IIS/5.0
[Date] => Wed, 28 Apr 2004 23:29:20 GMT
[X-Powered-By] => ASP.NET
[Connection] => close
[Set-Cookie] => COOKIESTUFF
[Expires] => Thu, 01 Dec 1994 16:00:00 GMT
[Content-Type] => text/html
[Content-Length] => 4040
)
[2] => Response body (string)
*/
list($response_headers,$response_body) = explode("\r\n\r\n",$response,2);
$response_header_lines = explode("\r\n",$response_headers);
// first line of headers is the HTTP response code
$http_response_line = array_shift($response_header_lines);
if (preg_match('@^HTTP/[0-9]\.[0-9] ([0-9]{3})@',$http_response_line,
$matches)) {
$response_code = $matches[1];
}
// put the rest of the headers in an array
$response_header_array = array();
foreach ($response_header_lines as $header_line) {
list($header,$value) = explode(': ',$header_line,2);
$response_header_array[$header] = $value;
}
return array($response_code,$response_header_array,$response_body);
}
http://php.net/manual/en/function.curl-setopt.php