How can I cross reference two API's? PHP

How can I cross reference two API's? PHP

in API Development

Posted by: pruocco.5368

pruocco.5368

Q:

I am new to using the GW2 API (or any API for that matter) and I’m starting to get the hang of it. I was able to teach myself how to get basic information using the API and PHP to decode the .json files. I’ve created a table showing our guild’s treasury which looks like the following:


Item       |   Quantity   |   Upgrade (Quantity Needed)
-----------------------------------------------------------------
12134     |   100             |   641 (1000)
12135     |   500             |   365 (750)

.
In it’s simplest form, it only shows the ID numbers for the items and upgrades. This is where I am wondering how to cross reference this data with the data from the items API to display the item names.

Does anybody have any instruction on how to do this?


                            $treasury = file_get_contents('https://api.guildwars2.com/v2/guild/B95A3B40-A764-4648-8EE6-39549E922A99/treasury?access_token=358AC6CB-0596-D64F-88D5-5CFA9AA27AAA273F3C6A-BC9E-47EE-AA28-63565C3EFEEE');
                            $treasury = json_decode($treasury);
                        
                            foreach($treasury as $key => $treasury) {
                                
                                echo '<tr>';
                                echo '<td>'.$treasury -> item_id.'</td>';
                                echo '<td>'.$treasury -> count.'</td>';
                                echo '<td>';
                                    foreach($treasury -> needed_by as $key => $treasury) {
                                        echo $treasury -> upgrade_id.' ('.$treasury -> count.')<br />';
                                    }
                                echo '</td>';
                                
                            }
                            echo '</table>';

(edited by pruocco.5368)

How can I cross reference two API's? PHP

in API Development

Posted by: Anhim.3156

Anhim.3156

All of the items are stored in ‘https://api.guildwars2.com/v2/items’. You can check here

So if I were you I would do something to the effect of.

$myItemUrl = file_get_contents(‘https://api.guildwars2.com/v2/items/12345’);//12345 is the id of the item you want to look at
$myItemUrl = json_decode($myItemUrl);

$itemName = $myItemUrl[“name”];

This way will get you the items one at a time (so multiple calls needed). Or you could use multiple Ids at once with “$myItemUrl = file_get_contents(‘https://api.guildwars2.com/v2/items/?ids=1,2,3’)” In which case you get an array returned, so need to adjust the code accordingly.

p.s: sorry for poor formatting, never been good with forum methods.

(edited by Anhim.3156)

How can I cross reference two API's? PHP

in API Development

Posted by: pruocco.5368

pruocco.5368

Thanks for the reply Anhim, but I don’t think that’s what I’m looking for. I have the data for our guild treasury shown in a table, but I am trying to show the names of the treasury items in the results. The guild treasury API does not have the items names, just their ID. https://wiki.guildwars2.com/wiki/API:2/guild/:id/treasury

Attachments:

(edited by pruocco.5368)

How can I cross reference two API's? PHP

in API Development

Posted by: Anhim.3156

Anhim.3156

You can find all the materials in /v2/items, such as elder wood log and any other items, For example the first few entries would be 18 Slot Thick Leather Pack, carrots, potatoes.

Is this not what you would be looking for? You would get the item id from the treasury API, then launch a new query with this item id into the /v2/items.

Edit:

So if I take your code:



                           $treasury = file_get_contents('https://api.guildwars2.com/v2/guild/B95A3B40-A764-4648-8EE6-39549E922A99/treasury?access_token=358AC6CB-0596-D64F-88D5-5CFA9AA27AAA273F3C6A-BC9E-47EE-AA28-63565C3EFEEE');
                            $treasury = json_decode($treasury);
                            
                            foreach($treasury as $key =&gt; $treasury) {
                                $item_id = $treasury[$key]["id"];
                                $myItemUrl = file_get_contents(‘https://api.guildwars2.com/v2/items/$item_id’);
                                $myItemUrl = json_decode($myItemUrl);
                                $itemName = $myItemUrl[“name”];
                                echo '&lt;tr&gt;';
                                echo '&lt;td&gt;'.$itemName.'&lt;/td&gt;';
                                echo '&lt;td&gt;'.$treasury -&gt; count.'&lt;/td&gt;';
                                echo '&lt;td&gt;';
                                    foreach($treasury -&gt; needed_by as $key =&gt; $treasury) {
                                        echo $treasury -&gt; upgrade_id.' ('.$treasury -&gt; count.')&lt;br /&gt;';
                                    }
                                echo '&lt;/td&gt;';
                                
                            }
                            echo '&lt;/table&gt;';

My changes probably have some buggy behavior since it’s just a quick sketch, and this will be very slow since you are calling the API so many times. I’d recommend getting an array with all the IDs and then simply making one call to the API with all the item IDs.

(edited by Anhim.3156)