net-http-request
![Software License][ico-license]
, (*1)
This package provides a Request class which extends the PSR-7 ServerRequest class with header and role objects and simplifies access to attributes, query and post parameters., (*2)
The included RequestFactory class can build requests automatically from PHP superglobals or from given data. It uses the factory interfaces defined in binsoul/net-http-message-bridge to build URIs and streams. Headers typically set by load balancers or proxies are used to build requests., (*3)
Install
Via composer:, (*4)
``` bash
$ composer require binsoul/net-http-request, (*5)
## Usage
Build a request from PHP superglobals:
``` php
buildFromEnvironment();
```
Build a request from provided data:
``` php
write('Hello world!');
$factory = new RequestFactory(new DefaultUriFactory(), new DefaultStreamFactory());
$request = $factory->buildFromData($stream, $_SERVER, $_GET, $_POST, $_COOKIE, $_FILES);
echo (string) $request->getBody(); // Hello world!
```
Use the URI implementation from [league/uri](https://github.com/thephpleague/uri):
``` php
buildFromEnvironment();
```
## Example
Output a HTML page with information about the current request:
``` php
buildFromEnvironment();
// Roles
$client = $request->getClient();
$server = $request->getServer();
// Headers
$userAgent = $request->getUserAgent();
$cacheControl = $request->getCacheControl();
$acceptMediaType = $request->getAcceptMediaType();
$acceptEncoding = $request->getAcceptEncoding();
$acceptLanguage = $request->getAcceptLanguage();
$acceptCharset = $request->getAcceptCharset();
// make var_export HTML friendly
function dump($value) {
return htmlentities(preg_replace("/\s+=>\s+/m", ' => ', var_export($value, true)));
}
?>
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
h1 { font-size: 150%; margin: 1em 0 0.25em 0; border-bottom: 1px solid #888;}
td { vertical-align: top; padding: 2px 6px; }
td:first-child {white-space: nowrap;}
</style>
</head>
<body>
Request
| protocol: |
HTTP/= $request->getProtocolVersion() ?> |
| method: |
= $request->getMethod() ?> |
| URI: |
= $request->getUri() ?> |
| query: |
= dump($request->getQueryParams()) ?>
|
| post: |
= dump($request->getParsedBody()) ?>
|
| files: |
= dump($request->getUploadedFiles()) ?>
|
| headers: |
= dump($request->getHeaders()) ?>
|
| Is SSL? |
= $request->isSSL() ? 'yes' : 'no' ?> |
| Is DNT? |
= $request->isDoNotTrack() ? 'yes' : 'no' ?> |
| Is Javascript? |
= $request->isJavascript() ? 'yes' : 'no' ?> |
Server
| IP: |
= $server->getIP() ?> |
| port: |
= $server->getPort() ?> |
Client
| IP: |
= $client->getIP() ?> |
| port: |
= $client->getPort() ?> |
| Is headless? |
= $client->isHeadless() ? 'yes' : 'no' ?> |
User-Agent
| browser: |
= $userAgent->getBrowser() ?> |
| platform: |
= $userAgent->getPlatform() ?> |
| device: |
= $userAgent->getDeviceType() ?> |
| Is bot? |
= $userAgent->isBot() ? 'yes' : 'no' ?> |
Cache-Control
| Has max-age? |
= $cacheControl->hasMaxAge() ? 'yes' : 'no' ?> |
| max-age: |
= $cacheControl->getMaxAge() ?> |
| Is refresh? |
= $cacheControl->isRefresh() ? 'yes' : 'no' ?> |
| Is reload? |
= $cacheControl->isReload() ? 'yes' : 'no' ?> |
Accept
getMediaTypes() as $mediaType): ?>
- = $mediaType ?>
Accept-Encoding
getEncodings() as $encoding) : ?>
- = $encoding ?>
Accept-Language
getLanguages() as $language): ?>
- = $language ?>
Accept-Charset
getCharsets() as $charset): ?>
- = $charset ?>
</body>
</html>
Testing
bash
$ composer test, (*6)
License
The MIT License (MIT). Please see License File for more information., (*7)