我正在使用Piwik跟踪我的Drupal 7网站上的分析。Piwik模块的安装和跟踪非常完美。我遇到的问题是使用PHP方法进行报告。当我使用Piwik文档中的示例代码时,它会导致一个错误,导致整个页面显示为纯文本HTML/source,而不是呈现的网站。但是,在Drupal之外的一个基本PHP文件上使用代码很好。
这是Piwik docs的代码(我用它进行了修改以包含我的auth令牌):
<?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;如果我将定义的常量更改为true,则会显示一个有错误的页面:
会话已经由session.auto-start或session_start()启动
Drupal的.htaccess已经关闭了auto.start,我还尝试了用change Piwik将会话数据存储在DB中而不是文件中,但两者都不起作用。
只有FYI,代码被放置到一个节点模板(节点-183.tpl.php)中,以覆盖单个页面的输出。
谢谢你的帮助!
发布于 2014-10-07 05:14:52
如果您在Piwik项目中,您使用的代码可以工作。
如果您想从您的Drupal应用程序中调用Piwik,可以使用HTTP API。下面是文档中的代码示例:
<?php
// this token is used to authenticate your API request.
// You can get the token on the API page inside your Piwik interface
$token_auth = 'anonymous';
// we call the REST API and request the 100 first keywords for the last month for the idsite=7
$url = "http://demo.piwik.org/";
$url .= "?module=API&method=Referrers.getKeywords";
$url .= "&idSite=7&period=month&date=yesterday";
$url .= "&format=PHP&filter_limit=20";
$url .= "&token_auth=$token_auth";
$fetched = file_get_contents($url);
$content = unserialize($fetched);
// case error
if (!$content) {
print("Error, content fetched = " . $fetched);
}
print("<h1>Keywords for the last month</h1>");
foreach ($content as $row) {
$keyword = htmlspecialchars(html_entity_decode(urldecode($row['label']), ENT_QUOTES), ENT_QUOTES);
$hits = $row['nb_visits'];
print("<b>$keyword</b> ($hits hits)<br>");
}https://stackoverflow.com/questions/26091232
复制相似问题