[ 'method' => 'GET', 'header' => implode("\r\n", [ 'Accept: application/json', 'X-Contractless-Address: ' . $address, 'X-Contractless-Public-Key: ' . $publicKey, 'X-Contractless-Signature: ' . $signature, ]), 'ignore_errors' => true, 'timeout' => 20, ], ]); $body = file_get_contents($baseUrl . $route, false, $context); if ($body === false) { throw new RuntimeException('The API request could not be completed.'); } $responseHeaders = $http_response_header ?? []; $statusLine = (string) ($responseHeaders[0] ?? ''); if (preg_match('/\s(\d{3})\s/', $statusLine, $match) !== 1) { throw new RuntimeException('The API returned an invalid HTTP response.'); } $status = (int) $match[1]; echo "HTTP $status\n"; try { $decoded = json_decode($body, true, flags: JSON_THROW_ON_ERROR); echo json_encode( $decoded, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR, ) . PHP_EOL; } catch (JsonException) { echo $body . PHP_EOL; } exit($status >= 200 && $status < 300 ? 0 : 1); } catch (Throwable $error) { fwrite(STDERR, 'API test failed: ' . $error->getMessage() . PHP_EOL); exit(1); }