PHP code example of api-analytics / php

1. Go to this page and download the library: Download api-analytics/php 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/ */

    

api-analytics / php example snippets




piAnalytics\Core\Config;
use ApiAnalytics\PHP\Analytics;

$analytics = new Analytics('your-api-key');

// Start timing
$startTime = microtime(true);

// Your API logic here
$response = ['message' => 'Hello World!'];
$statusCode = 200;

// Calculate response time in milliseconds
$responseTimeMs = (int) round((microtime(true) - $startTime) * 1000);

// Log the request
$analytics->log($_SERVER, $responseTimeMs, $statusCode);

// Send response
header('Content-Type: application/json');
http_response_code($statusCode);
echo json_encode($response);

use ApiAnalytics\Core\Config;
use ApiAnalytics\PHP\Analytics;

$config = new Config();

// Custom IP extraction
$config->setGetIpAddress(function (array $ctx) {
    return $ctx['HTTP_X_REAL_IP'] ?? $ctx['REMOTE_ADDR'] ?? null;
});

// Custom user ID from API key header
$config->setGetUserId(function (array $ctx) {
    return $ctx['HTTP_X_API_KEY'] ?? null;
});

$analytics = new Analytics('your-api-key', $config);

$config = new Config();
$config->privacyLevel = 2;

$config = new Config();
$config->serverUrl = 'https://your-server.com/';

$analytics = new Analytics('your-api-key', $config);
bash
composer