devicedetect / devicedetectionlib
这是一个设备检测-识别库
v1.0.5
2019-10-07 19:58 UTC
Requires
- php: ^7.2
- mobiledetect/mobiledetectlib: ^2.8
Requires (Dev)
- mockery/mockery: ^1.0
- nunomaduro/collision: ^3.0
- phpunit/phpunit: ^8.0
README
关于
Device Detect 是一个轻量级的 PHP 类,用于检测设备。它基于 mobiledetect 库。它通过结合 User-Agent 字符串和特定的 HTTP 头来识别设备(浏览器、操作系统等)。
安装
作为一个composer 包
composer require devicedetect/devicedetectionlib
或在 composer.json
文件中包含依赖
{ "require": { "devicedetect/devicedetectionlib": "1.0.*" } }
用法
<?php /** * Retrieve the User-Agent. * @method getUserAgent() * @return string|null The user agent if it's set. */ /** * Retrieve the list of known browsers. * @method getBrowsers() * @return array */ /** * Retrieve the list of operating systems. * @method getOperatingSystems() * @return array */ /** * Retrieve the list of device types. * @method getDeviceTypes() * @return array */ /** * Get device type. * @method getDeviceType($userAgent = null) * @param null $userAgent * @return string */ /** * Get device browser. * @method getBrowser() * @return string */ /** * Retrieve the User-Agent. * @method getOperatingSystem() * @return string */ /** * Mobile device detection. * @method isMobile($userAgent = null) * @param null $userAgent * @return bool */ /** * Tablet detection. * @method isTablet($userAgent = null) * @param null $userAgent * @return bool */ /** * Computer detection * @method isComputer($userAgent = null) * @param null $userAgent * @return bool */ /** * Ipad detection * @method isIpad() * @return bool */ /** * Iphone detection * @method isIphone() * @return bool */ /** * Android Os detection * @method isAndroid() * @return bool */ /** * Get version of os, browser etc * @method getVersion(string $key) * @param string $key * @return float|string */
代码示例
将库注入到你的代码中。根据需要使用。
<?php namespace App\Http\Controllers; use App\Device_Detect; /** * Class DeviceDetectionController * * @package App\Http\Controllers */ class DeviceDetectionController extends Controller { /** * @var Device_Detect */ private $deviceDetect; /** * DeviceDetectionController constructor. * * @param $deviceDetect */ public function __construct(Device_Detect $deviceDetect) { $this->deviceDetect = $deviceDetect; } /** * Device Detection */ public function detect() { print_r($this->deviceDetect->getBrowser()); echo "\n"; print_r($this->deviceDetect->getOperatingSystem()); echo "\n"; print_r($this->deviceDetect->getDeviceType()); } }