hexydec/agentzero 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

hexydec/agentzero

Composer 安装命令:

composer require hexydec/agentzero

包简介

A zero knowledge library for extracting information from User-Agent strings

README 文档

README

A library for extracting information from User-Agent strings very fast.

Licence: MIT Tests Status Code Coverage

Description

Doesn't match full UA strings or patterns, instead it extracts and categorises features from UA strings, so is faster, and can handle new UA strings or variations of common UA patterns.

Most User-Agent detection libraries rely on lists of regular expressions to match user agent string patterns and extract information. With lots of patterns you can do an ok job of getting this info, but it is not dynamic enough, which leads to minor variations or new UA strings not being captured, and they tend to be quite slow.

AgentZero is a simple string matching library that splits the user agent strings up into tokens to extract information from each part, leading to more complete information, more flexibility in capturing new user agent strings, and better performance.

You can try out AgentZero online at https://hexydec.com/apps/user-agent-parser/, or run the supplied index.php file after installation.

Usage

To use AgentZero:

$ua = $_SERVER['HTTP_USER_AGENT']; // or whatever UA you want e.g:
$ua = 'Mozilla/5.0 (Linux; Android 13; Pixel 7 Pro Build/TD1A.220804.031; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/116.0.0.0 Mobile Safari/537.36 Instagram 301.1.0.33.110 Android (33/13; 420dpi; 1080x2116; Google/google; Pixel 7 Pro; cheetah; cheetah; en_GB; 517986703)';
if (($browser = \hexydec\agentzero\agentzero::parse($ua)) !== false) {
	// use $browser
}

parse() returns false if the string is empty, longer than 2000 characters, or produces no recognisable tokens. Always check the return value before using the object. The returned value will be something like:

\hexydec\agentzero\agentzero (

	public readonly string $string => string 'Mozilla/5.0 (Linux; Android 13; Pixel 7 Pro Build/TD1A.220804.031; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/116.0.0.0 Mobile Safari/537.36 Instagram 301.1.0.33.110 Android (33/13; 420dpi; 1080x2116; Google/google; Pixel 7 Pro; cheetah; cheetah; en_GB; 517986703)';

	// categories
	public readonly ?string $type => string 'human';
	public readonly ?string $category => string 'mobile';

	// device
	public readonly ?string $vendor => string 'Google';
	public readonly ?string $device => string 'Pixel';
	public readonly ?string $model => string '7 Pro';
	public readonly ?string $build => string 'TD1A.220804.031';
	public readonly ?int $ram => null;

	// architecture
	public readonly ?string $processor => null;
	public readonly ?string $architecture => null;
	public readonly ?int $bits => null;
	public readonly ?string $cpu => null;
	public readonly ?int $cpuclock => null;

	// platform
	public readonly ?string $kernel => string 'Linux';
	public readonly ?string $platform => string 'Android';
	public readonly ?string $platformversion => string '13';

	// browser
	public readonly ?string $engine => string 'Blink';
	public readonly ?string $engineversion => string '116.0.0.0';
	public readonly ?string $browser => string 'Chrome';
	public readonly ?string $browserversion => string '116.0.0.0';
	public readonly ?string $browserstatus => 'previous';
	public readonly ?string $browserreleased => '2023-09-15';
	public readonly ?string $browserlatest => '133.0.6943.54';
	public readonly ?string $language => string 'en-GB';

	// app
	public readonly ?string $app => string 'Instagram';
	public readonly ?string $appname => string 'Instagram';
	public readonly ?string $appversion => string '301.1.0.33.110';
	public readonly ?string $framework => null;
	public readonly ?string $frameworkversion => null;
	public readonly ?string $url => null;

	// network
	public readonly ?string $nettype => null;
	public readonly ?string $proxy => null;

	// screen
	public readonly ?int $width => int 1080;
	public readonly ?int $height => int 2116;
	public readonly ?int $dpi => int 420;
	public readonly ?float $density => null;
	public readonly ?bool $darkmode => null;
);

You can read the full list of properties here.

Client Hints

AgentZero now supports processing client hints for improved user-agent information. You must request the client hints to improve the information delivered through the user-agent string:

// request client hints
\header('Accept-CH: Width, ECT, Device-Memory, Sec-CH-UA-Platform-Version, Sec-CH-UA-Model, Sec-CH-UA-Full-Version-List');

// retrieve client hints
$hints = \hexydec\agentzero\agentzero::getHints();

// parse
$az = \hexydec\agentzero\agentzero::parse($_SERVER['HTTP_USER_AGENT'], $hints);

Note that by using the Accept-CH header, you may receive client hints on subsequent requests, if you need the client hints on first call, use the Critical-CH header instead (Warning: This will cause an extra round trip as the browser must re-request the first page).

Browser Versions

You can determine the date the browser was released, latest version, and status, by setting where the version file should be cached:

$config = [
	'versionscache' => __DIR__.'/cache/versions.json'
];
$az = \hexydec\agentzero\agentzero::parse($_SERVER['HTTP_USER_AGENT'], [], $config);
var_dump(
	$az->browserstatus,  // "nightly", "canary", "beta", "current", "previous", "outdated" (2+ years old), or "legacy" (5+ years old)
	$az->browserreleased, // date the browser version was released, e.g. "2024-01-15"
	$az->browserlatest   // latest known version number of the browser
);

The browser version information is sourced from my browser versions project.

Supported Features

AgentZero supports a wide range of architectures, browsers, rendering engines, platforms, devices, languages, and crawlers. Access the full list on the Supported Features page.

Installation

The easiest way to get up and running is to use composer:

$ composer require hexydec/agentzero

Test Suite

You can run the test suite like this:

Linux

$ vendor/bin/phpunit

Windows

> vendor\bin\phpunit

Support

AgentZero supports PHP version 8.1+.

Contributing

If you find an issue with AgentZero, please create an issue in the tracker.

If you wish to fix an issue yourself, please fork the code, fix the issue, then create a pull request, and I will evaluate your submission.

Licence

The MIT License (MIT). Please see License File for more information.

hexydec/agentzero 适用场景与选型建议

hexydec/agentzero 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 121.99k 次下载、GitHub Stars 达 40, 最近一次更新时间为 2023 年 08 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「browser」 「user-agents」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 hexydec/agentzero 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 hexydec/agentzero 我们能提供哪些服务?
定制开发 / 二次开发

基于 hexydec/agentzero 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 121.99k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 40
  • 点击次数: 34
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 40
  • Watchers: 4
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-08-02