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

<?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!

  • 写回答

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:

    <?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>");
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?