Files
api/services/nginx/app/modules/xlvask/classes/xlvask_request.php
T
Jeppe B 6d888a455d Automate XL-Vask invoice-period resolution (#340)
Deploy the revision-aware XL-Vask import and guarded autopilot infrastructure. Automatic actions remain fail-closed pending production readiness, calibration, dry-run, and canary gates.
2026-08-03 15:33:55 +02:00

80 lines
3.0 KiB
PHP

<?php
namespace xlvask\classes;
require_once WD . '/modules/xlvask/interfaces/xlvask_request_i.php';
use classes\slack;
use xlvask\interfaces\xlvask_request_i;
class xlvask_request implements xlvask_request_i
{
/**
* @inheritDoc
*/
public function sendRequest(string $url, string $method = 'GET', array $data = [], array $headers = []): array
{
// Initialize cURL
$ch = curl_init();
$method = strtoupper($method);
// Set the URL
curl_setopt($ch, CURLOPT_URL, $url . ($method === 'GET' && !empty($data) ? '?' . http_build_query($data) : ''));
// Set the HTTP method
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
// Set the data to send with the request
if ($method !== 'GET') {
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$headers[] = 'Content-Type: application/json';
};
// Set the headers
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Verify the upstream certificate and keep requests bounded.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
// Execute the request
$response = curl_exec($ch);
// Check for errors
if (curl_errno($ch)) {
throw new \Exception('cURL error: ' . curl_error($ch));
}
// Get the HTTP status code
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// Close the cURL session
curl_close($ch);
// Check if the response is successful
if ($httpCode < 200 || $httpCode >= 300) {
$slack = new slack();
$urlParts = parse_url($url);
$safeEndpoint = (string)($urlParts['host'] ?? 'unknown-host') . (string)($urlParts['path'] ?? '');
$slack->send_message(
'XLVask API Request Failed',
"Endpoint: {$safeEndpoint}\nMethod: {$method}\nHTTP Code: {$httpCode}\n"
. 'Request fields: ' . count($data) . "\nResponse bytes: " . strlen((string)$response)
);
throw new \Exception('Request failed with status code ' . $httpCode);
}
// Check if the response is valid JSON
$responseData = json_decode($response, true);
if (json_last_error() !== JSON_ERROR_NONE) {
// Check if the response is empty (which is valid)
if ($response === '') {
return [];
}
throw new \Exception('Invalid JSON response: ' . json_last_error_msg());
}
// Return the response data
return $responseData;
}
/**
* @inheritDoc
*/
public function generateBasicAuthHeader(string $username, string $password): string
{
return 'Authorization: Basic ' . base64_encode($username . ':' . $password);
}
}