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
// 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.
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');
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');
Related
i have a file structure like this in my PHP 7.1
Model/Abstract.php
Model/NTLStm.php
Model/SoapCl.php
Controller.php
i Called Model/Abstract.php in Controller like this:
use Model/Abstract as modelAbstract;
$abstract = new modelAbstract();
i tried to include the Model/NTLStm.php & Model/SoapCl.php in Model/Abstract.php like this:
defined('DS') OR define('DS', DIRECTORY_SEPARATOR);
require_once dirname(__FILE__) . DS . 'SoapCl.php';
require_once dirname(__FILE__) . DS . 'NTLStm.php';
echo 'success';
but it seems it always terminate the process in require_once , i already tried to put try catch like this:
try{
defined('DS') OR define('DS', DIRECTORY_SEPARATOR);
require_once dirname(__FILE__) . DS . 'SoapCl.php';
require_once dirname(__FILE__) . DS . 'NTLStm.php';
} catch(\Exception $e){
echo $e->getMessage();
}
but it won't print anything
Try using parentheses with require_once... like this:
require_once (dirname(__FILE__) . DS . 'SoapCl.php');
require_once (dirname(__FILE__) . DS . 'NTLStm.php');
I am pretty new to PHP so this may be a dumb question but yeah. I'm trying to get information about a user on instagram with this library: https://github.com/postaddictme/instagram-php-scraper
I've added the library and also the Unirest library so that it should work?
I also have this code in my index.php file:
<?php
require_once dirname(__FILE__) . '/Unirest/Exception.php';
require_once dirname(__FILE__) . '/Unirest/Method.php';
require_once dirname(__FILE__) . '/Unirest/Response.php';
require_once dirname(__FILE__) . '/Unirest/Request.php';
require_once dirname(__FILE__) . '/Unirest/Request/Body.php';
require_once dirname(__FILE__) . '/InstagramScraper/Instagram.php';
require_once dirname(__FILE__) . '/InstagramScraper/Endpoints.php';
require_once dirname(__FILE__) . '/InstagramScraper/Model/Account.php';
require_once dirname(__FILE__) . '/InstagramScraper/Model/Comment.php';
require_once dirname(__FILE__) . '/InstagramScraper/Model/Location.php';
require_once dirname(__FILE__) . '/InstagramScraper/Model/Media.php';
require_once dirname(__FILE__) . '/InstagramScraper/Model/Tag.php';
require_once dirname(__FILE__) . '/InstagramScraper/Exception/InstagramException.php';
require_once dirname(__FILE__) . '/InstagramScraper/Exception/InstagramAuthException.php';
require_once dirname(__FILE__) . '/InstagramScraper/Exception/InstagramNotFoundException.php';
use InstagramScraper\Instagram;
$account = Instagram::getAccountById(272308256);
echo $account->username;
?>
But this code does not want to run :/. I keep getting this error:
Fatal error: Using $this when not in object context in
C:\xampp\htdocs\Instagramposts\InstagramScraper\Instagram.php on line 313
I mean if it's a finished library it should immediately work? Anybody's got a solution?
You need to create an instance of the class (object) first before using the method. The method makes use of the "this" operator which needs an object.
Your code should look like this:
use InstagramScraper\Instagram;
$insta = new Instagram;
$account = $insta->getAccountById(272308256);
echo $account->username;
I'm trying to include this file in my symfony2 project which is a file with a bunch of require statements. Unfortunately this, file doesn't contain a class. I don't want to manually write namespaces for all the included file that do contain the classes that I need so I was wondering how I can include this file in a way that it will also include the other files that I need. The file that I'm trying to include into symfony2 looks like this :
<?php
// Tested on PHP 5.2, 5.3
// This snippet (and some of the curl code) due to the Facebook SDK.
if (!function_exists('curl_init')) {
throw new Exception('Stripe needs the CURL PHP extension.');
}
if (!function_exists('json_decode')) {
throw new Exception('Stripe needs the JSON PHP extension.');
}
if (!function_exists('mb_detect_encoding')) {
throw new Exception('Stripe needs the Multibyte String PHP extension.');
}
// Stripe singleton
require(dirname(__FILE__) . '/Stripe/Stripe.php');
// Utilities
require(dirname(__FILE__) . '/Stripe/Util.php');
require(dirname(__FILE__) . '/Stripe/Util/Set.php');
// Errors
require(dirname(__FILE__) . '/Stripe/Error.php');
require(dirname(__FILE__) . '/Stripe/ApiError.php');
require(dirname(__FILE__) . '/Stripe/ApiConnectionError.php');
require(dirname(__FILE__) . '/Stripe/AuthenticationError.php');
require(dirname(__FILE__) . '/Stripe/CardError.php');
require(dirname(__FILE__) . '/Stripe/InvalidRequestError.php');
// Plumbing
require(dirname(__FILE__) . '/Stripe/Object.php');
require(dirname(__FILE__) . '/Stripe/ApiRequestor.php');
require(dirname(__FILE__) . '/Stripe/ApiResource.php');
require(dirname(__FILE__) . '/Stripe/SingletonApiResource.php');
require(dirname(__FILE__) . '/Stripe/List.php');
// Stripe API Resources
require(dirname(__FILE__) . '/Stripe/Account.php');
require(dirname(__FILE__) . '/Stripe/Card.php');
require(dirname(__FILE__) . '/Stripe/Charge.php');
require(dirname(__FILE__) . '/Stripe/Customer.php');
require(dirname(__FILE__) . '/Stripe/Invoice.php');
require(dirname(__FILE__) . '/Stripe/InvoiceItem.php');
require(dirname(__FILE__) . '/Stripe/Plan.php');
require(dirname(__FILE__) . '/Stripe/Token.php');
require(dirname(__FILE__) . '/Stripe/Coupon.php');
require(dirname(__FILE__) . '/Stripe/Event.php');
require(dirname(__FILE__) . '/Stripe/Transfer.php');
require(dirname(__FILE__) . '/Stripe/Recipient.php');
I found stripe finally on packagist :)
{
"require": {
"stripe/stripe-php": "1.8.*"
}
}
Yes! With composer, and you can call Stripe functions where you want like this:
\Stripe::setApiKey("sk_test_yourkey");
$charge = \Stripe_Charge::create(array(...
I think it would be better creating a service and use it from there.
Hope it helps.
Regards.
We are using a VPS server in our company and I'm trying to install Roundcube webmail interface
But I can't even get to the configuration phase because the set_include_path function doesn't work and the script can't find the required configuration files.
I get an error like "Fatal Error, ini_set/set_include_path function does not work."
I assume some php settings is causing this but I don't which one.
I'd be glad if I could get some help.
Thanks in advance
//EDIT Here is the codes from the script
ini_set('error_reporting', E_ALL&~E_NOTICE);
ini_set('display_errors', 1);
define('INSTALL_PATH', realpath(dirname(__FILE__) . '/../').'/');
define('RCMAIL_CONFIG_DIR', INSTALL_PATH . 'config');
$include_path = INSTALL_PATH . 'program/lib' . PATH_SEPARATOR;
$include_path .= INSTALL_PATH . 'program' . PATH_SEPARATOR;
$include_path .= INSTALL_PATH . 'program/include' . PATH_SEPARATOR;
$include_path .= ini_get('include_path');
set_include_path($include_path);
require_once 'utils.php';
require_once 'main.inc';
I'm doing this from memory, so it might not be quite right, but I think maybe you are confusing the path and directory separators. There may also be a nicer way to do this than what you are doing (i.e. assembling the whole path at once). Try something like this:
define('INSTALL_PATH', dirname(dirname(__FILE__)));
set_include_path(get_include_path() . PATH_SEPARATOR . INSTALL_PATH . DIRECTORY_SEPARATOR . 'program' . DIRECTORY_SEPARATOR . 'lib');
set_include_path(get_include_path() . PATH_SEPARATOR . INSTALL_PATH . DIRECTORY_SEPARATOR . 'program' . DIRECTORY_SEPARATOR . 'include');
set_include_path(get_include_path() . PATH_SEPARATOR . INSTALL_PATH . DIRECTORY_SEPARATOR . 'program');
Usually I compress this a little bit with implode, since DIRECTORY_SEPARATOR is so verbose:
...PATH_SEPARATOR . implode(DIRECTORY_SEPARATOR, Array(INSTALL_PATH, 'program', 'lib'));
I think by (most importantly) changing some of your PATHs to DIRECTORYs, and (possibly) using incremental get_include_path and set_include_path calls, it will be more readable, portable and just might work properly.
I'm just trying to build my first app on PHP Fog but there's a piece of code that doesn't run properly - works fine on localhost and other regular hosts though.
I use a modified version of TinyMVC, this is the code responsible for setting up autoloading:
/* Set include_path for spl_autoload */
set_include_path(get_include_path()
. PATH_SEPARATOR . FRAMEWORK_BASEDIR . 'core' . DS
. PATH_SEPARATOR . FRAMEWORK_BASEDIR . 'libraries' . DS
. PATH_SEPARATOR . FRAMEWORK_APPLICATION . DS . 'controllers' . DS
. PATH_SEPARATOR . FRAMEWORK_APPLICATION . DS . 'models' . DS
);
/* File extensions to include */
spl_autoload_extensions('.php,.inc');
/* Setup __autoload */
$spl_funcs = spl_autoload_functions();
if($spl_funcs === false)
spl_autoload_register();
elseif(!in_array('spl_autoload',$spl_funcs))
spl_autoload_register('spl_autoload');
Basically, it fails at the first class it should load, which is located in "FRAMEWORK_BASEDIR . 'core' . DS". The class filename is "framework_controller.php" and class name is "Framework_Controller" (tried lowercase as well). If I include the class manually it works but fails with autoload.
Here's the error message that I get:
Fatal error: spl_autoload(): Class Framework_Controller could not be loaded in /var/fog/apps/app7396/claudiu.phpfogapp.com/application/controllers/home.php on line 12
Any ideas as to what could the problem be?
I managed to sort it out:
function framework_autoload($className, $extList='.inc,.php') {
$autoload_paths = array (
FRAMEWORK_BASEDIR . 'core' . DS,
FRAMEWORK_BASEDIR . 'libraries' . DS,
FRAMEWORK_APPLICATION . DS . 'controllers' . DS,
FRAMEWORK_APPLICATION . DS . 'models' . DS
);
$ext = explode(',',$extList);
foreach($ext as $x) {
foreach ($autoload_paths as $v) {
$fname = $v . strtolower($className).$x;
if(#file_exists($fname)) {
require_once($fname);
return true;
}
}
}
return false;
}
spl_autoload_register('framework_autoload');
Thanks to another question here on StackOverflow: spl_autoload problem