PHP code example of thadafinser / user-agent-parser
1. Go to this page and download the library: Download thadafinser/user-agent-parser 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/ */
thadafinser / user-agent-parser example snippets
use UserAgentParser\Exception\NoResultFoundException;
use UserAgentParser\Provider\WhichBrowser;
$provider = new WhichBrowser();
try {
/* @var $result \UserAgentParser\Model\UserAgent */
$result = $provider->parse('Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.73 Safari/537.36');
} catch (NoResultFoundException $ex){
// nothing found
}
if($result->isBot() === true) {
// if one part has no result, it's always set not null
$result->getBot()->getName();
$result->getBot()->getType();
} else {
// if one part has no result, it's always set not null
$result->getBrowser()->getName();
$result->getBrowser()->getVersion()->getComplete();
$result->getRenderingEngine()->getName();
$result->getRenderingEngine()->getVersion()->getComplete();
$result->getOperatingSystem()->getName();
$result->getOperatingSystem()->getVersion()->getComplete();
$result->getDevice()->getModel();
$result->getDevice()->getBrand();
$result->getDevice()->getType();
$result->getDevice()->getIsMobile();
$result->getDevice()->getIsTouch();
}
// initialisation see Getting started
if($result->isBot() === true) {
// do something special with the bot
}
// initialisation see Getting started
if($result->isMobile() === true) {
// redirect to the the mobile optimized page or suggest the other to download your app
// NOTE mobile means not "phone". It can be any moveable device, e.g. tablet, media player, watch, ...
}
use UserAgentParser\Provider;
$userAgent = 'Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_5 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5';
$provider = new Provider\PiwikDeviceDetector();
/* @var $result \UserAgentParser\Model\UserAgent */
$result = $provider->parse($userAgent);
// optional add all headers, to improve the result further
// $result = $provider->parse($userAgent, getallheaders());
$result->getBrowser()->getName(); // Mobile Safari
$result->getOperatingSystem()->getName(); // iOS
$result->getDevice()->getBrand(); // iPod Touch
$result->getDevice()->getBrand(); // Apple
$result->getDevice()->getType(); // portable media player
$resultArray = $result->toArray();
use UserAgentParser\Provider;
$userAgent = 'Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_5 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5';
$chain = new Provider\Chain([
new Provider\PiwikDeviceDetector(),
new Provider\WhichBrowser(),
new Provider\UAParser(),
new Provider\Woothee(),
new Provider\DonatjUAParser()
]);
/* @var $result \UserAgentParser\Model\UserAgent */
$result = $chain->parse($userAgent);
// optional add all headers, to improve the result further (used currently only by WhichBrowser)
//$result = $chain->parse($userAgent, getallheaders());
$result->getBrowser()->getName(); // Mobile Safari
$result->getOperatingSystem()->getName(); // iOS
$result->getDevice()->getBrand(); // iPod Touch
$result->getDevice()->getBrand(); // Apple
$result->getDevice()->getType(); // portable media player
$resultArray = $result->toArray();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.