cURL returns the complete response including headers when you set CURLOPT_HEADER option. The header and content are separated by two CRLF; the headers themselves are separated using CRLF. This information should be enough for you to parse the status header from the response:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost/err-500.php");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
list($header, $body) = explode("\r\n\r\n", $data, 2);
$header = explode("\r\n", $header);
echo $header[0];
Output:
HTTP/1.1 500 Internal Server Error
If you are interested in throwing an error, your script can emit the 500 response status plus a custom header with a single line of code:
Or you can use the header() function to set the response code:
header("X-Error-Message: Invalid Signature", true, 500);
PHP fills in the 1.1 and Internal Server Error portions of the status line:
HTTP/1.1 500 Internal Server Error
Content-Type: text/html
Server: Microsoft-IIS/7.5
X-Powered-By: PHP/5.3.19
X-Powered-By: ASP.NET
X-Error-Message: Invalid Signature
Date: Thu, 14 Feb 2013 21:11:00 GMT
Content-Length: 100