softwaresystem/matomo-reporting-api

使用guzzlehttp 7.0查询Matomo Reporting API的PHP客户端库

dev-main 2022-05-04 11:46 UTC

This package is not auto-updated.

Last update: 2024-09-19 21:09:02 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服务器之外。