Closed Niko78 closed 1 year ago
Hi, You can create your own list of bots so as not to break the code for other people who use this library.
create php class for you local repositorty src/components/detector/BotExtend.php
<?php
namespace app/components/detector;
use DeviceDetector\Parser\AbstractBotParser;
class BotExtend extends AbstractBotParser
{
/**
* @var string
*/
protected $fixtureFile = 'regexes/bots-extend.yml';
/**
* @var string
*/
protected $parserName = 'bot-extend';
}
create fixture yaml file src/components/detector/regexes/bots-extend.yml
---
- regex: 'curl'
name: 'CURL'
category: 'Crawler'
url: ''
- regex: 'Wget'
name: 'WGet'
category: 'Crawler'
url: ''
- regex: 'MicrosoftPreview'
name: 'Microsoft Preview'
category: 'Service Agent'
url: ''
<?php
require_once 'vendor/autoload.php';
use DeviceDetector\ClientHints;
use DeviceDetector\DeviceDetector;
use DeviceDetector\Parser\Device\AbstractDeviceParser;
use app\components\detector\BotExtend;
// 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);
$dd->addBotParser(new BotExtend());
// 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();
}
great ! thanks for your detailed explanation.
if you have any questions, write to this topic, we will reopen the issue
hi, I use matomo/device-detector and it works well but I need to filter more UA for my own usage as bots, is there an easy way to do that and to avoid to repush this list on each device-detector update ?
My list is like that by example : MicrosoftPreview curl Wget VLC iCoreService WhatsApp Microsoft Office ms-office
Thanks