topoff/user-agent-parser

正确解析用户代理

v6.0.0 2021-11-24 15:53 UTC

README

Build Status Code Coverage Scrutinizer Code Quality

Latest Stable Version Latest Unstable Version License Total Downloads

用户代理解析一直是件痛苦的事情。

本包的目标是通过提供许多用户代理解析器的抽象层,使其变得不那么痛苦。

目前提供 11 个本地提供者6 个 HTTP 提供者在此处查看比较列表

因此您可以

  • 同时使用多个提供者,使用 Chain 提供者
  • 同时使用本地和/或 HTTP API 提供者
  • 在解析器之间切换,而无需更改您的代码
  • 比较不同解析器的结果
  • 无论您当前使用哪个解析器,始终获得相同的模型结果

目前本包的质量由以下内容保证

  • 单元测试(373 测试,746 断言)
  • 集成测试(86 测试,310 断言)
  • 常规真实结果测试(超过 33,000 个用户代理的结果在此处比较这里

试试看

实时测试

比较解析器的检测结果

安装

目前使用 composer 是安装此包的唯一支持方式。

composer require thadafinser/user-agent-parser

注意:要使用本地提供者,您需要安装额外的包,这些包列在 composer 的 suggests 部分中

入门

您需要注册一个 API 密钥或安装一个额外的包(在 composer.jsonsuggest 部分列出)

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, ...
}

提供者

UserAgentParser 内置本地和 HTTP 提供者

在此处查看详细文档

本地提供者

HTTP 提供者

本地提供者

本地提供者通常比 HTTP 提供者更快,并且不需要有效的互联网连接。但您需要不时自行更新它们,以确保检测到最新的用户代理。

  • BrowscapFull
  • BrowscapLite
  • BrowscapPhp
  • DonatjUAParser
  • Endorphin
  • HandsetDetection
  • JenssegersAgent
  • MatomoDeviceDetector
  • SinergiBrowserDetector
  • UAParser
  • WhichBrowser
  • Woothee
  • Zsxsoft

HTTP 提供者(API)

HTTP 提供者使用简单,因为您只需 API 密钥即可开始。但它们需要(始终)有效的互联网连接。

  • Http\DeviceAtlasCom
  • Http\FiftyOneDegreesCom
  • Http\NeutrinoApiCom
  • Http\UdgerCom
  • Http\UserAgentApiCom
  • Http\WhatIsMyBrowserCom

比较矩阵

这是一个比较矩阵,包含许多已分析的 UserAgent 字符串,以帮助您确定哪个提供者符合您的需求。每个提供者都有其优势和劣势,因此您应该根据自己的需求选择。

前往比较

概述

单个提供者

require 'vendor/autoload.php';

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\MatomoDeviceDetector();

/* @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();

链式提供者

这非常有用,可以提高您的结果。链式提供者从第一个提供者开始,检查是否有结果,如果没有,则获取下一个,依此类推。如果没有一个提供者有结果,它将抛出一个 NoResultException,就像单个提供者一样。

require 'vendor/autoload.php';

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\MatomoDeviceDetector(),
    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();