anterior  
índice  
siguiente  
Updated 2012-10-02 - Version ZF 2.0.3

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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
use Zend\Http\Request;
$request = Request::fromString(<<<EOS
POST /foo HTTP/1.1
HeaderField1: header-field-value
HeaderField2: header-field-value2

foo=bar&
EOS);

// OR, the completely equivalent

$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');

Configuration Options

None currently

Available Methods

Request::fromString

Request::fromString(string $string)

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

Returns Zend\Http\Request

setMethod

setMethod(string $method)

Set the method for this request.

Returns Zend\Http\Request

getMethod

getMethod()

Return the method for this request.

Returns string.

setUri

setUri(string|\Zend\Stdlib\RequestInterface|\Zend\Stdlib\Message|\Zend\Stdlib\ParametersInterface|\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

getUri()

Return the URI for this request object.

Returns string.

uri

uri()

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

Returns Zend\Uri\Http.

setVersion

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.

setVersion

getVersion()

Return the HTTP version for this request

Returns string

setQuery

setQuery(Zend\Stdlib\ParametersInterface $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

setQuery

query()

Return the parameter container responsible for query parameters.

Returns Zend\Stdlib\ParametersInterface

setPost

setPost(Zend\Stdlib\ParametersInterface $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

post()

Return the parameter container responsible for post parameters.

Returns Zend\Stdlib\ParametersInterface

setFile

setFile(Zend\Stdlib\ParametersInterface $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

file()

Return the parameter container responsible for file parameters

Returns Zend\Stdlib\ParametersInterface

setServer

setServer(Zend\Stdlib\ParametersInterface $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

server()

Return the parameter container responsible for server parameters

Returns Zend\Stdlib\ParametersInterface

setEnv

setEnv(Zend\Stdlib\ParametersInterface $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

env()

Return the parameter container responsible for env parameters

Returns Zend\Stdlib\ParametersInterface

setHeader

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

header()

Return the header container responsible for headers

Returns Zend\Http\Headers

setRawBody

setRawBody(string $string)

Set the raw body for the request

Returns Zend\Http\Request

getRawBody

getRawBody()

Get the raw body for the request

Returns string

isOptions

isOptions()

Is this an OPTIONS method request?

Returns bool

isGet

isGet()

Is this a GET method request?

Returns bool

isHead

isHead()

Is this a HEAD method request?

Returns bool

isPost

isPost()

Is this a POST method request?

Returns bool

isPut

isPut()

Is this a PUT method request?

Returns bool

isDelete

isDelete()

Is this a DELETE method request?

Returns bool

isTrace

isTrace()

Is this a TRACE method request?

Returns bool

isConnect

isConnect()

Is this a CONNECT method request?

Returns bool

renderRequestLine

renderRequestLine()

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

Returns string

toString

toString()

Returns string

__toString

__toString()

Allow PHP casting of this object

Returns string

setMetadata

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

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

Retrieve all metadata or a single metadatum as specified by key

Returns mixed

setContent

setContent(mixed $value)

Set message content

Returns Zend\Stdlib\Message

getContent

getContent()

Get message content

Returns mixed

Examples

Generating a Request object from a string

1
2
3
4
5
6
7
8
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'

Generating a Request object from an array

1
N/A

Retrieving and setting headers

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

Retrieving and setting GET and POST values

1
2
3
4
5
6
7
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');

Generating a formatted HTTP Request from a Request object

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
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
*/
|

Contenidos

Tema anterior

Zend\Http

Próximo tema

Zend\Http\Response

Esta página

Nota: Es necesario estar conectado a tu cuenta GitHub para contribuir a la documentación.

Edit this document

Edit this document

The source code of this file is hosted on GitHub. Everyone can update and fix errors in this document with few clicks - no downloads needed.

  1. Login with your GitHub account.
  2. Go to Zend\Http\Request on GitHub.
  3. Edit file contents using GitHub's text editor in your web browser
  4. Fill in the Commit message text box at the end of the page telling why you did the changes. Press Propose file change button next to it when done.
  5. On Send a pull request page you don't need to fill in text anymore. Just press Send pull request button.
  6. Your changes are now queued for review under project's Pull requests tab on GitHub.