|
| 1 | +<?php |
| 2 | + |
| 3 | +$ch = curl_init(); |
| 4 | + |
| 5 | +// Configurar la URL a la que se hará la petición |
| 6 | +curl_setopt($ch, CURLOPT_URL, "https://retosdeprogramacion.com/"); |
| 7 | + |
| 8 | +// Configurar cURL para devolver el resultado en lugar de imprimirlo directamente |
| 9 | +curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
| 10 | + |
| 11 | +// Configurar cURL para usar el archivo de certificados descargado |
| 12 | +curl_setopt($ch, CURLOPT_CAINFO, 'cacert.pem'); |
| 13 | + |
| 14 | +// Ejecutar la petición |
| 15 | +$output = curl_exec($ch); |
| 16 | + |
| 17 | +// Verificar si hubo algún error durante la ejecución de la petición |
| 18 | +if($output === false) { |
| 19 | + echo "cURL Error: " . curl_error($ch); |
| 20 | +} else { |
| 21 | + // Obtener el código de estado HTTP de la respuesta |
| 22 | + $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
| 23 | + |
| 24 | + // Mostrar el código de estado HTTP |
| 25 | + echo "HTTP Status Code: " . $http_code . "\n\n"; |
| 26 | + |
| 27 | + // Mostrar el contenido de la respuesta |
| 28 | + echo $output; |
| 29 | +} |
| 30 | + |
| 31 | +// Cerrar la sesión cURL |
| 32 | +curl_close($ch); |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | +// EXTRA |
| 37 | + |
| 38 | +echo "\n\n DIFICULTAD EXTRA \n\n"; |
| 39 | + |
| 40 | +function getPokemonData($pokemon) { |
| 41 | + $data = file_get_contents("https://pokeapi.co/api/v2/pokemon/$pokemon"); |
| 42 | + $data = json_decode($data); |
| 43 | + |
| 44 | + echo "Nombre: " . $data->name . "\n"; |
| 45 | + echo "ID: " . $data->id . "\n"; |
| 46 | + echo "Peso: " . $data->weight . "\n"; |
| 47 | + echo "Altura: " . $data->height . "\n"; |
| 48 | + echo "Tipos: "; |
| 49 | + foreach ($data->types as $type) { |
| 50 | + echo $type->type->name . " "; |
| 51 | + } |
| 52 | + echo "\n"; |
| 53 | + |
| 54 | + $speciesData = file_get_contents($data->species->url); |
| 55 | + $speciesData = json_decode($speciesData); |
| 56 | + |
| 57 | + echo "Cadena de evolución: " . $speciesData->evolution_chain->url . "\n"; |
| 58 | + |
| 59 | + echo "Juegos: "; |
| 60 | + foreach ($data->game_indices as $game) { |
| 61 | + echo $game->version->name . " "; |
| 62 | + } |
| 63 | + echo "\n"; |
| 64 | +} |
| 65 | + |
| 66 | +try { |
| 67 | + while (true) { |
| 68 | + echo "Introduce el nombre o número del Pokémon (o 'salir' para terminar): "; |
| 69 | + $pokemon = trim(fgets(STDIN)); |
| 70 | + if ($pokemon == 'salir') { |
| 71 | + break; |
| 72 | + } |
| 73 | + getPokemonData($pokemon); |
| 74 | + } |
| 75 | +} catch (Exception $e) { |
| 76 | + echo "Ha ocurrido un error: " . $e->getMessage(); |
| 77 | +} |
| 78 | +?> |
| 79 | + |
0 commit comments