Read/Search information inside a Json with PHP

Asked

Viewed 474 times

0

I’m starting now with the most complex queries within PHP. As always I played Wow I decided to learn a little about API with PHP using the Blizzard API that gives me some information.

After a lot of research I was able to pull the information inside a table, but I had a question: using the search of the pets as an example, and if I wanted my list to bring me only the pets with the level higher than 10 (pets collected->Stats->level)

Below is the code of how it is returning a table with all the pets and some information

the full json url is: https://us.api.battle.net/wow/character/Goldrinn/Lokinhaa?fields=pets&locale=pt_BR&apikey=3ps9yqk69mhjhy435m8sq4razamkwy25

<?php


$type = 'character';
$reino = 'Goldrinn';
$char = 'Lokinhaa';
$language = 'pt_BR';
$fields = 'pets';
$api_key = '3ps9yqk69mhjhy435m8sq4razamkwy25';

$url = "https://us.api.battle.net/wow/$type/$reino/$char?fields=$fields&locale=$language&apikey=$api_key"; // path to your JSON file
$data = file_get_contents($url); // put the contents of the file into a variable
$characters = json_decode($data, true); // decode the JSON feed
$pets = $characters['pets']['collected'];

?>

<table>
    <tbody>
        <tr>
            <th>Pet</th>
            <th>Spell</th>
            <th>Level</th>
            <th>Health</th>
            <th>Power</th>
            <th>Speed</th>
        </tr>
        <?php foreach ($pets as $pet) : ?>
        <tr>
            <td> <?php echo $pet['name']; ?> </td>
            <td> <?php echo $pet['spellId']; ?> </td>
            <td> <?php echo $pet['stats']['level']; ?> </td>
            <td> <?php echo $pet['stats']['health']; ?> </td>
            <td> <?php echo $pet['stats']['power']; ?> </td>
            <td> <?php echo $pet['stats']['speed']; ?> </td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>

?>

1 answer

1


This way it will display in your HTML only those with level 10 or higher.

<?php foreach ($pets as $pet) : ?>
<?php if($pet['stats']['level']>=10){ ?>
<tr>
    <td> <?php echo $pet['name']; ?> </td>
    <td> <?php echo $pet['spellId']; ?> </td>
    <td> <?php echo $pet['stats']['level']; ?> </td>
    <td> <?php echo $pet['stats']['health']; ?> </td>
    <td> <?php echo $pet['stats']['power']; ?> </td>
    <td> <?php echo $pet['stats']['speed']; ?> </td>
</tr>
<?php } ?>
<?php endforeach; ?>

This format only works by filtering the data you have already received ( full ), perhaps in the API documentation, you can find a parameter in which you can pass and receive the data already filtered according to your need.

  • Oops! What you said did work! Thank you very much. Now about the "rules" of the API, I searched and they have some very strict parameters in relation to "filters" I can only filter directly by the "specie" API and a few others, but nothing very detailed. But you’ve already given me a light! Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.