Piwik API集成

时间:2015-05-29 13:53:24

标签: matomo

我正在尝试编写一些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行的

错误:文档为空 下面是第一个错误的页面呈现。

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

感谢

1 个答案:

答案 0 :(得分: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;