Just setting up a super simple API for something but I have a question. I'm calling the api, and it works perfectly, but I'm wondering about the header status code that's being return.
On all errors, I want to set the error to 500:
header('HTTP 1.1 500 invalid signature');
It's the description I'm curious about. Is there a way to grab that text using CuRL? I can get the code no problem, not the text.
Or can someone recommend and easier way of throwing a status of 500, and passing a string back through to the api file that can show what error happened?
If I were you I would use some other custom header for status message:
header('HTTP/1.1 500 Internal Server Error');
header('My-Super-Custom-Error-Msg: invalid signature');
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("
", $data, 2);
$header = explode("
", $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