2014-09-29 122 views
1

我在使用Piwik跟蹤Drupal 7站點上的分析。 Piwik模塊安裝並完美追蹤。我遇到的問題是使用PHP方法進行報告。當我使用Piwik文檔中的示例代碼時,它會導致一個錯誤,導致整個頁面顯示爲純文本HTML /源代碼,而不是渲染的網站。但是,在Drupal之外的基本PHP文件上使用代碼很好。在Drupal 7中查詢Piwik

這是Piwik文檔中的代碼(我與修改,包括我的身份驗證令牌):

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

如果我改變定義的常量爲真,這則表明有錯誤的頁面:

會話已經開始通過session.auto啓動或在session_start()

的Drupal的.htaccess已經有auto.start關閉,我也試圖改變Piwik會話數據存儲在數據庫中,而不是文件,但都沒有成功。

只是FYI,代碼被放置到節點模板(node - 183.tpl.php)中,以覆蓋單個頁面的輸出。

感謝您的幫助!

+0

'這是Piwik docs'的代碼=>您是否有該文檔的鏈接?我不明白爲什麼有'FrontController :: getInstance() - > init();' – 2014-10-07 04:32:04

+0

Nevermind我發現它:http://developer.piwik.org/guides/querying-the-reporting-api#call- -piwik-api-in-php這實際上就是你可以在Piwik應用程序中使用的代碼。 – 2014-10-07 05:15:25

回答

0

如果您在Piwik項目中,您正在使用的代碼有效。

如果你想從你的Drupal應用程序調用Piwik,你可以use the 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>"); 
} 
+0

謝謝。我目前使用的是HTTP API,但速度稍慢,所以我只是好奇,是否有方法在Drupal中使用PHP方法。但是,似乎這是不可能的。感謝您的檢查。 – 2014-10-08 04:55:47