vietstars / client-detect
Desktop/mobile user agent parser with support for Laravel, based on Mobiledetect
Requires
- php: ^8.0
- jaybizzle/crawler-detect: ^1.2
- mobiledetect/mobiledetectlib: ^2.7.6
Requires (Dev)
- php-coveralls/php-coveralls: ^2.1
- phpunit/phpunit: ^5.0|^6.0|^7.0
README
Installation
Install using composer:
composer require vietstars/client-detect
Laravel (optional)
Add the service provider in config/app.php
:
Vietstars\ClientDetect\ClientServiceProvider::class,
And add the Client alias to config/app.php
:
'Client' => Vietstars\ClientDetect\Facades\Client::class,
Basic Usage
Start by creating an Client
instance (or use the Client
Facade if you are using Laravel):
use Vietstars\ClientDetect\Client; $client = new Client();
If you want to parse user agents other than the current request in CLI scripts for example, you can use the setUserAgent
and setHttpHeaders
methods:
$client->setUserAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.13+ (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2'); $client->setHttpHeaders($headers);
All of the original Mobile Detect methods are still available, check out some original examples at https://github.com/serbanghita/Mobile-Detect/wiki/Code-examples
Is?
Check for a certain property in the user agent.
$client->is('Windows'); $client->is('Firefox'); $client->is('iPhone'); $client->is('OS X');
Magic is-method
Magic method that does the same as the previous is()
method:
$client->isAndroidOS(); $client->isNexus(); $client->isSafari();
Mobile detection
Check for mobile device:
$client->isMobile(); $client->isTablet();
Match user agent
Search the user agent with a regular expression:
$client->match('regexp');
Additional Functionality
Accept languages
Get the browser's accept languages. Example:
$languages = $client->languages(); // ['nl-nl', 'nl', 'en-us', 'en']
Device name
Get the device name, if mobile. (iPhone, Nexus, AsusTablet, ...)
$device = $client->device();
Operating system name
Get the operating system. (Ubuntu, Windows, OS X, ...)
$platform = $client->platform();
Browser name
Get the browser name. (Chrome, IE, Safari, Firefox, ...)
$browser = $client->browser();
Desktop detection
Check if the user is using a desktop device.
$client->isDesktop();
This checks if a user is not a mobile device, tablet or robot.
Phone detection
Check if the user is using a phone device.
$client->isPhone();
Robot detection
Check if the user is a robot. This uses jaybizzle/crawler-detect to do the actual robot detection.
$client->isRobot();
Robot name
Get the robot name.
$robot = $client->robot();
Browser/platform version
MobileDetect recently added a version
method that can get the version number for components. To get the browser or platform version you can use:
$browser = $client->browser(); $version = $client->version($browser); $platform = $client->platform(); $version = $client->version($platform);
Note, the version method is still in beta, so it might not return the correct result.