2017 © Pedro Peláez
 

library net-http-request

Extended ServerRequest class and request factory

image

binsoul/net-http-request

Extended ServerRequest class and request factory

  • Tuesday, April 26, 2016
  • by binsoul
  • Repository
  • 1 Watchers
  • 0 Stars
  • 21 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

net-http-request

Latest Version on Packagist ![Software License][ico-license] Total Downloads, (*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): ?>
  1. = $mediaType ?>

Accept-Encoding

    getEncodings() as $encoding) : ?>
  1. = $encoding ?>

Accept-Language

    getLanguages() as $language): ?>
  1. = $language ?>

Accept-Charset

    getCharsets() as $charset): ?>
  1. = $charset ?>
</body> </html>

Testing

bash $ composer test, (*6)

License

The MIT License (MIT). Please see License File for more information., (*7)

The Versions