chillerlan / php-http-message-utils
PSR-7/17/18 utilities
Fund package maintenance!
Ko Fi
Installs: 9 862
Dependents: 5
Suggesters: 1
Security: 0
Stars: 0
Watchers: 3
Forks: 0
Open Issues: 0
Requires
- php: ^8.1
- ext-fileinfo: *
- ext-intl: *
- ext-json: *
- ext-mbstring: *
- ext-simplexml: *
- ext-zlib: *
- psr/http-client: ^1.0
- psr/http-factory: ^1.1
- psr/http-message: ^1.1 || ^2.0
Requires (Dev)
- ext-curl: *
- chillerlan/phpunit-http: ^1.0
- guzzlehttp/guzzle: ^7.8
- phpmd/phpmd: ^2.15
- phpstan/phpstan: ^1.11
- phpstan/phpstan-deprecation-rules: ^1.2
- phpunit/phpunit: ^10.5
- slevomat/coding-standard: ^8.15
- squizlabs/php_codesniffer: ^3.10
Suggests
- chillerlan/php-httpinterface: A PSR-18 HTTP client implementation
- chillerlan/php-oauth: A PSR-7 OAuth client/handler that also acts as PSR-18 HTTP client
- chillerlan/psr-7: A PSR-7/PSR-17 HTTP message and factory implementation.
Provides
README
A collection of framework-agnostic utilities for use with PSR-7 Message implementations.
Documentation
Requirements
- PHP 8.1+
ext-fileinfo
,ext-intl
,ext-json
,ext-mbstring
,ext-simplexml
,ext-zlib
- for
MessageUtil::decompress()
:ext-br
kjdev/php-ext-brotli orext-zstd
kjdev/php-ext-zstd
Installation
requires composer
composer.json
(note: replace dev-main
with a version boundary, e.g. ^2.2
)
{ "require": { "php": "^8.1", "chillerlan/php-http-message-utils": "dev-main#<commit_hash>" } }
Profit!
Usage
URLExtractor
The URLExtractor
wraps a PSR-18 ClientInterface
to extract and follow shortened URLs to their original location.
// @see https://github.com/chillerlan/php-httpinterface $options = new HTTPOptions; $options->user_agent = 'my cool user agent 1.0'; $options->ssl_verifypeer = false; $options->curl_options = [ CURLOPT_FOLLOWLOCATION => false, CURLOPT_MAXREDIRS => 25, ]; $httpClient = new CurlClient($responseFactory, $options, $logger); $urlExtractor = new URLExtractor($httpClient, $responseFactory); $request = $factory->createRequest('GET', 'https://t.co/ZSS6nVOcVp'); $urlExtractor->sendRequest($request); // -> response from the final location // you can retrieve an array with all followed locations afterwards $responses = $urlExtractor->getResponses(); // -> ResponseInterface[] // if you just want the URL of the final location, you can use the extract method: $url = $urlExtractor->extract('https://t.co/ZSS6nVOcVp'); // -> https://api.guildwars2.com/v2/build
EchoClient
The EchoClient
returns a JSON representation the original message:
$echoClient = new EchoClient($responseFactory); $request = $requestFactory->createRequest('GET', 'https://example.com?whatever=value'); $response = $echoClient->sendRequest($request); $json = json_decode($response->getBody()->getContents());
Which yields an object similar to the following
{ "headers": { "Host": "example.com" }, "request": { "url": "https://example.com?whatever=value", "params": { "whatever": "value" }, "method": "GET", "target": "/", "http": "1.1" }, "body": "" }
LoggingClient
The LoggingClient
wraps a ClientInterface
and outputs the HTTP messages in a readable way through a LoggerInterface
(do NOT use in production!).
$loggingClient = new LoggingClient($httpClient, $logger); $loggingClient->sendRequest($request); // -> log to output given via logger
The output looks similar to the following (using monolog):
[2024-03-15 22:10:41][debug] LoggingClientTest:
----HTTP-REQUEST----
GET /get HTTP/1.1
Host: httpbin.org
[2024-03-15 22:10:41][debug] LoggingClientTest:
----HTTP-RESPONSE---
HTTP/1.1 200 OK
Date: Fri, 15 Mar 2024 21:10:40 GMT
Content-Type: application/json
Content-Length: 294
Connection: keep-alive
Server: gunicorn/19.9.0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
{
"args": {},
"headers": {
"Host": "httpbin.org",
"User-Agent": "chillerlanPHPUnitHttp/1.0.0 +https://github.com/chillerlan/phpunit-http",
"X-Amzn-Trace-Id": "Root=1-65f4b950-1f87b9e37182673438091aea"
},
"origin": "93.236.207.163",
"url": "https://httpbin.org/get"
}
API
The following classes contain static methods for use with PSR-7 http message objects.
HeaderUtil
QueryUtil
MessageUtil
UriUtil
MimeTypeUtil
StreamUtil
ServerUtil
The ServerUtil
object requires a set of PSR-17 factories on invocation, namely ServerRequestFactoryInterface
, UriFactoryInterface
, UploadedFileFactoryInterface
and StreamFactoryInterface
.
It provides convenience methods to create server requests, URIs and uploaded files from the superglobals.
Cookie
Implements a HTTP cookie