PHP code example of tomkyle / matomo-api-client

1. Go to this page and download the library: Download tomkyle/matomo-api-client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tomkyle / matomo-api-client example snippets


use tomkyle\MatomoApiClient\MatomoApiClient;
use GuzzleHttp\Psr7\Uri;

// Initialize the client
$uri = new Uri('https://your-matomo-domain.com');
$defaults = ['token_auth' => 'your-api-token'];
$client = new MatomoApiClient($uri, $defaults);

// Send a request
$params = ['idSite' => '1', 'period' => 'day', 'date' => 'today'];
$response = $client->request($params, 'VisitsSummary.get');

print_r($response);

/**
 * @param  array<string,string>  $params API params
 * @return array<string|int,mixed> API result
 */
public function request(array $params, string $method = null): array;

use tomkyle\MatomoApiClient\RetryingMatomoApiClient;

// Maximum 3 attempts, 5 seconds wait
$retryingClient = new RetryingMatomoApiClient($client, 3, 5); 

// Send a request
$params = ['idSite' => '1', 'period' => 'day', 'date' => 'today'];
$response = $retryingClient->request($params, 'VisitsSummary.get');

use tomkyle\MatomoApiClient\Psr6CacheMatomoApiClient;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;

$cachePool = new FilesystemAdapter();
$cachingClient = new Psr6CacheMatomoApiClient($cachePool, $client);

$response = $cachingClient->request($params, 'VisitsSummary.get');