2014-09-29 01:05
浏览 55

在Drupal 7中查询Piwik

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):

use Piwik\API\Request;
use Piwik\FrontController;

define('PIWIK_INCLUDE_PATH', realpath('../..'));
define('PIWIK_USER_PATH', realpath('../..'));
define('PIWIK_ENABLE_DISPATCH', 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";


// This inits the API Request with the specified parameters
$request = new Request('
// 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 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!

图片转代码服务由CSDN问答提供 功能建议

我正在使用Piwik跟踪Drupal 7网站上的分析。 Piwik模块已安装并完美跟踪。 我遇到的问题是使用PHP方法进行报告。 当我使用Piwik文档中的示例代码时,会导致错误导致整个页面显示为纯文本HTML /源,而不是呈现的网站。 但是,在Drupal之外的基本PHP文件上使用代码可以正常工作。

这是来自Piwik docs的代码(我修改了它以包含我的身份验证令牌):

use Piwik \ API \ Request; 
use Piwik \ FrontController; 
define('PIWIK_INCLUDE_PATH',realpath('../ ..')); \  ndefine('PIWIK_USER_PATH',realpath('../ ..')); 
 //并包含“libs / upgradephp / upgrade.php”和“core / Loader.php”
require_once PIWIK_INCLUDE_PATH。  “/index.php";
require_once PIWIK_INCLUDE_PATH。  “/ core/API/Request.php";

 $ request = new Request(  '
 module = API 
&amp; method = UserSettings.getResolution 
&amp; idSite = 7 
&amp; date = yesterday 
&amp; period = week 
&amp; format = XML 
&amp; filter_limit = 3  
&amp; token_auth = anonymous 
 $ result = $ request-&gt; process(); 
echo $ result; 
  <  / pre> 


会话已由启动 start或session_start()

Drupal的.htaccess已关闭auto.start,我也尝试更改Piwik将会话数据存储在数据库而不是文件中,但都没有工作。< / p>

仅供参考,代码被放入节点模板(node-183.tpl.php)以覆盖as的输出 单页。


  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douou8954 2014-10-07 05:14

    The code you are using works if you are within the Piwik project.

    If you want to call Piwik from your Drupal application, you can use the HTTP API. Here is the code example from the docs:

    // 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 = "";
    $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>");
    解决 无用
    打赏 举报

相关推荐 更多相似问题