These interface implementations have been created to establish a standard between middleware implementations. Applications often need to receive data from external sources such as the users using the application. The Phalcon\Http\Message\ServerRequest represents an incoming, server-side HTTP request. Per the HTTP specification, this interface includes properties for each of the following: These interface implementations have been created to establish a standard between middleware implementations. Applications often need to receive data from external sources such as the users using the application. The Phalcon\Http\Message\ServerRequest represents an incoming, server-side HTTP request. Per the HTTP specification, this interface includes properties for each of the following:
Additionally, it encapsulates all data as it has arrived at the application from the CGI and/or PHP environment, including:
The values represented in $_SERVER.
Any cookies provided (generally via $_COOKIE)
Query string arguments (generally via $_GET, or as parsed via parse_str())
Upload files, if any (as represented by $_FILES)
Unserialized body parameters (generally from $_POST)
$_SERVER values are treated as immutable, as they represent application state at the time of request; as such, no methods are provided to allow modification of those values. The other values provide such methods, as they can be restored from $_SERVER or the request body, and may need treatment during the application (e.g., body parameters may be unserialized based on content type).
Additionally, this interface recognizes the utility of introspecting a request to derive and match additional parameters (e.g., via URI path matching, decrypting cookie values, unserializing non-form-encoded body content, matching authorization headers to users, etc). These parameters are stored in an “attributes” property.
The constructor accepts parameters allowing you to create the object with certain properties populated. You can define the target HTTP method, the URL, the body as well as the headers. All parameters are optional.
method - defaults to GET. The supported methods are: GET, CONNECT, DELETE, HEAD, OPTIONS, PATCH, POST, PUT, TRACE
serverParams - A key value array, with key as the server variable name and value as the server value
body - It defaults to php://input. The method accepts either an object that implements the StreamInterface interface or a string such as the name of the stream. The default mode for the stream is w+b. If a non valid stream is passed, an InvalidArgumentException is thrown
headers - A key value array, with key as the header name and value as the header value.
cookies - A key value array, with key as the cookie name and value as the cookie value.
queryParams - A key value array, with key as the query parameter name and value as the query parameter value.
uploadFiles - An array of uploaded files ($_FILES)
parsedBody - The parsed body of the server request
protocol - A string representing the protocol (1.0, 1.1)
Returns a single derived request attribute. The method gets a single attribute as produced by getAttributes(). The first parameter defines the name of the attribute that we need to retrieve. You can also supply a second variable which will be used as a default, in case the requested attribute name does not exist.
Returns an array with all the attributes derived from the request. These request “attributes” may be used to allow injection of any parameters derived from the request: e.g., the results of path match operations; the results of decrypting cookies; the results of unserializing non-form-encoded message bodies; etc. Attributes will be application and request specific, and can be mutable.
Returns all of the header values of the given case-insensitive header name as a string concatenated together using a comma. If the string parameter representing the header name requested, an empty string is returned.
Returns an array with all the message header values. The keys represent the header name as it will be sent over the wire, and each value is an array of strings associated with the header. While header names are not case-sensitive, this method preserves the exact case in which headers were originally specified.
Returns any parameters provided in the request body. If the request Content-Type is either application/x-www-form-urlencoded or multipart/form-data, and the request method is POST, this method will return the contents of $_POST. Otherwise, this method may return any results of unserializing the request body content; as parsing returns structured content, the potential types will be arrays or objects only. If there is no body content, null will be returned.
Returns an array with the unserialized query string arguments, if any. Note that the query params might not be in sync with the URI or server parameters. If you need to ensure you are only getting the original values, you may need to parse the query string from getUri()->getQuery() or from the QUERY_STRING server parameter.
Returns a string representing the message’s request-target either as it will appear (for clients), as it appeared at request (for servers), or as it was specified for the instance (see withRequestTarget()). In most cases, this will be the origin-form of the composed URI, unless a value was provided to the concrete implementation (see withRequestTarget()).
Returns an array with upload metadata in a normalized tree, with each leaf is an instance of Psr\Http\Message\UploadedFileInterface. These values can derive from the $_FILES superglobal or the message body during instantiation or they can be injected using withUploadedFiles(). If no data is present, an empty array will be returned.
The Request object is immutable. However there are a number of methods that allow you to inject data into it. The returned object is a clone of the original one.
Returns an instance with an additional header appended with the given value. Existing values for the specified header will be maintained. The new value(s) will be appended to the existing list. If the header did not exist previously, it will be added. Throws InvalidArgumentException for invalid header names or values. The header values can be a string or an array of strings.
Returns an instance with the specified cookies. The data is not required to come from the $_COOKIE superglobal, but it must be compatible with the structure of $_COOKIE. Typically, this data will be injected at instantiation. This method does not update the related Cookie header of the request instance, nor related values in the server parameters.
Returns an instance with the provided value replacing the specified header. While header names are case-insensitive, the casing of the header will be preserved by this function, and returned from getHeaders(). Throws InvalidArgumentException for invalid header names or values.
<?phpusePhalcon\Http\Message\ServerRequest;$request=newServerRequest('POST');echo$request->getMethod();// POST$clone=$request->withMethod('GET');echo$clone->getMethod();// GET
Returns an instance with the specified body parameters. If the request Content-Type is either application/x-www-form-urlencoded or multipart/form-data, and the request method is POST, this method should be used only to inject the contents of $_POST. The data is not required to come from $_POST, but will be the results of unserializing the request body content. Unserialization/parsing returns structured data, and, as such, this method only accepts arrays or objects, or a null value if nothing was available to parse.
As an example, if content negotiation determines that the request data is a JSON payload, this method could be used to create a request instance with the unserialized parameters. Throws InvalidArgumentException for unsupported argument types.
Returns an instance with the specified query string arguments. These values remain immutable over the course of the incoming request. You can inject these parameters during instantiation, such as from PHP’s $_GET superglobal, or they can be derived from some other value such as the URI. In cases where the arguments are parsed from the URI, the data is compatible with what PHP’s parse_str() would return for purposes of how duplicate query parameters are handled, and how nested sets are handled.
Returns an instance with the provided UriInterface URI. This method updates the Host header of the returned request by default if the URI contains a host component. If the URI does not contain a host component, any pre-existing Host header will be carried over to the returned request.
You can opt-in to preserving the original state of the Host header by setting $preserveHost to true. When $preserveHost is set to true, this method interacts with the Host header in the following ways:
If the Host header is missing or empty, and the new URI contains a host component, this method will update the Host header in the returned request.
If the Host header is missing or empty, and the new URI does not contain a host component, this method will not update the Host header in the returned request.
If a Host header is present and non-empty, this method will not update the Host header in the returned request.