A PHP desktop/mobile user agent parser with support for Laravel, based on Mobile Detect with desktop support and additional functionality.
Install using composer:
composer require nxtcode/agent
Add the service provider in config/app.php
:
NxtCode\Service\ServiceServiceProvider::class,
And add the Service alias to config/app.php
:
'Service' => NxtCode\Service\Facades\Service::class,
Start by creating an Service
instance (or use the Service
Facade if you are using Laravel):
use NxtCode\Service\Service;
$tool = new Service();
If you want to parse user agents other than the current request in CLI scripts for example, you can use the setUserService
and setHttpHeaders
methods:
$tool->setUserService('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');
$tool->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
Check for a certain property in the user agent.
$tool->is('Windows');
$tool->is('Firefox');
$tool->is('iPhone');
$tool->is('OS X');
Magic method that does the same as the previous is()
method:
$tool->isAndroidOS();
$tool->isNexus();
$tool->isSafari();
Check for mobile device:
$tool->isMobile();
$tool->isTablet();
Search the user agent with a regular expression:
$tool->match('regexp');
Get the browser's accept languages. Example:
$languages = $tool->languages();
// ['nl-nl', 'nl', 'en-us', 'en']
Get the device name, if mobile. (iPhone, Nexus, AsusTablet, ...)
$device = $tool->device();
Get the operating system. (Ubuntu, Windows, OS X, ...)
$platform = $tool->platform();
Get the browser name. (Chrome, IE, Safari, Firefox, ...)
$browser = $tool->browser();
Check if the user is using a desktop device.
$tool->isDesktop();
This checks if a user is not a mobile device, tablet or robot.
Check if the user is using a phone device.
$tool->isPhone();
Check if the user is a robot. This uses jaybizzle/crawler-detect to do the actual robot detection.
$tool->isRobot();
Get the robot name.
$robot = $tool->robot();
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 = $tool->browser();
$version = $tool->version($browser);
$platform = $tool->platform();
$version = $tool->version($platform);
Note, the version method is still in beta, so it might not return the correct result.
Laravel User Service is licensed under The MIT License (MIT).
To report a security vulnerability, follow these steps.