ua-parser / uap-php
A multi-language port of Browserscope's user agent parser.
Installs: 23 873 078
Dependents: 81
Suggesters: 3
Security: 1
Stars: 521
Watchers: 30
Forks: 84
Open Issues: 11
Requires
- php: ^7.2 || ^8.0
- composer/ca-bundle: ^1.1
Requires (Dev)
- phpstan/phpstan: ^0.12.33
- phpunit/phpunit: ^8 || ^9
- symfony/console: ^3.4 || ^4.2 || ^4.3 || ^5.0
- symfony/filesystem: ^3.4 || ^4.2 || ^4.3 || ^5.0
- symfony/finder: ^3.4 || ^4.2 || ^4.3 || ^5.0
- symfony/yaml: ^3.4 || ^4.2 || ^4.3 || ^5.0
- vimeo/psalm: ^3.12
Suggests
- symfony/console: Required for CLI usage - ^3.4 || ^4.3 || ^5.0
- symfony/filesystem: Required for CLI usage - ^3.4 || ^4.3 || ^5.0
- symfony/finder: Required for CLI usage - ^3.4 || ^4.3 || ^5.0
- symfony/yaml: Required for CLI usage - ^3.4 || ^4.3 || ^5.0
- dev-master
- v3.9.14
- v3.9.13
- v3.9.12
- v3.9.11
- v3.9.10
- v3.9.9
- v3.9.8
- v3.9.7
- v3.9.6
- v3.9.5
- v3.9.4
- v3.9.3
- v3.9.2
- v3.9.1
- v3.9.0
- v3.8.9
- v3.8.8
- v3.8.7
- v3.8.6
- v3.8.5
- v3.8.4
- v3.8.3
- v3.8.2
- v3.8.1
- v3.8.0
- v3.7.0
- v3.6.0
- v3.5.0
- v3.4.7
- v3.4.6
- v3.4.5
- v3.4.4
- v3.4.3
- v3.4.2
- v3.4.1
- v3.4.0
- dev-php8
- dev-auto-update-resources
This package is auto-updated.
Last update: 2024-10-29 04:30:57 UTC
README
This is the PHP library for the uap-core project.
Installation
Add ua-parser/uap-php
to the require section of your composer.json
file and run composer update
.
Usage
Straightforward:
require_once 'vendor/autoload.php'; use UAParser\Parser; $ua = "Mozilla/5.0 (Macintosh; Intel Ma..."; $parser = Parser::create(); $result = $parser->parse($ua); print $result->ua->family; // Safari print $result->ua->major; // 6 print $result->ua->minor; // 0 print $result->ua->patch; // 2 print $result->ua->toString(); // Safari 6.0.2 print $result->ua->toVersion(); // 6.0.2 print $result->os->family; // Mac OS X print $result->os->major; // 10 print $result->os->minor; // 7 print $result->os->patch; // 5 print $result->os->patchMinor; // [null] print $result->os->toString(); // Mac OS X 10.7.5 print $result->os->toVersion(); // 10.7.5 print $result->device->family; // Other print $result->toString(); // Safari 6.0.2/Mac OS X 10.7.5 print $result->originalUserAgent; // Mozilla/5.0 (Macintosh; Intel Ma...
Using Your Own Custom regexes.php File
You can use your own regexes.php
file if you've customized the official file. I strongly encourage you to push back any changes you may have so others can benefit. That said, to use your own do the following:
require_once 'vendor/autoload.php'; use UAParser\Parser; $parser = Parser::create("path/to/custom/regexes.php");
Using ua-parser PHP Library from the Command Line
A command line utility is now included with the PHP library. The following commands are supported:
Get Usage Info
Provides simple usage information:
php bin/uaparser
Update the regexes.php File
Fetches an updated YAML file for ua-parser
and overwrites the current regexes.php file. You can use the following as part of a cron job that runs nightly.
php bin/uaparser ua-parser:update [--no-backup]
By default creates a backup file. Use --no-backup
to turn that feature off.
Convert an Existing regexes.yaml File to regexes.php
With the change to v2.0 you may have an existing and customized YAML file for ua-parser
. Use the following to convert it to JSON.
php bin/uaparser ua-parser:convert [file]
Grab Just the Latest regexes.yaml File From the Repository
If you need to add a new UA it's easier to edit the original YAML and then convert it to JSON.
php bin/uaparser ua-parser:fetch [file]
Fetches an updated YAML file. Warning: This method overwrites any existing regexes.yaml
file.
Parse a Single User Agent String
Parses a user agent string and dumps the results as a list.
php bin/uaparser ua-parser:parse "your user agent string"
Parse a Webserver Log File
Parses the supplied log file or log directory to test ua-parser. Saves the UA to a file when the UA or OS family aren't recognized or when the UA is listed as a generic smartphone or as a generic feature phone.
php bin/uaparser ua-parser:logfile [-f /path/to/logfile] [-d /path/to/logdir] [--include "*.gz"] [--exclude "*.gz"] errors.log
Multiple --include
and --exclude
parameters are allowed.
Credits
Thanks to the original ua-parser team for making the YAML file available for others to build upon.
Also, many thanks to the following major contributors to the PHP library:
- Bryan Shelton
- Michael Bond
- @rjd22
- Timo Tijhof
- Marcus Bointon
- Ryan Parman
- Pravin Dahal
Licensing
- The library is licensed under the MIT license
- The user agents data from the ua-parser project is licensed under the Apache license
- The initial list of generic feature phones & smartphones came from Mobile Web OSP under the MIT license
- The initial list of spiders was taken from Yiibu's profile project under the MIT license.