pfrenssen/piwik-reporting-api

此软件包已被废弃,不再维护。作者建议使用pfrenssen/matomo-reporting-api软件包。

PHP客户端库,用于查询Matomo Reporting API。

3.0.0 2023-10-03 08:35 UTC

This package is auto-updated.

Last update: 2023-10-03 12:35:16 UTC


README

(前Piwik Reporting API)

此库允许使用PHP通过Matomo开源网站分析平台使用Matomo Reporting API获取数据。

用法

<?php

use Matomo\ReportingApi\QueryFactory;

require getcwd() . '/vendor/autoload.php';

// The URL of the Matomo server. *Always use HTTPS!*
$matomo_url = 'https://my.matomo.server';

// The user authentication token. You can get it in the web interface of the
// Matomo server at Administration > Platform > API.
$token = 'e0357ffdf830ca8be8af4151b564b53d';

// Instantiate the query factory. This class helps to quickly generate
// different query objects with reusable default parameters.
$query_factory = QueryFactory::create($matomo_url);

// Set some default parameters such as the site ID and user authentication
// token. 
$query_factory
    ->set('idSite', 1)
    ->set('token_auth', $token);

// Example: retrieve the version of the Matomo server.
$query = $query_factory->getQuery('API.getMatomoVersion');
$response = $query->execute()->getResponse();
$matomo_version = $response->value;

echo "Server is running Matomo $matomo_version.\n\n";

// Example: retrieve browser usage statistics for the past week.
$response = $query_factory->getQuery('DevicesDetection.getBrowsers')
  ->setParameter('date', 'today')
  ->setParameter('period', 'week')
  ->execute()
  ->getResponse();

foreach ($response as $browser) {
  echo "Browser: {$browser->label}\n";
  echo "Total visits: {$browser->nb_visits}\n";
  echo "Bounce count: {$browser->bounce_count}\n";
  echo "Daily unique visitors: {$browser->sum_daily_nb_uniq_visitors}\n\n";
}

有关可用方法的完整列表,请参阅Matomo API参考

安全性

Matomo Reporting API使用用户身份验证令牌来访问API。该API非常强大,允许查看、编辑和删除大部分数据,甚至添加和删除用户。这意味着用户身份验证令牌应该与您的用户名和密码一样保密

由于令牌通过网络发送,因此确保所有发送到Matomo API的网络流量都加密至关重要。您可以在测试期间使用未加密的HTTP URL,但对于生产服务器,强烈建议使用HTTPS,否则攻击者可能会窃取您的凭据并将您锁定在Matomo服务器之外。