<?php

declare(strict_types=1);

$base = 'http://karviapi1.de';
$paths = [
    'GET /karvipartner/contracts',
    'GET /api/contracts',
    'GET /api/contracts/testtoken123',
    'GET /karvipartner/contracts/testtoken123',
    'POST /karvipartner/contracts/sign/testtoken123',
    'POST /api/contracts/testtoken123/sign',
];

foreach ($paths as $item) {
    [$method, $path] = explode(' ', $item, 2);
    $url = $base . $path;

    $ch = curl_init($url);
    $opts = [
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_TIMEOUT => 15,
        CURLOPT_HTTPHEADER => ['Accept: application/json', 'Content-Type: application/json'],
    ];
    if ($method === 'POST') {
        $opts[CURLOPT_POST] = true;
        $opts[CURLOPT_POSTFIELDS] = json_encode(['terms_accepted' => true, 'raw_data' => []]);
    } else {
        $opts[CURLOPT_CUSTOMREQUEST] = 'GET';
    }
    curl_setopt_array($ch, $opts);
    $raw = curl_exec($ch);
    $status = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    $snippet = is_string($raw) ? substr(str_replace("\n", ' ', $raw), 0, 120) : '';
    echo sprintf("%-45s HTTP %3d  %s\n", $item, $status, $snippet);
}
