如何在 php 项目中使用 Piwik 设备检测器?
How to use Piwik device detector in php project?
我想使用著名的 Piwik 项目中的 php device detector,但我不明白如何在我的 php 代码中包含和使用该代码?我不想使用作曲家。
我写了:
<?php
include 'DeviceDetector.php';
use DeviceDetector\DeviceDetector;
use DeviceDetector\Parser\Device\DeviceParserAbstract;
$dd = new DeviceDetector($_SERVER['HTTP_USER_AGENT']);
$dd->parse();
$clientInfo = $dd->getClient();
var_dump($clientInfo);
但是没用。我得到这个错误:
Fatal error: Uncaught exception 'Exception' with message 'client parser not found' in D:\DeviceDetector.php:214
Stack trace:
#0 D:\DeviceDetector.php(136): DeviceDetector\DeviceDetector->addClientParser('FeedReader')
#1 D:\index.php(67): DeviceDetector\DeviceDetector->__construct('Mozilla/5.0 (Wi...')
#2 {main}
thrown in D:\DeviceDetector.php on line 214
// I figured it out. Pretty easy. Grab a copy of master and make a few mods.
// At the top of DeviceDetector.php and in this order:
namespace DeviceDetector;
require_once (dirname(__FILE__).'/spyc.php');
require_once (dirname(__FILE__).'/Cache/Cache.php');
require_once (dirname(__FILE__).'/Cache/StaticCache.php');
require_once (dirname(__FILE__).'/Parser/ParserAbstract.php');
require_once (dirname(__FILE__).'/Parser/Bot.php');
require_once (dirname(__FILE__).'/Parser/OperatingSystem.php');
require_once (dirname(__FILE__).'/Parser/VendorFragment.php');
require_once (dirname(__FILE__).'/Parser/Client/ClientParserAbstract.php');
require_once (dirname(__FILE__).'/Parser/Device/DeviceParserAbstract.php');
require_once (dirname(__FILE__).'/Parser/Client/Browser/Engine.php');
// Add as the first line of addClientParser():
require_once (dirname(__FILE__).'/Parser/Client/'.$parser.'.php');
// Add as the first line of addDeviceParser():
require_once (dirname(__FILE__).'/Parser/Device/'.$parser.'.php');
// You'll also have to grab a copy of spyc.php - google it - easy to find.
// That's it. Works awesome. Faster than anything else.
对于那些不使用自动加载器的人,根据 Erick 对先前版本的回答,这里有一个适用于 Device Detector 3.10.1 版的解决方案:
//Same as before, add this to the top of DeviceDetector.php in this order:
namespace DeviceDetector;
require_once(dirname(__FILE__) . '/Cache/Cache.php');
require_once(dirname(__FILE__) . '/Cache/StaticCache.php');
require_once(dirname(__FILE__) . '/Parser/ParserAbstract.php');
require_once(dirname(__FILE__) . '/Parser/BotParserAbstract.php');
require_once(dirname(__FILE__) . '/Parser/Bot.php');
require_once(dirname(__FILE__) . '/Parser/OperatingSystem.php');
require_once(dirname(__FILE__) . '/Parser/VendorFragment.php');
require_once(dirname(__FILE__) . '/Parser/Client/ClientParserAbstract.php');
require_once(dirname(__FILE__) . '/Parser/Device/DeviceParserAbstract.php');
require_once(dirname(__FILE__) . '/Parser/Client/Browser.php');
require_once(dirname(__FILE__) . '/Yaml/Parser.php');
require_once(dirname(__FILE__) . '/Yaml/Spyc.php');
//Same as before, you'll need to find your own copy of spyc.php. Here is how I add it (pulls from a directory above the library):
require_once(realpath(dirname(__FILE__) . '/..') . '/spyc.php');
//Add as the first line of addClientParser():
require_once(dirname(__FILE__) . '/Parser/Client/' . $parser . '.php');
//Add as the first line of addDeviceParser():
require_once(dirname(__FILE__) . '/Parser/Device/' . $parser . '.php');
对我来说,我工作得很好。对于 DeviceDetector 版本 3.7.3:
namespace DeviceDetector;
require_once(dirname(__FILE__) . '/Cache/Cache.php');
require_once(dirname(__FILE__) . '/Cache/StaticCache.php');
require_once(dirname(__FILE__) . '/Parser/ParserAbstract.php');
require_once(dirname(__FILE__) . '/Parser/Bot.php');
require_once(dirname(__FILE__) . '/Parser/OperatingSystem.php');
require_once(dirname(__FILE__) . '/Parser/VendorFragment.php');
require_once(dirname(__FILE__) . '/Parser/Client/ClientParserAbstract.php');
require_once(dirname(__FILE__) . '/Parser/Device/DeviceParserAbstract.php');
require_once(dirname(__FILE__) . '/Parser/Client/Browser/Engine.php');
require_once(dirname(__FILE__) . '/Parser/Client/Browser/Engine/Version.php');
require_once(dirname(__FILE__) . '/Parser/Client/Browser.php');
require_once(dirname(__FILE__) . '/Yaml/Parser.php');
require_once(dirname(__FILE__) . '/Yaml/Spyc.php');
//Same as before, you'll need to find your own copy of spyc.php. Here is how I add it (pulls from a directory above the library):
require_once(realpath(dirname(__FILE__) . '/..') . '/spyc.php');
//Add as the first line of addClientParser():
require_once(dirname(__FILE__) . '/Parser/Client/FeedReader.php');
require_once(dirname(__FILE__) . '/Parser/Client/MobileApp.php');
require_once(dirname(__FILE__) . '/Parser/Client/MediaPlayer.php');
require_once(dirname(__FILE__) . '/Parser/Client/PIM.php');
require_once(dirname(__FILE__) . '/Parser/Client/Browser.php');
require_once(dirname(__FILE__) . '/Parser/Client/Library.php');
//Add as the first line of addDeviceParser():
require_once(dirname(__FILE__) . '/Parser/Device/HbbTv.php');
require_once(dirname(__FILE__) . '/Parser/Device/Console.php');
require_once(dirname(__FILE__) . '/Parser/Device/CarBrowser.php');
require_once(dirname(__FILE__) . '/Parser/Device/Camera.php');
require_once(dirname(__FILE__) . '/Parser/Device/PortableMediaPlayer.php');
require_once(dirname(__FILE__) . '/Parser/Device/Mobile.php');
我想使用著名的 Piwik 项目中的 php device detector,但我不明白如何在我的 php 代码中包含和使用该代码?我不想使用作曲家。
我写了:
<?php
include 'DeviceDetector.php';
use DeviceDetector\DeviceDetector;
use DeviceDetector\Parser\Device\DeviceParserAbstract;
$dd = new DeviceDetector($_SERVER['HTTP_USER_AGENT']);
$dd->parse();
$clientInfo = $dd->getClient();
var_dump($clientInfo);
但是没用。我得到这个错误:
Fatal error: Uncaught exception 'Exception' with message 'client parser not found' in D:\DeviceDetector.php:214
Stack trace:
#0 D:\DeviceDetector.php(136): DeviceDetector\DeviceDetector->addClientParser('FeedReader')
#1 D:\index.php(67): DeviceDetector\DeviceDetector->__construct('Mozilla/5.0 (Wi...')
#2 {main}
thrown in D:\DeviceDetector.php on line 214
// I figured it out. Pretty easy. Grab a copy of master and make a few mods.
// At the top of DeviceDetector.php and in this order:
namespace DeviceDetector;
require_once (dirname(__FILE__).'/spyc.php');
require_once (dirname(__FILE__).'/Cache/Cache.php');
require_once (dirname(__FILE__).'/Cache/StaticCache.php');
require_once (dirname(__FILE__).'/Parser/ParserAbstract.php');
require_once (dirname(__FILE__).'/Parser/Bot.php');
require_once (dirname(__FILE__).'/Parser/OperatingSystem.php');
require_once (dirname(__FILE__).'/Parser/VendorFragment.php');
require_once (dirname(__FILE__).'/Parser/Client/ClientParserAbstract.php');
require_once (dirname(__FILE__).'/Parser/Device/DeviceParserAbstract.php');
require_once (dirname(__FILE__).'/Parser/Client/Browser/Engine.php');
// Add as the first line of addClientParser():
require_once (dirname(__FILE__).'/Parser/Client/'.$parser.'.php');
// Add as the first line of addDeviceParser():
require_once (dirname(__FILE__).'/Parser/Device/'.$parser.'.php');
// You'll also have to grab a copy of spyc.php - google it - easy to find.
// That's it. Works awesome. Faster than anything else.
对于那些不使用自动加载器的人,根据 Erick 对先前版本的回答,这里有一个适用于 Device Detector 3.10.1 版的解决方案:
//Same as before, add this to the top of DeviceDetector.php in this order:
namespace DeviceDetector;
require_once(dirname(__FILE__) . '/Cache/Cache.php');
require_once(dirname(__FILE__) . '/Cache/StaticCache.php');
require_once(dirname(__FILE__) . '/Parser/ParserAbstract.php');
require_once(dirname(__FILE__) . '/Parser/BotParserAbstract.php');
require_once(dirname(__FILE__) . '/Parser/Bot.php');
require_once(dirname(__FILE__) . '/Parser/OperatingSystem.php');
require_once(dirname(__FILE__) . '/Parser/VendorFragment.php');
require_once(dirname(__FILE__) . '/Parser/Client/ClientParserAbstract.php');
require_once(dirname(__FILE__) . '/Parser/Device/DeviceParserAbstract.php');
require_once(dirname(__FILE__) . '/Parser/Client/Browser.php');
require_once(dirname(__FILE__) . '/Yaml/Parser.php');
require_once(dirname(__FILE__) . '/Yaml/Spyc.php');
//Same as before, you'll need to find your own copy of spyc.php. Here is how I add it (pulls from a directory above the library):
require_once(realpath(dirname(__FILE__) . '/..') . '/spyc.php');
//Add as the first line of addClientParser():
require_once(dirname(__FILE__) . '/Parser/Client/' . $parser . '.php');
//Add as the first line of addDeviceParser():
require_once(dirname(__FILE__) . '/Parser/Device/' . $parser . '.php');
对我来说,我工作得很好。对于 DeviceDetector 版本 3.7.3:
namespace DeviceDetector;
require_once(dirname(__FILE__) . '/Cache/Cache.php');
require_once(dirname(__FILE__) . '/Cache/StaticCache.php');
require_once(dirname(__FILE__) . '/Parser/ParserAbstract.php');
require_once(dirname(__FILE__) . '/Parser/Bot.php');
require_once(dirname(__FILE__) . '/Parser/OperatingSystem.php');
require_once(dirname(__FILE__) . '/Parser/VendorFragment.php');
require_once(dirname(__FILE__) . '/Parser/Client/ClientParserAbstract.php');
require_once(dirname(__FILE__) . '/Parser/Device/DeviceParserAbstract.php');
require_once(dirname(__FILE__) . '/Parser/Client/Browser/Engine.php');
require_once(dirname(__FILE__) . '/Parser/Client/Browser/Engine/Version.php');
require_once(dirname(__FILE__) . '/Parser/Client/Browser.php');
require_once(dirname(__FILE__) . '/Yaml/Parser.php');
require_once(dirname(__FILE__) . '/Yaml/Spyc.php');
//Same as before, you'll need to find your own copy of spyc.php. Here is how I add it (pulls from a directory above the library):
require_once(realpath(dirname(__FILE__) . '/..') . '/spyc.php');
//Add as the first line of addClientParser():
require_once(dirname(__FILE__) . '/Parser/Client/FeedReader.php');
require_once(dirname(__FILE__) . '/Parser/Client/MobileApp.php');
require_once(dirname(__FILE__) . '/Parser/Client/MediaPlayer.php');
require_once(dirname(__FILE__) . '/Parser/Client/PIM.php');
require_once(dirname(__FILE__) . '/Parser/Client/Browser.php');
require_once(dirname(__FILE__) . '/Parser/Client/Library.php');
//Add as the first line of addDeviceParser():
require_once(dirname(__FILE__) . '/Parser/Device/HbbTv.php');
require_once(dirname(__FILE__) . '/Parser/Device/Console.php');
require_once(dirname(__FILE__) . '/Parser/Device/CarBrowser.php');
require_once(dirname(__FILE__) . '/Parser/Device/Camera.php');
require_once(dirname(__FILE__) . '/Parser/Device/PortableMediaPlayer.php');
require_once(dirname(__FILE__) . '/Parser/Device/Mobile.php');