| <?php |
| |
|
|
| |
| |
| $api_keys = [ |
| 'gemini' => 'AIzaSyBRGR_80aOangxTYu45I0S6g_WemsadDnI', |
| 'pexels_images' => 'Ig8Ko8Y2e9TXMYKaopPpVVKtAIh4e1pDD0hQth3gMajaWFggIDlESw0F', |
| 'pexels_videos' => 'Ig8Ko8Y2e9TXMYKaopPpVVKtAIh4e1pDD0hQth3gMajaWFggIDlESw0F' |
| ]; |
|
|
| header('Content-Type: application/json'); |
|
|
| |
| $request_body = file_get_contents('php://input'); |
| $request_data = json_decode($request_body, true); |
|
|
| if (!$request_data || !isset($request_data['service'])) { |
| http_response_code(400); |
| echo json_encode(['error' => 'Requisição inválida.']); |
| exit(); |
| } |
|
|
| $service = $request_data['service']; |
|
|
| |
| switch ($service) { |
| case 'gemini_chat': |
| handle_gemini_chat($request_data, $api_keys['gemini']); |
| break; |
| case 'pexels_image': |
| handle_pexels_search($request_data, $api_keys['pexels_images'], 'https://api.pexels.com/v1/search'); |
| break; |
| case 'pexels_video': |
| handle_pexels_search($request_data, $api_keys['pexels_videos'], 'https://api.pexels.com/videos/search'); |
| break; |
| default: |
| http_response_code(400); |
| echo json_encode(['error' => 'Serviço desconhecido.']); |
| exit(); |
| } |
|
|
| |
| |
| |
| function handle_gemini_chat($data, $api_key) { |
| if (strpos($api_key, 'SUA_CHAVE') !== false || empty($api_key)) { |
| http_response_code(500); |
| echo json_encode(['error' => ['message' => 'A chave da API Gemini não foi configurada ou é inválida no servidor (api_proxy.php).']]); |
| exit(); |
| } |
|
|
| |
| |
| |
| $geminiUrl = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent?key=' . $api_key; |
| |
| |
| $post_data = $data['payload'] ?? []; |
| |
| |
| echo forward_request($geminiUrl, json_encode($post_data), ['Content-Type: application/json']); |
| } |
|
|
| |
| |
| |
| function handle_pexels_search($data, $api_key, $api_url_base) { |
| if (strpos($api_key, 'SUA_CHAVE') !== false || empty($api_key)) { |
| http_response_code(500); |
| echo json_encode(['error' => ['message' => 'A chave da API Pexels não foi configurada ou é inválida no servidor (api_proxy.php).']]); |
| exit(); |
| } |
| |
| $query = $data['query'] ?? 'abstract'; |
| $api_url = $api_url_base . '?query=' . urlencode($query) . '&per_page=1'; |
| |
| echo forward_request($api_url, null, ['Authorization: ' . $api_key]); |
| } |
|
|
|
|
| |
| |
| |
| function forward_request($url, $post_fields = null, $headers = []) { |
| $ch = curl_init(); |
| curl_setopt($ch, CURLOPT_URL, $url); |
| curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
| |
| if ($post_fields) { |
| curl_setopt($ch, CURLOPT_POST, true); |
| curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields); |
| } |
| |
| $response = curl_exec($ch); |
| $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
| |
| if (curl_errno($ch)) { |
| http_response_code(500); |
| |
| return json_encode(['error' => 'Erro de comunicação interna do servidor: ' . curl_error($ch)]); |
| } |
| |
| curl_close($ch); |
| |
| http_response_code($http_code); |
| return $response; |
| } |
| ?> |