// --- Mostrar un Pokémon aleatorio usando la PokeAPI --- if ($action === 'pokemon') { // Obtener el número total de Pokémon (actualmente 1025 en la PokeAPI) $maxPokemon = 1025; $randomId = rand(1, $maxPokemon); $pokeUrl = "https://pokeapi.co/api/v2/pokemon/{$randomId}"; // Usar cURL para obtener los datos del Pokémon $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $pokeUrl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 15); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; PokeAPI Client)'); $pokeResponse = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($httpCode === 200 && $pokeResponse !== false) { $pokeData = json_decode($pokeResponse, true); if (json_last_error() === JSON_ERROR_NONE) { // Preparar datos relevantes para el frontend $result = [ 'name' => ucfirst($pokeData['name']), 'image' => $pokeData['sprites']['front_default'], 'id' => $pokeData['id'], 'types' => array_map(function($type) { return $type['type']['name']; }, $pokeData['types']) ]; echo json_encode($result, JSON_UNESCAPED_UNICODE); exit; } } sendError('No se pudo obtener el Pokémon aleatorio', 500); }