Download the PHP package kambo/httpmessage without Composer
On this page you can find all versions of the php package kambo/httpmessage. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download kambo/httpmessage
More information about kambo/httpmessage
Files in kambo/httpmessage
Package httpmessage
Short Description Kambo httpmessage a PSR-7 implementation
License MIT
Informations about the package httpmessage
kambo httpmessage
Standalone complete implementation of PSR-7 - HTTP message interfaces.
Package is not depended on any existing framework and implements only functionality described in PSR-7.
Install
Prefered way to install library is with composer:
Basic usage
Factories
Package comes with the set of factory classes for creating instances of , , and classes from the super global variables (, , etc.). Each of object is created by calling method on corresponding factory. Only parameter is instance of environment class. For example following code will create instance of :
Environment object is simple wrapper around server and CGI variables and is usually instanced from the super global variables (, , etc.).
Constructor of class has two mandatory parameters - and .
Server is an associative array containing information such as headers, paths, and script locations, it must have same structure as the super global variable . Body is , created from raw data from the request body. Usually it should be created from the stream.
Constructor also accept three optional parameters - post, cookie and files. Each of these parameters is counterpart to super global variable and it must have same structure eg.: cookie must have same structure as the variable. If some of these parameters is missing an empty array is used.
Server request - ServerRequest
Server request is representation of an incoming, server-side HTTP request.
Per the HTTP specification, this class includes properties for each of the following:
- Protocol version
- HTTP method
- URI
- Headers
- Message body
Additionally, it encapsulates all data as it has arrived to 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)
- Deserialized body parameters (generally from $_POST)
Creation of instance is done through from existing super global variables ($_POST, $_COOKIE, $_FILES, etc.):
Working with ServerRequest
Getting values
ServerRequest comes with lot of handy methods for working with the request:
One of the most important part of the request its body. Request body can be obtained by calling method :
Package parse raw body of request according the content type of request. Following content type are supported:
- json, if the content type is application/json,
- xml, if the content type is application/xml or text/xml (instance of SimpleXMLElement is returned) and
- query string, if the content type is application/x-www-form-urlencoded or multipart/form-data
Uploaded files are stored as tree of UploadedFile class instances. They can be obtained with method :
Modifying request
Request can be modified by methods that are prefixed by with string. For example:
- withMethod() - change method of the request
- withQueryParams() - change query params
- withParsedBody() - change parsed body
- withCookieParams() - change cookie parameters
As the requests are immutable; all methods that change state retain the internal state of the current message and return an instance that contains the changed state.
Stream
Stream provides a wrapper around the most common PHP streams operations, including serialization of the entire stream to a string.
Stream is usually used for describing of or body content.
New instance of Stream must be created from existing :
Content of can be easily converted to string with method :
Request
Request is representation of an outgoing, client-side request. It can for example represent request to some third party website.
Request object is created with method and URI as the parameters:
Request object constructor also accept three additional optional parameters - headers, body and protocol.
Headers are represented by instance of object, following snippet show creating request with header set to value :
Request body can be string or instance of class, if the string is provided an instance of will be created from this string:
Request is immutable; all methods that change state retain the internal state of the instance and return a new instance that contains the changed state.
Note: package do not provide client for performing the actual request.
Response
Response is representation of an outgoing, server-side response. Usually represents data that will be send to a browser.
Constructor of response object has three optional parameters - status, headers and body. If the status is not provided status code 200 ('OK') is used. Default value for the rest of parameters is null.
If you want to include additional headers you can do it in same way as in , by creating and setting instance of class:
Treatment of body is also same as in the case of class, it can be an instance of or string.
Response is immutable; all methods that change state retain the internal state of the instance and return a new instance that contains the changed state.
License
The MIT License (MIT), https://opensource.org/licenses/MIT