ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[TOC] > [github](https://github.com/matomo-org/device-detector) ## 概述 ## 安装 ``` composer require matomo/device-detector ``` ## 示例 ``` require_once 'vendor/autoload.php'; use DeviceDetector\ClientHints; use DeviceDetector\DeviceDetector; use DeviceDetector\Parser\Device\AbstractDeviceParser; // OPTIONAL: Set version truncation to none, so full versions will be returned // By default only minor versions will be returned (e.g. X.Y) // for other options see VERSION_TRUNCATION_* constants in DeviceParserAbstract class AbstractDeviceParser::setVersionTruncation(AbstractDeviceParser::VERSION_TRUNCATION_NONE); $userAgent = $_SERVER['HTTP_USER_AGENT']; // change this to the useragent you want to parse $clientHints = ClientHints::factory($_SERVER); // client hints are optional $dd = new DeviceDetector($userAgent, $clientHints); // OPTIONAL: Set caching method // By default static cache is used, which works best within one php process (memory array caching) // To cache across requests use caching in files or memcache // $dd->setCache(new Doctrine\Common\Cache\PhpFileCache('./tmp/')); // OPTIONAL: Set custom yaml parser // By default Spyc will be used for parsing yaml files. You can also use another yaml parser. // You may need to implement the Yaml Parser facade if you want to use another parser than Spyc or [Symfony](https://github.com/symfony/yaml) // $dd->setYamlParser(new DeviceDetector\Yaml\Symfony()); // OPTIONAL: If called, getBot() will only return true if a bot was detected (speeds up detection a bit) // $dd->discardBotInformation(); // OPTIONAL: If called, bot detection will completely be skipped (bots will be detected as regular devices then) // $dd->skipBotDetection(); $dd->parse(); if ($dd->isBot()) { // handle bots,spiders,crawlers,... $botInfo = $dd->getBot(); } else { $clientInfo = $dd->getClient(); // holds information about browser, feed reader, media player, ... $osInfo = $dd->getOs(); $device = $dd->getDeviceName(); $brand = $dd->getBrandName(); $model = $dd->getModel(); } ``` other ``` $dd->isSmartphone(); $dd->isFeaturePhone(); $dd->isTablet(); $dd->isPhablet(); $dd->isConsole(); $dd->isPortableMediaPlayer(); $dd->isCarBrowser(); $dd->isTV(); $dd->isSmartDisplay(); $dd->isSmartSpeaker(); $dd->isCamera(); $dd->isWearable(); $dd->isPeripheral(); ```