Handling a HTTP Response is an essential part to any RESTful service. Mobile and Web Clients speaking to your service sometimes rely on the HTTP Response Code to know whether an action was a success or not.
Examples are:
201 - Created
500 - Internal Server Error
404 - Not Found
Regardless of how you need to use it or why, handling the response isn't difficult and thankfully PHP does a great job of managing headers.
I wrote this helper class as part of my App_ Framework, but I've stripped it out to share with you.
<?php
class HttpResponseCode
{
    /**
     *
     * @var HttpResponseCode
     */
    protected static $_response = null;
    /**
     * Singleton instance which means this is redundant
     */
    protected function __construct()
    {}
    /**
     * Return instance of HttpResponseCode
     * @return HttpResponseCode
     */
    public static function getResponse()
    {
        if (null === self::$_response)
        {
            self::$_response = new self();
        }
        return self::$_response;
    }
    /**
     * Assign a HTTP Response code.
     * @param integer $code
     * @return HttpResponseCode
     */
    public function setHttpCode($code)
    {
        $http_codes = $this->_getHttpResponseCodes();
        if (!array_key_exists($code, $http_codes))
        {
            $string = 'HTTP/1.0 500 ' . $http_codes[500];
        }
        else
        {
            $string = 'HTTP/1.0 ' . $code . ' ' . $http_codes[$code];
        }
        header($string);
        return $this;
    }
    /**
     * Associated array of HTTP Response codes
     * @return array
     */
    protected function _getHttpResponseCodes()
    {
        return $http_codes = array(
            100 => 'Continue',
            101 => 'Switching Protocols',
            102 => 'Processing',
            200 => 'OK',
            201 => 'Created',
            202 => 'Accepted',
            203 => 'Non-Authoritative Information',
            204 => 'No Content',
            205 => 'Reset Content',
            206 => 'Partial Content',
            207 => 'Multi-Status',
            300 => 'Multiple Choices',
            301 => 'Moved Permanently',
            302 => 'Found',
            303 => 'See Other',
            304 => 'Not Modified',
            305 => 'Use Proxy',
            306 => 'Switch Proxy',
            307 => 'Temporary Redirect',
            400 => 'Bad Request',
            401 => 'Unauthorized',
            402 => 'Payment Required',
            403 => 'Forbidden',
            404 => 'Not Found',
            405 => 'Method Not Allowed',
            406 => 'Not Acceptable',
            407 => 'Proxy Authentication Required',
            408 => 'Request Timeout',
            409 => 'Conflict',
            410 => 'Gone',
            411 => 'Length Required',
            412 => 'Precondition Failed',
            413 => 'Request Entity Too Large',
            414 => 'Request-URI Too Long',
            415 => 'Unsupported Media Type',
            416 => 'Requested Range Not Satisfiable',
            417 => 'Expectation Failed',
            418 => 'I\'m a teapot',
            422 => 'Unprocessable Entity',
            423 => 'Locked',
            424 => 'Failed Dependency',
            425 => 'Unordered Collection',
            426 => 'Upgrade Required',
            449 => 'Retry With',
            450 => 'Blocked by Windows Parental Controls',
            500 => 'Internal Server Error',
            501 => 'Not Implemented',
            502 => 'Bad Gateway',
            503 => 'Service Unavailable',
            504 => 'Gateway Timeout',
            505 => 'HTTP Version Not Supported',
            506 => 'Variant Also Negotiates',
            507 => 'Insufficient Storage',
            509 => 'Bandwidth Limit Exceeded',
            510 => 'Not Extended'
        );
    }
}
Using this class couldn't possibly be any easier. Use it like the example below, replacing the 404 with whatever HTTP Response you wish to send out.
<?php
// Replace the 404 with your response code and let the
// class do the rest.
HttpResponseCode::getResponse()->setHttpCode(404);
And there you have it, send well formed HTTP Responses without all the hassle.
Tags: HTTP Response PHP, HTTP Code PHP

Rodriguo commented on Dec 9th 2012