|
|
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.
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');
None currently
A factory that produces a Request object from a well-formed Http Request string
Returns Zend\Http\Request
Set the method for this request.
Returns Zend\Http\Request
Return the method for this request.
Returns string.
Set the URI/URL for this request; this can be a string or an instance of
Zend\Uri\Http.
Returns Zend\Http\Request
Return the URI for this request object.
Returns string.
Return the URI for this request object as an instance of
Zend\Uri\Http.
Returns Zend\Uri\Http.
Set the HTTP version for this object, one of 1.0 or 1.1 (Request::VERSION_10, Request::VERSION_11).
Returns Zend\Http\Request.
Return the HTTP version for this request
Returns string
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
Return the parameter container responsible for query parameters.
Returns Zend\Stdlib\ParametersDescription
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
Return the parameter container responsible for post parameters.
Returns Zend\Stdlib\ParametersDescription
Return the Cookie header, this is the same as calling
$request->header()->get('Cookie');.
Returns Zend\Http\Header\Cookie
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
Return the parameter container responsible for file parameters
Returns Zend\Stdlib\ParametersDescription
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
Return the parameter container responsible for server parameters
Returns Zend\Stdlib\ParametersDescription
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
Return the parameter container responsible for env parameters
Returns Zend\Stdlib\ParametersDescription
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
Return the header container responsible for headers
Returns Zend\Http\Headers
Set the raw body for the request
Returns Zend\Http\Request
Get the raw body for the request
Returns string
Is this an OPTIONS method request?
Returns bool
Is this a GET method request?
Returns bool
Is this a HEAD method request?
Returns bool
Is this a POST method request?
Returns bool
Is this a PUT method request?
Returns bool
Is this a DELETE method request?
Returns bool
Is this a TRACE method request?
Returns bool
Is this a CONNECT method request?
Returns bool
Return the formatted request line (first line) for this HTTP request
Returns string
Returns string
Allow PHP casting of this object
Returns string
Set message metadata
Non-destructive setting of message metadata; always adds to the metadata, never overwrites the entire metadata container.
Returns Zend\Stdlib\Message
Retrieve all metadata or a single metadatum as specified by key
Returns mixed
Set message content
Returns Zend\Stdlib\Message
Get message content
Returns mixed
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
*/
|
|
Copyright © 2005-2011 Zend Technologies Inc (compiled by mikaelkael with ZFDocumentor - GIT c517eb0).

