bleuren / agent
Laravel增强型用户代理解析器
v1.0.1
2024-04-27 10:13 UTC
Requires
- php: ^8.3
- jaybizzle/crawler-detect: ^1.2.118
- mobiledetect/mobiledetectlib: ^4.8
Requires (Dev)
- phpunit/phpunit: ^9.0
README
《bleuren/agent》包是Laravel应用程序的增强型用户代理解析器。它提供了检测各种类型设备、浏览器、操作系统和爬虫的功能,使开发者能够根据检测到的环境定制用户体验。
特性
- 设备检测:识别用户设备是桌面、平板还是移动设备。
- 浏览器检测:检测并返回正在使用的浏览器类型。
- 平台检测:确定设备的操作系统。
- 爬虫检测:识别爬虫,将它们与常规用户流量区分开来。
- HTTP头部分析:管理和分析与移动设备和用户代理相关的HTTP头部。
安装
要安装此包,请在Laravel项目中运行以下命令
composer require bleuren/agent
使用方法
设备检测
$isDesktop = Agent::isDesktop(); $isMobile = Agent::isMobile(); $isTablet = Agent::isTablet();
浏览器和平台检测
$browser = Agent::browser(); $platform = Agent::platform();
爬虫检测
// Check if the current user agent is a robot. $isRobot = Agent::isRobot(); // Alternatively, the same result can be achieved with isCrawler() $isCrawler = Agent::isCrawler(); // You can also check for a specific crawler by passing a user agent string. $isSpecificCrawler = Agent::isCrawler('specific-user-agent-string'); // Retrieve the name of the detected robot/crawler, if any. $robot = Agent::robot();
HTTP头部和用户代理管理
Agent::setHttpHeaders(['User-Agent' => $_SERVER['HTTP_USER_AGENT']]); $httpHeaders = Agent::getHttpHeaders(); $mobileHeaders = Agent::getMobileHeaders(); $cloudFrontHeaders = Agent::getCloudFrontHttpHeaders(); Agent::setUserAgent('specific-user-agent-string'); $userAgent = Agent::getUserAgent();
示例
// Determine the device type: 'desktop', 'mobile', 'tablet', or 'robot'. $deviceType = Agent::deviceType(); // Browser and platform detection $browser = Agent::browser(); $platform = Agent::platform(); // Detailed robot detection if (Agent::isRobot()) { $robot = Agent::robot(); }