Skip to content

Commit edf9f4b

Browse files
committed
#20 - php
Terminado
1 parent dadd0cb commit edf9f4b

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

Roadmap/20 - PETICIONES HTTP/php/miguelex.php

+38
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,43 @@
3737

3838
echo "\n\n DIFICULTAD EXTRA \n\n";
3939

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+
}
4065

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+
?>
4179

0 commit comments

Comments
 (0)