0

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?

3
  • Take a look at curl_error function: php.net/manual/en/function.curl-error.php Commented Feb 14, 2013 at 19:13
  • doesnt work because the url is still being loaded, so it returns as ok Commented Feb 14, 2013 at 19:27
  • If it is an API I suggest you don't need the response body for any data? So you could put your error/description string there. Commented Feb 14, 2013 at 19:48

2 Answers 2

1

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');
Sign up to request clarification or add additional context in comments.

2 Comments

"As far as I am concerned text that comes after the status code is fixed by the HTTP 1.1 standard." This seems to be addressed in the standard: w3.org/Protocols/rfc2616/rfc2616-sec6.html#sec6.1.1 "... an example set of corresponding Reason-Phrase's, are presented below. The reason phrases listed here are only recommendations -- they MAY be replaced by local equivalents without affecting the protocol"
Thanks a lot for noting this Josh. I was wrong indeed. I removed my false assumptions from answer.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.