Previous Next

Zend\Http\Request

Overview

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

A typical HTTP request looks like this:

--------------------------
| METHOD | URI | VERSION |
--------------------------
|        HEADERS         |
--------------------------
|         BODY           |
--------------------------
        

In simplified terms, the request consist of a method, URI and the HTTP version number which all make up the "Request Line." Next is a set of headers; there can be 0 or an unlimited number of headers. After that is the request body, which is typically used when a client wishes to send data to the server in the form of an encoded file, or include a set of POST parameters, for example. More information on the structure and specification of an HTTP request can be found in » RFC-2616 on the W3.org site.

Quick Start

Request 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\Request class.

use Zend\Http\Request;
$request = Request::fromString(<<setMethod(Request::METHOD_POST);
$request->setUri('/foo');
$request->header()->addHeaders(array(
    'HeaderField1' => 'header-field-value',
    'HeaderField2' => 'header-field-value2',
);
$request->post()->set('foo', 'bar');

Configuration Options

None currently

Available Methods

Request::fromString ( string $string )

A factory that produces a Request object from a well-formed Http Request string

Returns Zend\Http\Request

setMethod ( string $method )

Set the method for this request.

Returns Zend\Http\Request

getMethod ( )

Return the method for this request.

Returns string.

setUri ( string|Zend\Stdlib\RequestDescription\Zend\Stdlib\Message\Zend\Stdlib\ParametersDescription\Zend\Stdlib\Parameters\Zend\Uri\Http $uri )

Set the URI/URL for this request; this can be a string or an instance of Zend\Uri\Http.

Returns Zend\Http\Request

getUri )

Return the URI for this request object.

Returns string.

uri )

Return the URI for this request object as an instance of Zend\Uri\Http.

Returns Zend\Uri\Http.

setVersion ( string $version )

Set the HTTP version for this object, one of 1.0 or 1.1 (Request::VERSION_10, Request::VERSION_11).

Returns Zend\Http\Request.

getVersion )

Return the HTTP version for this request

Returns string

setQuery ( Zend\Stdlib\ParametersDescription $query )

Provide an alternate Parameter Container implementation for query parameters in this object. (This is NOT the primary API for value setting; for that, see query().)

Returns Zend\Http\Request

query )

Return the parameter container responsible for query parameters.

Returns Zend\Stdlib\ParametersDescription

setPost ( Zend\Stdlib\ParametersDescription $post )

Provide an alternate Parameter Container implementation for post parameters in this object. (This is NOT the primary API for value setting; for that, see post().)

Returns Zend\Http\Request

post )

Return the parameter container responsible for post parameters.

Returns Zend\Stdlib\ParametersDescription

cookie )

Return the Cookie header, this is the same as calling $request->header()->get('Cookie');.

Returns Zend\Http\Header\Cookie

setFile ( Zend\Stdlib\ParametersDescription $files )

Provide an alternate Parameter Container implementation for file parameters in this object. (This is NOT the primary API for value setting; for that, see file().)

Returns Zend\Http\Request

file )

Return the parameter container responsible for file parameters

Returns Zend\Stdlib\ParametersDescription

setServer ( Zend\Stdlib\ParametersDescription $server )

Provide an alternate Parameter Container implementation for server parameters in this object. (This is NOT the primary API for value setting; for that, see server().)

Returns Zend\Http\Request

server )

Return the parameter container responsible for server parameters

Returns Zend\Stdlib\ParametersDescription

setEnv ( Zend\Stdlib\ParametersDescription $env )

Provide an alternate Parameter Container implementation for env parameters in this object. (This is NOT the primary API for value setting; for that, see env().)

Returns Zend\Http\Request

env )

Return the parameter container responsible for env parameters

Returns Zend\Stdlib\ParametersDescription

setHeader ( Zend\Http\Headers $headers )

Provide an alternate Parameter Container implementation for headers in this object. (This is NOT the primary API for value setting; for that, see header().)

Returns Zend\Http\Request

header )

Return the header container responsible for headers

Returns Zend\Http\Headers

setRawBody ( string $string )

Set the raw body for the request

Returns Zend\Http\Request

getRawBody ( )

Get the raw body for the request

Returns string

isOptions )

Is this an OPTIONS method request?

Returns bool

isGet )

Is this a GET method request?

Returns bool

isHead )

Is this a HEAD method request?

Returns bool

isPost )

Is this a POST method request?

Returns bool

isPut )

Is this a PUT method request?

Returns bool

isDelete )

Is this a DELETE method request?

Returns bool

isTrace )

Is this a TRACE method request?

Returns bool

isConnect )

Is this a CONNECT method request?

Returns bool

renderRequestLine )

Return the formatted request line (first line) for this HTTP request

Returns string

toString )

Returns string

__toString )

Allow PHP casting of this object

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

Examples

Example #1 Generating a Request object from a string

use Zend\Http\Request;
$string = "GET /foo HTTP/1.1\r\n\r\nSome Content";
$request = Request::fromString($string);

$request->getMethod();  // returns Request::METHOD_GET
$request->getUri();     // returns '/foo'
$request->getVersion(); // returns Request::VERSION_11 or '1.1'
$request->getRawBody(); // returns 'Some Content'

Example #2 Generating a Request object from an array

N/A

Example #3 Retrieving and setting headers

use Zend\Http\Request;
$request = new Request();
$request->headers()->get('Content-Type'); // return content type
$request->headers()->addHeader(new Cookie('foo' => 'bar'));
foreach ($request->headers() as $header) {
    echo $header->getFieldName() . ' with value ' . $header->getFieldValue();
}

Example #4 Retrieving and setting GET and POST values

use Zend\Http\Request;
$request = new Request();

// post() and get() both return, by default, a Parameters object, which extends ArrayObject
$request->post()->foo = 'value';
echo $request->get()->myVar;
echo $request->get()->offsetGet('myVar');

Example #5 Generating an formatted HTTP Request from an Request object

use Zend\Http\Request;
$request = new Request();
$request->setMethod(Request::METHOD_POST);
$request->setUri('/foo');
$request->header()->addHeaders(array(
    'HeaderField1' => 'header-field-value',
    'HeaderField2' => 'header-field-value2',
);
$request->post()->set('foo', 'bar');
echo $request->toString();

/** Will produce:
POST /foo HTTP/1.1
HeaderField1: header-field-value
HeaderField2: header-field-value2

foo=bar
*/
Previous Next