De acuerdo con su código JSON, el siguiente código debería funcionar:
$response = file_get_contents($uri); $json = json_decode($response); foreach ($json->data as $beer) { echo $beer->name."\n"; }
Si quieres saber cuántas cervezas hay, usa
. count($json->data)
Con varias páginas, el código puede ser el siguiente:
- Cómo aprender desarrollo web en un mes
- Cómo restablecer mis funciones de JavaScript
- Cómo enviar correo desde una página HTML usando PHP
- ¿Por qué alguien debería aprender Vue.JS sobre Angular o React?
- ¿Qué lenguajes de programación son necesarios para crear un sitio web que funcione sin problemas? Back End incluido?
// here we store the beers names $beer_names = array(); // the current page $page = 0; // the maximum number of pages // (we don't know for the moment, so // we set it to 0) $max_pages = 0; do { $page++; // get the data for page $page $response = file_get_contents($uri.'&p='.$page); // decode the data $json = json_decode($response); foreach ($json->data as $beer) { $beer_names []= $beer->name: } // if $max_pages is 0 (ie is not set), we set // it with the given value if ($max_pages == 0) { $max_pages = $json->numberOfPages; } } while ($page < $max_pages); echo implode("\n", $beer_names);