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.
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');
|
None currently
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\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()
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\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
query()
Return the parameter container responsible for query parameters.
Returns Zend\Stdlib\ParametersInterface
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()
Return the parameter container responsible for post parameters.
Returns Zend\Stdlib\ParametersInterface
cookie()
Return the Cookie header, this is the same as calling $request->header()->get(‘Cookie’);.
Returns Zend\Http\Header\Cookie
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()
Return the parameter container responsible for file parameters
Returns Zend\Stdlib\ParametersInterface
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()
Return the parameter container responsible for server parameters
Returns Zend\Stdlib\ParametersInterface
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()
Return the parameter container responsible for env parameters
Returns Zend\Stdlib\ParametersInterface
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
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
*/
|
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.