Piwik API 集成

Piwik API integration

我正在尝试编写一些 php 来从 Piwik 中提取数据。目前我想做的就是将它变成 运行 示例代码,但我做不到。我已经将它安装在一个名为 analytics 的目录中,我的代码是

            <?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 列错误:文档为空 以下是第一个错误之前的页面呈现。

所以它有点工作但不完全。我找不到任何可以帮助我的东西,所以如果你有任何想法,我将不胜感激

谢谢

您需要格式化您的代码。擦除代码的所有回声,最后一个除外。如果混合使用 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;