I'm using Piwik to track analytics on my Drupal 7 site. The Piwik module is installed and tracking perfectly. The problem I'm having is with reporting using the PHP method. When I use the sample code from the Piwik docs, it causes an error that causes the entire page to show as plain text HTML/source, instead of the rendered website. However, using the code on a basic PHP file outside of Drupal works fine.
This is the code from Piwik docs (which I modified with to include my auth token):
<?php
use Piwik\API\Request;
use Piwik\FrontController;
define('PIWIK_INCLUDE_PATH', realpath('../..'));
define('PIWIK_USER_PATH', realpath('../..'));
define('PIWIK_ENABLE_DISPATCH', false);
define('PIWIK_ENABLE_ERROR_HANDLER', false);
define('PIWIK_ENABLE_SESSION_START', false);
// if you prefer not to include 'index.php', you must also define here PIWIK_DOCUMENT_ROOT
// and include "libs/upgradephp/upgrade.php" and "core/Loader.php"
require_once PIWIK_INCLUDE_PATH . "/index.php";
require_once PIWIK_INCLUDE_PATH . "/core/API/Request.php";
FrontController::getInstance()->init();
// This inits the API Request with the specified parameters
$request = new Request('
module=API
&method=UserSettings.getResolution
&idSite=7
&date=yesterday
&period=week
&format=XML
&filter_limit=3
&token_auth=anonymous
');
// Calls the API and fetch XML data back
$result = $request->process();
echo $result;
If I change the defined constants to true, it then shows a page that has the error:
session has already been started by session.auto-start or session_start()
Drupal's .htaccess already has the auto.start turned off, and I also tried change Piwik to store session data in the DB instead of files, but neither worked.
Just FYI, the code was placed into a node template (node--183.tpl.php) in order to override the output of a single page.
Thanks for any help!