Search code examples
phplaravelsymfonyhttp-response-codes

Is there a built in list of http response codes and their DESCRIPTIONS in Laravel or Symfony?


Everyone on the internet seems to understand that http response codes have a certain 'description' or 'message'. For example, 404 has the description "Not Found".

So I would expect frameworks like Laravel and Symfony to have a list of such descriptions. Right? But I can't seem to find them.

I'm trying to throw an exception when a cURL request responds with an unacceptable code. I don't have the code messages/descriptions memorized. Neither do I think it's smart to copy/paste/maintain forever an associative array of these values. I just want a framework like Laravel or Symfony to be smart enough to have their own that they manage for everyone. I would expect PHP to have one, but there is no evidence of them having one, and a few people have confirmed as much.

I'm hoping for a function (or maybe a global array?) that works like this:

    $responseCode = curl_getinfo($curl, CURLINFO_RESPONSE_CODE);
    if($responseCode && $responseCode > 399) {
        $message = http_response_code_description($responseCode);
        throw new \Exception("Error $responseCode; $message");//if $responseCode === 404, the exception message would read 'Error 404; Not Found'
    }

Since PHP doesn't appear to be helpful in this regard - do Laravel or Symfony have any solutions?

Neither this question... Predefined array of HTTP errors for PHP use?

...nor this question... How to get HTTP status text from HTTP status code?

...help in my situation.

I'm not using pure PHP.

I'm not using Guzzle.

I'm not using a request object.

I'm executing a cURL request, in a Laravel project (which includes PHP and Symfony).

I understand that there is no pure PHP solution that automates this. I figured from the beginning that this was likely - which is why I specifically asked if LARAVEL or SYMFONY had a solution in the original question.


Solution

  • Edited: This code will return http error description: \Illuminate\Http\Response::$statusTexts["404"]