Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- UAParserService
- |
- |_ composer.json
- |_ Vendor
- |_ index.php
- |_ custom_apps.yml
- |_ CustomAppParser.php
- |_ DeviceDetector.php
- <?php
- require_once "vendor/autoload.php";
- use DeviceDetectorDeviceDetector;
- use DeviceDetectorParserClientCustomAppParser;
- $userAgent = "MyApp/1.0.0 (Linux; Android 9; ONEPLUS A6010)"; // Android App
- $dd = new DeviceDetector($userAgent);
- $parser = new CustomAppParser();
- $dd -> addClientParser($parser);
- $dd -> parse();
- // Check if user agent is a bot
- $isBot = $dd -> isBot();
- if($isBot) {
- echo json_encode(["is_bot" => $isBot]);
- }
- else {
- $clientInfo = $dd->getClient();
- $osInfo = $dd->getOs();
- $device = $dd->getDeviceName();
- $brand = $dd->getBrandName();
- $model = $dd->getModel();
- echo json_encode([
- "is_bot" => $isBot,
- "client_info" => $clientInfo,
- "os_info" => $osInfo,
- "device_type" => $device,
- "device_brand" => $brand,
- "device_model" => $model,
- ], JSON_PRETTY_PRINT);
- }
- <?php
- namespace UAParserServiceDeviceDetector;
- use function array_pop;
- use function array_unshift;
- class DeviceDetector extends DeviceDetectorDeviceDetector
- {
- public function addClientParser($parser){
- parent::addClientParser($parser);
- $item = array_pop($this -> clientParsers);
- array_unshift($this -> clientParsers, $item);
- }
- }
- <?php
- namespace DeviceDetectorParserClient;
- class CustomAppParser extends ClientParserAbstract
- {
- protected $fixtureFile = "custom_apps.yml";
- protected $parserName = "mobile app";
- protected function getRegexesDirectory()
- {
- return dirname(__DIR__);
- }
- }
- {
- "require": {
- "piwik/device-detector": "3.11.7",
- "ext-json": "*"
- }
- }
- "autoload": {
- "psr-4": {
- "UAParserService\": "src/"
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement