This is a userland replacement for PHP's native get_browser()
function, which is officially supported by the Browser Capabilities Project.
Run the command below to install via Composer
composer require browscap/browscap-php
Then you may identify the current user agent this way:
$cache = new \MatthiasMullie\Scrapbook\Psr16\SimpleCache($doctrineFileCache); // or maybe any other PSR-16 compatible caches
$logger = new \Monolog\Logger('name'); // or maybe any other PSR-3 compatible logger
$browscap = new \BrowscapPHP\Browscap($cache, $logger);
$info = $browscap->getBrowser();
Before you can start, you have to run the browscap:fetch
command to download the browscap.ini
file, and use the
browscap:convert
command to convert it into a cache. There are two ways.
a. Download the file and convert it in two steps. The downloaded file will be stored in a local file, but there is no check
if the remote file has changed. If your cache gets corrupted you only need to rerun the convert
command.
vendor/bin/browscap-php browscap:fetch
vendor/bin/browscap-php browscap:convert
b. Download the file and convert it in one step. The downloaded file will not be stored in a local file, but there is a check if the remote file has changed. If your cache gets corrupted you have clean the cache and restart the process.
vendor/bin/browscap-php browscap:update
If you want to autoupdate the used cache, we recommend a separate cron job that calls the command listed above.
- [BC] The @no-named-arguments annotation was added from BrowscapPHP\BrowscapInterface#setFormatter()
- [BC] The @no-named-arguments annotation was added from BrowscapPHP\BrowscapInterface#setParser()
- [BC] The @no-named-arguments annotation was added from BrowscapPHP\BrowscapInterface#getParser()
- [BC] The @no-named-arguments annotation was added from BrowscapPHP\BrowscapInterface#getBrowser()
- [BC] The @no-named-arguments annotation was added from BrowscapPHP\Formatter\FormatterInterface#setData()
- [BC] The @no-named-arguments annotation was added from BrowscapPHP\Formatter\FormatterInterface#getData()
- [BC] The @no-named-arguments annotation was added from BrowscapPHP\IniParser\ParserInterface#createIniParts()
- [BC] The @no-named-arguments annotation was added from BrowscapPHP\IniParser\ParserInterface#createPatterns()
- [BC] The @no-named-arguments annotation was added from BrowscapPHP\Parser\ParserInterface#getBrowser()
- [BC] The @no-named-arguments annotation was added from BrowscapPHP\Parser\Helper\GetDataInterface#getSettings()
- [BC] The @no-named-arguments annotation was added from BrowscapPHP\Parser\Helper\GetPatternInterface#getPatterns()
- [BC] The @no-named-arguments annotation was added from BrowscapPHP\BrowscapUpdaterInterface#convertFile()
- [BC] The @no-named-arguments annotation was added from BrowscapPHP\BrowscapUpdaterInterface#convertString()
- [BC] The @no-named-arguments annotation was added from BrowscapPHP\BrowscapUpdaterInterface#fetch()
- [BC] The @no-named-arguments annotation was added from BrowscapPHP\BrowscapUpdaterInterface#update()
- [BC] The @no-named-arguments annotation was added from BrowscapPHP\BrowscapUpdaterInterface#checkUpdate()
- [BC] The @no-named-arguments annotation was added from BrowscapPHP\Cache\BrowscapCacheInterface#getVersion()
- [BC] The @no-named-arguments annotation was added from BrowscapPHP\Cache\BrowscapCacheInterface#getReleaseDate()
- [BC] The @no-named-arguments annotation was added from BrowscapPHP\Cache\BrowscapCacheInterface#getType()
- [BC] The @no-named-arguments annotation was added from BrowscapPHP\Cache\BrowscapCacheInterface#getItem()
- [BC] The @no-named-arguments annotation was added from BrowscapPHP\Cache\BrowscapCacheInterface#setItem()
- [BC] The @no-named-arguments annotation was added from BrowscapPHP\Cache\BrowscapCacheInterface#hasItem()
- [BC] The @no-named-arguments annotation was added from BrowscapPHP\Cache\BrowscapCacheInterface#removeItem()
- [BC] The @no-named-arguments annotation was added from BrowscapPHP\Cache\BrowscapCacheInterface#flush()
- [BC] The @no-named-arguments annotation was added from BrowscapPHP\Helper\IniLoaderInterface#setRemoteFilename()
- [BC] The @no-named-arguments annotation was added from BrowscapPHP\Helper\IniLoaderInterface#getRemoteIniUrl()
- [BC] The @no-named-arguments annotation was added from BrowscapPHP\Helper\IniLoaderInterface#getRemoteTimeUrl()
- [BC] The @no-named-arguments annotation was added from BrowscapPHP\Helper\IniLoaderInterface#getRemoteVersionUrl()
- [BC] The @no-named-arguments annotation was added from BrowscapPHP\Helper\ConverterInterface#setFilesystem()
- [BC] The @no-named-arguments annotation was added from BrowscapPHP\Helper\ConverterInterface#convertFile()
- [BC] The @no-named-arguments annotation was added from BrowscapPHP\Helper\ConverterInterface#convertString()
- [BC] The @no-named-arguments annotation was added from BrowscapPHP\Helper\ConverterInterface#getIniVersion()
- [BC] The @no-named-arguments annotation was added from BrowscapPHP\Helper\ConverterInterface#setVersion()
- [BC] The @no-named-arguments annotation was added from BrowscapPHP\Helper\ConverterInterface#storeVersion()
- [BC] The @no-named-arguments annotation was added from BrowscapPHP\Helper\SupportInterface#getUserAgent()
- [BC] The @no-named-arguments annotation was added from BrowscapPHP\Helper\QuoterInterface#pregQuote()
- [BC] The @no-named-arguments annotation was added from BrowscapPHP\Helper\QuoterInterface#pregUnQuote()
- [BC] The parameter $content of BrowscapPHP\IniParser\ParserInterface#createPatterns() changed from no type to a non-contravariant string
- [BC] The parameter $content of BrowscapPHP\IniParser\ParserInterface#createPatterns() changed from no type to string
- [BC] The parameter $content of BrowscapPHP\IniParser\IniParser#createPatterns() changed from no type to a non-contravariant string
- [BC] The parameter $filename of BrowscapPHP\Helper\Filesystem#dumpFile() changed from no type to a non-contravariant string
- [BC] The parameter $filename of BrowscapPHP\Helper\Filesystem#dumpFile() changed from no type to string
- [BC] Constant BrowscapPHP\Data\PropertyHolder::TYPE_IN_ARRAY was removed
- [BC] Method BrowscapPHP\Data\PropertyHolder#checkValueInArray() was removed
- [BC] Method BrowscapPHP\Parser\Helper\GetDataInterface#__construct() was removed
- [BC] Method BrowscapPHP\Parser\Helper\GetPatternInterface#__construct() was removed
- [BC] Method BrowscapPHP\Cache\BrowscapCacheInterface#__construct() was removed
$fileCache = new \League\Flysystem\Local\LocalFilesystemAdapter($cacheDir);
$filesystem = new \League\Flysystem\Filesystem($fileCache);
$cache = new \MatthiasMullie\Scrapbook\Psr16\SimpleCache(
new \MatthiasMullie\Scrapbook\Adapters\Flysystem($filesystem)
);
$logger = new \Monolog\Logger('name');
$bc = new \BrowscapPHP\Browscap($cache, $logger);
$result = $bc->getBrowser();
NOTE: You may use any other cache which implements the PSR-16 interface.
$bc = new \BrowscapPHP\BrowscapUpdater();
$bc->update(\BrowscapPHP\Helper\IniLoaderInterface::PHP_INI_FULL);
If you are behind a proxy or need a spcific configuration, you have to set up a client instance. See into the Guzzle documentation for more information about this.
$proxyConfig = [
'proxy' => [
'http' => 'tcp://localhost:8125',
'https' => 'tcp://localhost:8124',
],
];
$client = new \GuzzleHttp\Client($proxyConfig);
$bcu = new BrowscapUpdater();
$bcu->setClient($client);
$bc = new \BrowscapPHP\Browscap();
$current_browser = $bc->getBrowser();
$bc = new \BrowscapPHP\Browscap($cache, $logger);
$current_browser = $bc->getBrowser($the_user_agent);
NOTE: If you don't want to use a file cache, you could not use the CLI commands. It is not possible to use other caches there at the moment. NOTE: Each operation (fetch, update, check-update) which fetches data from the remote host browscap.org may run into the rate limit of that site. If this happens an Exception is thrown.
Each CLI command returns zero
if everything went fine.
If you only want to check if a new version of the browscap.ini is available, you can use the check-update
command.
vendor/bin/browscap-php browscap:check-update
cache
(optional) the relative path to your cache directory
- 1: no cached version found
- 2: no new version availble
- 3: an error occured while checking the cached version
- 4: an error occured while fetching the remote version
- 5: an other error occured
The fetch
command downloads an ini file from browscap.org.
vendor/bin/browscap-php browscap:fetch
cache
(optional) the relative path to your cache directoryremote-file
(optional) only required if you dont want to download the standerd file, possible values arePHP_BrowscapINI
downloads the standard file (default)Lite_PHP_BrowscapINI
downloads the lite fileFull_PHP_BrowscapINI
downloads the full file
file
(optional) the relative path to the local file where the remote content is stored
- 3: an error occured while checking the cached version
- 9: an error occured while fetching the remote data
- 10: an other error occured
The convert
command reads a local stored browscap.ini file and writes the contents into a cache.
vendor/bin/browscap-php browscap:convert
file
(optional) the relative path to the local file where the remote content is stored, this should be the same file as in the fetch commandcache
(optional) the relative path to your cache directory
- 6: the name of the file to convert is missing
- 7: the file to convert is not available or not readable
- 8: an other error occured while reading the file
The update
command downloads an ini file from browscap.org and writes the contents into a cache. No local files are created.
vendor/bin/browscap-php browscap:update
remote-file
(optional) only required if you dont want to download the standerd file, possible values arePHP_BrowscapINI
downloads the standard file (default)Lite_PHP_BrowscapINI
downloads the lite fileFull_PHP_BrowscapINI
downloads the full file
cache
(optional) the relative path to your cache directory
- 3: an error occured while checking the cached version
- 9: an error occured while fetching the remote data
- 10: an other error occured
The parse
command parses a given user agent and writes the result to the console.
vendor/bin/browscap-php browscap:parse
user-agent
(required) the user agent which should be parsedcache
(optional) the relative path to your cache directory
- 11: an other error occured while parsing the useragent
Note: Both ways to create/update the cache will use the standard
mode file as default.
If you want more detailed information you may change this with the remote-file
option.
Please use the help function this parameter.
vendor/bin/browscap-php browscap:update --remote-file Full_PHP_BrowscapINI
Each operation expect fetch uses a cache inside the resources
directory inside the project. If you update this library with
composer, the cache is cleared also. If you want to avoid this and want to set your own cache folder,
you can use the cache
option. If you do this, you have to set a Cache Instance for this this path.
vendor/bin/browscap-php browscap:update --cache ./browscap-cache
Please report your issues and ask for new features on the GitHub Issue Tracker at https://github.com/browscap/browscap-php/issues
Please report incorrectly identified User Agents and browser detect in the browscap.ini file here: https://github.com/browscap/browscap/issues