llw llw - 1 month ago 5
HTML Question

How to use an API to generate a table?

There's a game I'm interested in making a widget-type table for on my website.
They have a public API for "

invasions
", located here

I'm looking to generate a table using that information they provide. There are number of websites that already use this API and generate a table with the info, such as this website

I know how to make tables that are generated with MySQL data.

However, I've never made a table that is generated with data from an API.

Can someone get me started?

Answer

Something like this will work. It is up to you to set it up and style it/ structure it as you require it.

    $url = "https://www.toontownrewritten.com/api/invasions";
    $data = json_decode(file_get_contents($url));

    print "<table>";
    foreach ($data->invasions as $title => $inv) {
        print "<tr>";

        print "<td>{$title}</td><td>{$inv->progress}</td><td>{$inv->asOf}</td>";

        print "</tr>";
    }
    print "</table>";

You could alternatively use something like curl to run a http request to fetch the data. It is entirely up to you as to how you implement this.