From ff6068afcd3e8ed94f825adb9e7417192e88c50e Mon Sep 17 00:00:00 2001 From: "ry.yamafuji" Date: Thu, 18 Dec 2025 21:36:21 +0900 Subject: [PATCH] =?UTF-8?q?PHP=E3=82=92=E8=BF=BD=E5=8A=A0=E3=81=99?= =?UTF-8?q?=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/bff/proxy.php | 74 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/bff/proxy.php diff --git a/src/bff/proxy.php b/src/bff/proxy.php new file mode 100644 index 0000000..767cdbd --- /dev/null +++ b/src/bff/proxy.php @@ -0,0 +1,74 @@ + $v) { + if (strtolower($k) === 'host') continue; + $headers[] = "$k: $v"; +} + +// ========================= +// body(非ストリーミング) +// ========================= +$body = null; +if (!in_array($method, ['GET', 'HEAD'])) { + $body = file_get_contents('php://input'); +} + +// ========================= +// 中継実行 +// ========================= +$context = stream_context_create([ + 'http' => [ + 'method' => $method, + 'header' => implode("\r\n", $headers), + 'content' => $body, + 'ignore_errors' => true, // 4xx / 5xx も取得 + ] +]); + +$responseBody = file_get_contents($targetUrl, false, $context); + +// ========================= +// ステータスコード +// ========================= +if (isset($http_response_header[0])) { + if (preg_match('#HTTP/\d\.\d\s+(\d+)#', $http_response_header[0], $m)) { + http_response_code((int)$m[1]); + } +} + +// ========================= +// レスポンスヘッダ +// ========================= +foreach ($http_response_header as $h) { + if (stripos($h, 'Transfer-Encoding:') === 0) continue; + if (stripos($h, 'Connection:') === 0) continue; + header($h, false); +} + +// ========================= +// レスポンスボディ +// ========================= +echo $responseBody;