PHP code example of zozlak / http-accept
1. Go to this page and download the library: Download zozlak/http-accept library. Choose the download type require. 2. Extract the ZIP file and open the index.php. 3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
zozlak / http-accept example snippets
// 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));