| | <?php
|
| | $json_file = 'io.json';
|
| | $headers = getallheaders();
|
| | $auth_header = $headers['Authorization'] ?? '';
|
| | $input = file_get_contents('php://input');
|
| | $data = json_decode($input, true);
|
| | $method = $_SERVER['REQUEST_METHOD'];
|
| | $task_id = $_GET['task_id'] ?? null;
|
| |
|
| |
|
| | $storage = [];
|
| | if (file_exists($json_file)) {
|
| | $storage = json_decode(file_get_contents($json_file), true) ?? [];
|
| | }
|
| |
|
| | if ($method === 'POST' && !$task_id) {
|
| |
|
| | $ch = curl_init('https://ark.ap-southeast.bytepluses.com/api/v3/contents/generations/tasks');
|
| | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
| | curl_setopt($ch, CURLOPT_POST, true);
|
| | curl_setopt($ch, CURLOPT_POSTFIELDS, $input);
|
| | curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
| | 'Content-Type: application/json',
|
| | 'Authorization: ' . $auth_header
|
| | ]);
|
| |
|
| | $response = curl_exec($ch);
|
| | $result = json_decode($response, true);
|
| | $new_task_id = $result['id'] ?? null;
|
| |
|
| | if ($new_task_id) {
|
| | $storage[$new_task_id] = [
|
| | 'created' => time(),
|
| | 'status' => 'processing',
|
| | 'request' => $data,
|
| | 'response' => $result
|
| | ];
|
| | file_put_contents($json_file, json_encode($storage, JSON_PRETTY_PRINT));
|
| | }
|
| |
|
| | echo $response;
|
| |
|
| | } elseif ($method === 'GET' && $task_id) {
|
| |
|
| | $url = "https://ark.ap-southeast.bytepluses.com/api/v3/contents/generations/tasks/{$task_id}";
|
| |
|
| | $ch = curl_init($url);
|
| | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
| | curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
| | 'Authorization: ' . $auth_header
|
| | ]);
|
| |
|
| | $response = curl_exec($ch);
|
| | $result = json_decode($response, true);
|
| |
|
| |
|
| | if (isset($storage[$task_id])) {
|
| | $storage[$task_id]['response'] = $result;
|
| | $storage[$task_id]['status'] = $result['status'] ?? 'unknown';
|
| | $storage[$task_id]['last_poll'] = time();
|
| |
|
| | if ($result['status'] === 'succeeded') {
|
| | $storage[$task_id]['video_url'] = $result['content']['video_url'] ?? null;
|
| | }
|
| |
|
| | file_put_contents($json_file, json_encode($storage, JSON_PRETTY_PRINT));
|
| | }
|
| |
|
| | echo $response;
|
| |
|
| | } elseif ($_SERVER['REQUEST_URI'] === '/proxy/io.json') {
|
| |
|
| | header('Content-Type: application/json');
|
| | echo file_get_contents($json_file);
|
| | }
|
| |
|
| | ?> |