6

I want to use php device detector that is part of famous Piwik project, but i can't understand how to include and use the code in my php code? i don't want to use composer.

I wrote:

<?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);

But it doesn't work. i get this error:

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
MahdiY
  • 1,269
  • 21
  • 32
علیرضا
  • 2,434
  • 1
  • 27
  • 33
  • what errors you are getting ? – Kamran May 03 '15 at 07:27
  • @KamranAdil i update the question with error message. – علیرضا May 03 '15 at 07:32
  • Hi - did you get an answer to this? I also want to use piwik device detector without composer and all the other piwik stuff. – Erick May 12 '15 at 05:16
  • 1
    I figured it out. Pretty easy. Grab a copy of master and make a few mods. http://pastebin.com/SXBYaC6B – Erick May 13 '15 at 06:00
  • 1
    thanks @Erick, i can use the piwik device detector with your guide. you can add the Pastebin note here as answer too. i'm happy now :) – علیرضا May 13 '15 at 11:20
  • oh @Erick, i get an error when i change my user-agent: `Fatal error: Class 'DeviceDetector\Parser\Client\Browser\Engine' not found in D:\device-detector-master\Parser\Client\Browser.php on line 250` – علیرضا May 13 '15 at 11:23
  • 1
    Ah, the adds to DeviceDetector.php - put them after the namespace declaration. I added the solution as an answer here. Compare. – Erick May 15 '15 at 04:54

3 Answers3

7
// 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.
Etienne Martin
  • 10,018
  • 3
  • 35
  • 47
Erick
  • 369
  • 3
  • 13
  • i do that. (i use `namespace` before requires) but the error remain. but the error only accrues in some user-agent (such as Konqueror in linux) as you can see in [this image](http://stat-9px.rhcloud.com/asset/piwik-browser.png) – علیرضا May 15 '15 at 10:33
  • 1
    Could be a limitation? A missing regex? I kinda doubt it, but I don't have konq installed anywhere to test. There's a way to update the patterns, but not sure what it is. Maybe that fixes it? – Erick May 18 '15 at 03:32
  • yes i guess that it maybe a missing Regex or something like it. i don't have the browser too but i use [User Agent Switcher](https://addons.mozilla.org/en-US/firefox/addon/user-agent-switcher) Firefox add-on. – علیرضا May 19 '15 at 10:58
  • You may also need to add ```require_once(dirname(__FILE__) . '/Browser/Engine.php');``` to /Parser/Client/Browser.php at the bottom right inside ```if (empty($engine)) {``` in the ```buildEngine``` function. I'll offer an edit to the accepted answer too. – GreatBlakes Aug 04 '15 at 03:11
  • 1
    Just confirmed that you could place ```require_once(dirname(__FILE__) . '/Parser/Client/Browser/Engine.php');``` at the end of Erick's list of ```require_once()``` list at the top of DeviceDetector.php to centralize all edits to this library. It must be placed after the ...ParserAbstract.php files! – GreatBlakes Aug 04 '15 at 03:18
3

For those not using an autoloader, here is a solution that works with Device Detector version 3.10.1 based on Erick's answer for previous versions:

//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');
GreatBlakes
  • 3,971
  • 4
  • 20
  • 28
3

To me I worked well. For DeviceDetector version 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');