2026-02-23 12:58:19 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace MintyPHP\Service\Auth;
|
|
|
|
|
|
|
|
|
|
use MintyPHP\Curl;
|
|
|
|
|
|
|
|
|
|
class OidcHttpGateway
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* @param array<string, string> $headers
|
|
|
|
|
*/
|
|
|
|
|
public function call(string $method, string $url, mixed $data = '', array $headers = []): array
|
|
|
|
|
{
|
2026-05-28 13:14:19 +02:00
|
|
|
return $this->callWithStream(strtoupper($method), $url, $data, $headers);
|
2026-02-23 12:58:19 +01:00
|
|
|
}
|
2026-05-28 11:45:52 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array<string, string> $headers
|
|
|
|
|
*/
|
2026-05-28 13:14:19 +02:00
|
|
|
private function callWithStream(string $method, string $url, mixed $data, array $headers): array
|
2026-05-28 11:45:52 +02:00
|
|
|
{
|
|
|
|
|
$headerLines = [];
|
|
|
|
|
foreach ($headers as $key => $value) {
|
|
|
|
|
$headerLines[] = $key . ': ' . $value;
|
|
|
|
|
}
|
2026-05-28 13:14:19 +02:00
|
|
|
|
|
|
|
|
$httpContext = [
|
|
|
|
|
'method' => $method,
|
|
|
|
|
'header' => implode("\r\n", $headerLines),
|
|
|
|
|
'ignore_errors' => true,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if ($method === 'POST') {
|
|
|
|
|
$body = is_array($data) ? http_build_query($data) : (string) $data;
|
|
|
|
|
$httpContext['content'] = $body;
|
|
|
|
|
if (!isset($headers['Content-Type'])) {
|
|
|
|
|
$httpContext['header'] .= "\r\nContent-Type: application/x-www-form-urlencoded";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-28 11:45:52 +02:00
|
|
|
$ctx = stream_context_create([
|
2026-05-28 13:14:19 +02:00
|
|
|
'http' => $httpContext,
|
2026-05-28 11:45:52 +02:00
|
|
|
'ssl' => [
|
|
|
|
|
'verify_peer' => true,
|
|
|
|
|
'verify_peer_name' => true,
|
|
|
|
|
],
|
|
|
|
|
]);
|
2026-05-28 13:14:19 +02:00
|
|
|
|
2026-05-28 11:45:52 +02:00
|
|
|
$body = @file_get_contents($url, false, $ctx);
|
|
|
|
|
$status = 0;
|
|
|
|
|
$responseHeaders = [];
|
|
|
|
|
if (isset($http_response_header) && is_array($http_response_header)) {
|
|
|
|
|
foreach ($http_response_header as $i => $line) {
|
|
|
|
|
if ($i === 0 && preg_match('/HTTP\/[\d.]+ (\d+)/', $line, $m)) {
|
|
|
|
|
$status = (int) $m[1];
|
|
|
|
|
} elseif (str_contains($line, ': ')) {
|
|
|
|
|
[$k, $v] = explode(': ', $line, 2);
|
|
|
|
|
$responseHeaders[$k] = $v;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return [
|
|
|
|
|
'status' => $status,
|
|
|
|
|
'headers' => $responseHeaders,
|
|
|
|
|
'data' => is_string($body) ? $body : '',
|
|
|
|
|
'url' => $url,
|
|
|
|
|
];
|
|
|
|
|
}
|
2026-02-23 12:58:19 +01:00
|
|
|
}
|