Skip to content

PHP Proxy

Use a small PHP proxy when you don’t have a full framework but can run PHP on your server.

  1. Create the script proxy

    Create public/cvs.js.php (or configure your web server to route /cvs.js to this file):

    <?php
    header('Content-Type: application/javascript');
    header('Cache-Control: public, max-age=86400');
    $cacheFile = sys_get_temp_dir() . '/usertrax-cvs.js';
    $maxAge = 86400;
    if (file_exists($cacheFile) && (time() - filemtime($cacheFile)) < $maxAge) {
    readfile($cacheFile);
    exit;
    }
    $script = file_get_contents('https://usertrax.io/cvs.js');
    if ($script !== false) {
    file_put_contents($cacheFile, $script);
    echo $script;
    } else {
    http_response_code(502);
    }
  2. Create the events proxy

    Create public/api/usertrax/events.php:

    <?php
    if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    http_response_code(405);
    exit;
    }
    $body = file_get_contents('php://input');
    $headers = [
    'Content-Type: application/json',
    'Accept: application/json',
    'User-Agent: ' . ($_SERVER['HTTP_USER_AGENT'] ?? ''),
    'X-Auth-Key: ' . ($_SERVER['HTTP_X_AUTH_KEY'] ?? ''),
    ];
    $context = stream_context_create([
    'http' => [
    'method' => 'POST',
    'header' => implode("\r\n", $headers),
    'content' => $body,
    'ignore_errors' => true,
    ],
    ]);
    $response = file_get_contents('https://api.usertrax.io/api/events', false, $context);
    if ($response === false) {
    http_response_code(502);
    exit;
    }
    preg_match('/HTTP\/\d\.\d\s+(\d+)/', $http_response_header[0] ?? '', $matches);
    http_response_code((int) ($matches[1] ?? 200));
    header('Content-Type: application/json');
    echo $response;
  3. Configure URL rewriting

    Apache (.htaccess):

    RewriteEngine On
    RewriteRule ^cvs\.js$ cvs.js.php [L]
    RewriteRule ^api/usertrax/events$ api/usertrax/events.php [L]

    Nginx:

    location = /cvs.js {
    fastcgi_pass unix:/var/run/php/php8.4-fpm.sock;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME /path/to/public/cvs.js.php;
    }
    location = /api/usertrax/events {
    fastcgi_pass unix:/var/run/php/php8.4-fpm.sock;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME /path/to/public/api/usertrax/events.php;
    }
  4. Update your HTML

    <script>
    window.usertraxConfig = {
    endpoint: "/api/usertrax/events",
    };
    </script>
    <script src="/cvs.js" data-key="YOUR_API_KEY" defer></script>
  1. Visit your website
  2. Open the Network tab
  3. Confirm requests go through your domain
  • 502 errors: Ensure allow_url_fopen is enabled or use cURL instead of file_get_contents
  • 401 Unauthorized: Verify HTTP_X_AUTH_KEY is passed — some PHP setups require explicit header forwarding in Nginx