Upload proxy.php
Browse files
proxy.php
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?php
|
| 2 |
+
$json_file = 'io.json';
|
| 3 |
+
$headers = getallheaders();
|
| 4 |
+
$auth_header = $headers['Authorization'] ?? '';
|
| 5 |
+
$input = file_get_contents('php://input');
|
| 6 |
+
$data = json_decode($input, true);
|
| 7 |
+
$method = $_SERVER['REQUEST_METHOD'];
|
| 8 |
+
$task_id = $_GET['task_id'] ?? null;
|
| 9 |
+
|
| 10 |
+
// Read storage
|
| 11 |
+
$storage = [];
|
| 12 |
+
if (file_exists($json_file)) {
|
| 13 |
+
$storage = json_decode(file_get_contents($json_file), true) ?? [];
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
if ($method === 'POST' && !$task_id) {
|
| 17 |
+
// CREATE NEW TASK
|
| 18 |
+
$ch = curl_init('https://ark.ap-southeast.bytepluses.com/api/v3/contents/generations/tasks');
|
| 19 |
+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
| 20 |
+
curl_setopt($ch, CURLOPT_POST, true);
|
| 21 |
+
curl_setopt($ch, CURLOPT_POSTFIELDS, $input);
|
| 22 |
+
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
| 23 |
+
'Content-Type: application/json',
|
| 24 |
+
'Authorization: ' . $auth_header
|
| 25 |
+
]);
|
| 26 |
+
|
| 27 |
+
$response = curl_exec($ch);
|
| 28 |
+
$result = json_decode($response, true);
|
| 29 |
+
$new_task_id = $result['id'] ?? null;
|
| 30 |
+
|
| 31 |
+
if ($new_task_id) {
|
| 32 |
+
$storage[$new_task_id] = [
|
| 33 |
+
'created' => time(),
|
| 34 |
+
'status' => 'processing',
|
| 35 |
+
'request' => $data,
|
| 36 |
+
'response' => $result
|
| 37 |
+
];
|
| 38 |
+
file_put_contents($json_file, json_encode($storage, JSON_PRETTY_PRINT));
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
echo $response;
|
| 42 |
+
|
| 43 |
+
} elseif ($method === 'GET' && $task_id) {
|
| 44 |
+
// POLL EXISTING TASK - THIS UPDATES IO.JSON!
|
| 45 |
+
$url = "https://ark.ap-southeast.bytepluses.com/api/v3/contents/generations/tasks/{$task_id}";
|
| 46 |
+
|
| 47 |
+
$ch = curl_init($url);
|
| 48 |
+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
| 49 |
+
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
| 50 |
+
'Authorization: ' . $auth_header
|
| 51 |
+
]);
|
| 52 |
+
|
| 53 |
+
$response = curl_exec($ch);
|
| 54 |
+
$result = json_decode($response, true);
|
| 55 |
+
|
| 56 |
+
// UPDATE STORAGE WITH LATEST STATUS
|
| 57 |
+
if (isset($storage[$task_id])) {
|
| 58 |
+
$storage[$task_id]['response'] = $result;
|
| 59 |
+
$storage[$task_id]['status'] = $result['status'] ?? 'unknown';
|
| 60 |
+
$storage[$task_id]['last_poll'] = time();
|
| 61 |
+
|
| 62 |
+
if ($result['status'] === 'succeeded') {
|
| 63 |
+
$storage[$task_id]['video_url'] = $result['content']['video_url'] ?? null;
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
file_put_contents($json_file, json_encode($storage, JSON_PRETTY_PRINT));
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
echo $response;
|
| 70 |
+
|
| 71 |
+
} elseif ($_SERVER['REQUEST_URI'] === '/proxy/io.json') {
|
| 72 |
+
// DIRECT ACCESS TO JSON
|
| 73 |
+
header('Content-Type: application/json');
|
| 74 |
+
echo file_get_contents($json_file);
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
?>
|