zozlak / http-accept
Set of methods to deal with the HTTP Accept header
Installs: 7 406
Dependents: 6
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/zozlak/http-accept
Requires
- php: >=8.1
Requires (Dev)
- phpstan/phpstan: *
- phpunit/phpunit: ^10
This package is auto-updated.
Last update: 2026-02-17 13:20:57 UTC
README
HttpAccept
A static class making it easier to deal with the HTTP Accept header.
Can be also used to deal with other HTTP headers which provide multiple options with weights, e.g. the Accept-Encoding one.
installation
composer require zozlak/http-accept
usage
// Simplest use - parse the HTTP Accept header // e.g. for an Accept header of // text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 // the result is `application/xml;q=0.9` $header = zozlak\httpAccept\Accept::fromHeader(); $bestMatch = $header->getBestMatch(['application/json', 'application/xml']); echo (string) $bestMatch . "\n"; // Deal with Accept-Encoding $header = zozlak\httpAccept\Accept::fromHeader('accept-encoding'); try { $bestMatch = $header->getBestMatch(['deflate']); } catch (zozlak\httpAccept\NoMatchException) { $bestMatch = new zozlak\httpAccept\Format('identity'); } echo $bestMatch->type . "\n"; // Check if two formats match $format1 = zozlak\httpAccept\Format::fromString('application/xml'); $format2 = new zozlak\httpAccept\Format('application', '*'); var_dump($format1->matches($format2));