Previous Next

Zend\Http\Response

Overview

The Zend\Http\Response class is responsible for providing a fluent API that allows a developer to interact with all the various parts of an HTTP response.

A typical HTTP Response looks like this:

---------------------------
| VERSION | CODE | REASON |
---------------------------
|        HEADERS          |
---------------------------
|         BODY            |
---------------------------
        

The first line of the response consists of the HTTP version, status code, and the reason string for the provided status code; this is called the Response Line. Next is a set of headers; there can be 0 or an unlimited number of headers. The remainder of the response is the response body, which is typically a string of HTML that will render on the client's browser, but which can also be a place for request/response payload data typical of an AJAX request. More information on the structure and specification of an HTTP response can be found in » RFC-2616 on the W3.org site.

Quick Start

Response objects can either be created from the provided fromString() factory, or, if you wish to have a completely empty object to start with, by simply instantiating the Zend\Http\Response class.

use Zend\Http\Response;
$response = Response::fromString(<<

    Hello World


EOS);

// OR

$response = new Request();
$response->setStatusCode(Response::STATUS_CODE_200);
$response->headers()->addHeaders(array(
    'HeaderField1' => 'header-field-value',
    'HeaderField2' => 'header-field-value2',
);
$response->setRawBody(<<

    Hello World


EOS);

Configuration Options

None currently available

Available Methods

Response::fromString ( string $string )

Populate object from string

Returns Zend\Http\Response

renderStatusLine )

Render the status line header

Returns string

setHeaders ( Zend\Http\Headers $headers )

Set response headers

Returns Zend\Http\Response

headers )

Get response headers

Returns Zend\Http\Headers

setVersion ( string $version )

Returns Zend\Http\Response

getVersion )

Returns string

getStatusCode )

Retrieve HTTP status code

Returns int

setReasonPhrase ( string $reasonPhrase )

Returns Zend\Http\Response

getReasonPhrase )

Get HTTP status message

Returns string

setStatusCode ( numeric $code )

Set HTTP status code and (optionally) message

Returns Zend\Http\Response

isClientError )

Does the status code indicate a client error?

Returns bool

isForbidden )

Is the request forbidden due to ACLs?

Returns bool

isInformational )

Is the current status "informational"?

Returns bool

isNotFound )

Does the status code indicate the resource is not found?

Returns bool

isOk )

Do we have a normal, OK response?

Returns bool

isServerError )

Does the status code reflect a server error?

Returns bool

isRedirect )

Do we have a redirect?

Returns bool

isSuccess )

Was the response successful?

Returns bool

decodeChunkedBody ( string $body )

Decode a "chunked" transfer-encoded body and return the decoded text

Returns string

decodeGzip ( string $body )

Decode a gzip encoded message (when Content-encoding = gzip)

Currently requires PHP with zlib support

Returns string

decodeDeflate ( string $body )

Decode a zlib deflated message (when Content-encoding = deflate)

Currently requires PHP with zlib support

Returns string

setMetadata ( string|int|array|Traversable $spec, mixed $value )

Set message metadata

Non-destructive setting of message metadata; always adds to the metadata, never overwrites the entire metadata container.

Returns Zend\Stdlib\Message

getMetadata ( null|string|int $key, null|mixed $default )

Retrieve all metadata or a single metadatum as specified by key

Returns mixed

setContent ( mixed $value )

Set message content

Returns Zend\Stdlib\Message

getContent )

Get message content

Returns mixed

toString )

Returns string

Examples

Example #1 Generating a Response object from a string

use Zend\Http\Response;
$request = Response::fromString(<<

    Hello World


EOS);

Example #2 Generating a Response object from a string

use Zend\Http\Response;
$response = new Response();
$response->setStatusCode(Response::STATUS_CODE_200);
$response->headers()->addHeaders(array(
    'HeaderField1' => 'header-field-value',
    'HeaderField2' => 'header-field-value2',
);
$response->setRawBody(<<

    Hello World


EOS);
Previous Next