Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. UAParserService
  2. |
  3. |_ composer.json
  4. |_ Vendor
  5. |_ index.php
  6. |_ custom_apps.yml
  7. |_ CustomAppParser.php
  8. |_ DeviceDetector.php
  9.  
  10. <?php
  11.  
  12. require_once "vendor/autoload.php";
  13.  
  14. use DeviceDetectorDeviceDetector;
  15. use DeviceDetectorParserClientCustomAppParser;
  16.  
  17. $userAgent = "MyApp/1.0.0 (Linux; Android 9; ONEPLUS A6010)"; // Android App
  18.  
  19. $dd = new DeviceDetector($userAgent);
  20. $parser = new CustomAppParser();
  21. $dd -> addClientParser($parser);
  22. $dd -> parse();
  23.  
  24. // Check if user agent is a bot
  25. $isBot = $dd -> isBot();
  26.  
  27. if($isBot) {
  28. echo json_encode(["is_bot" => $isBot]);
  29. }
  30. else {
  31. $clientInfo = $dd->getClient();
  32. $osInfo = $dd->getOs();
  33. $device = $dd->getDeviceName();
  34. $brand = $dd->getBrandName();
  35. $model = $dd->getModel();
  36.  
  37. echo json_encode([
  38. "is_bot" => $isBot,
  39. "client_info" => $clientInfo,
  40. "os_info" => $osInfo,
  41. "device_type" => $device,
  42. "device_brand" => $brand,
  43. "device_model" => $model,
  44. ], JSON_PRETTY_PRINT);
  45. }
  46.  
  47. <?php
  48.  
  49.  
  50. namespace UAParserServiceDeviceDetector;
  51.  
  52. use function array_pop;
  53. use function array_unshift;
  54.  
  55. class DeviceDetector extends DeviceDetectorDeviceDetector
  56. {
  57. public function addClientParser($parser){
  58. parent::addClientParser($parser);
  59.  
  60. $item = array_pop($this -> clientParsers);
  61. array_unshift($this -> clientParsers, $item);
  62. }
  63. }
  64.  
  65. <?php
  66.  
  67.  
  68. namespace DeviceDetectorParserClient;
  69.  
  70.  
  71. class CustomAppParser extends ClientParserAbstract
  72. {
  73. protected $fixtureFile = "custom_apps.yml";
  74. protected $parserName = "mobile app";
  75.  
  76. protected function getRegexesDirectory()
  77. {
  78. return dirname(__DIR__);
  79. }
  80. }
  81.  
  82. {
  83. "require": {
  84. "piwik/device-detector": "3.11.7",
  85. "ext-json": "*"
  86. }
  87. }
  88.  
  89. "autoload": {
  90. "psr-4": {
  91. "UAParserService\": "src/"
  92. }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement