File tree 1 file changed +38
-0
lines changed
Roadmap/20 - PETICIONES HTTP/php
1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change 37
37
38
38
echo "\n\n DIFICULTAD EXTRA \n\n" ;
39
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
+ }
40
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
+ ?>
41
79
You can’t perform that action at this time.
0 commit comments