2015-05-29 108 views
0

我想寫一些PHP從Piwik中提取數據。目前我所要做的就是讓它運行示例代碼,但我不能。我在一個目錄下名爲分析安裝了它,我的代碼是Piwik API集成

  <?php 
      use Piwik\API\Request; 
      use Piwik\FrontController; 
      echo "result script loaded"; 

      define('PIWIK_INCLUDE_PATH', realpath('../../')."/httpdocs/analytics/"); 
      define('PIWIK_USER_PATH', realpath('../../'."/httpdocs/analytics/")); 
      define('PIWIK_ENABLE_DISPATCH', false); 
      define('PIWIK_ENABLE_ERROR_HANDLER', false); 
      define('PIWIK_ENABLE_SESSION_START', false); 

      echo "<br/>1 PIWIK_INCLUDE_PATH: ".PIWIK_INCLUDE_PATH; 

      // 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=Resolution.getResolution 
         &idSite=all 
         &date=last4 
         &period=month 
         &format=XML 
         &filter_limit=3 
         &token_auth=anonymous 
      '); 
      // Calls the API and fetch XML data back 
      echo "<br/>here"; 

      $result = $request->process(); 
      echo $result; 
      ?> 

這將運行,但產生

此頁面包含以下錯誤:第1欄第1行

錯誤:文件空 下面是頁面渲染到第一個錯誤。

所以它工作一點但不完全。我找不到任何東西來幫助我,所以如果您有任何想法,我會很感激

感謝

回答

0

您需要格式化你的代碼。清除代碼的所有回顯,除最後一個外。如果混合使用echo和XML,則XML輸出將不正確。我不知道在你的真實文件中,每行之前是否有這些空白,但在這種情況下,也要刪除它們。

因此,您的代碼應該是這樣的:

<?php 
use Piwik\API\Request; 
use Piwik\FrontController; 

define('PIWIK_INCLUDE_PATH', realpath('../../')."/httpdocs/analytics/"); 
define('PIWIK_USER_PATH', realpath('../../'."/httpdocs/analytics/")); 
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=Resolution.getResolution 
      &idSite=all 
      &date=last4 
      &period=month 
      &format=XML 
      &filter_limit=3 
      &token_auth=anonymous 
'); 
// Calls the API and fetch XML data back 
$result = $request->process(); 
echo $result; 
+0

感謝偉大的工作 – user1616338