-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnim-proxy.php
More file actions
40 lines (33 loc) · 1.42 KB
/
nim-proxy.php
File metadata and controls
40 lines (33 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
/**
* NIM BYOK Proxy — Feed Humanity
*
* Relays NVIDIA NIM API requests from the browser to bypass CORS.
* The user's API key passes through — never stored.
* Upload this file to your web hosting alongside index.html.
*/
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, OPTIONS');
header('Access-Control-Allow-Headers: Authorization, Content-Type');
// Handle preflight
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') { http_response_code(204); exit; }
if ($_SERVER['REQUEST_METHOD'] !== 'POST') { http_response_code(405); echo json_encode(['error' => 'POST only']); exit; }
$auth = $_SERVER['HTTP_AUTHORIZATION'] ?? '';
if (!$auth) { http_response_code(400); echo json_encode(['error' => 'Missing Authorization header']); exit; }
$body = file_get_contents('php://input');
$ch = curl_init('https://integrate.api.nvidia.com/v1/chat/completions');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $body,
CURLOPT_HTTPHEADER => ["Authorization: $auth", 'Content-Type: application/json'],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 60,
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
curl_close($ch);
if ($error) { http_response_code(502); echo json_encode(['error' => "Upstream error: $error"]); exit; }
http_response_code($httpCode);
echo $response;