chillerlan/php-http-message-utils

PSR-7/17/18 utilities

Fund package maintenance!
Ko Fi

2.2.2 2024-07-26 16:04 UTC

This package is auto-updated.

Last update: 2024-09-03 20:03:33 UTC


README

A collection of framework-agnostic utilities for use with PSR-7 Message implementations.

PHP Version Support version license Continuous Integration Coverage Codacy Packagist downloads

Documentation

Requirements

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