0

This code should pull from a JSON file, and display the values in a table, looping for each IP instance the API returns. Instead of displaying the entries in the table, I'm just getting a blank table. Any ideas?

This is using the Vultr API, and I masked my SUBID and API Key with X's, if you're wondering what that is. I know this method works, as I used it before with the same API. My theory is that my problem is stemming from the JSON being returned with the [].

I have the following JSON coming in:

{"30953157":[{"ip":"207.148.0.160","netmask":"255.255.254.0","gateway":"207.148.0.1","type":"main_ip","reverse":"brazos.cwservernetwork.com"},{"ip":"144.202.69.201","netmask":"255.255.254.0","gateway":"144.202.68.1","type":"secondary_ip","reverse":"brazos.cwservernetwork.com"}]}

PHP:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>List IP</title>

<meta http-equiv="Content-Style-Type" content="text/css" />
</head>

<body>

    &nbsp;&nbsp;&nbsp;&nbsp;

    <table width="1800px">
        <tr>
            <td colspan="6"><h1><b><u>Listing IPv4</h1></u></b></td>
        </tr>
        <tr>
            <td  style="text-align:center"><b>IPv4</b></td>
            <td  style="text-align:center"><b>Netmask</b></td>
            <td  style="text-align:center"><b>Gateway</b></td>
            <td  style="text-align:center"><b>Type</b></td>
            <td  style="text-align:center"><b>Reverse</b></td>       
        </tr>

    <?php
    $myVultrApiKey="XXXXXXXXXXXXXXXXXXXXXX"; 

    $loadingtime = time();

    $json = file_get_contents("https://api.vultr.com/v1/server/list_ipv4?api_key=$myVultrApiKey&SUBID=XXXXXXXXX");
    $data = json_decode($json);
print $json;

    foreach ($data as $name => $value)  
    { 
    ?>          
            <tr>                    
            <td  style="text-align:center"><?php echo $value->ip; ?></td>
            <td  style="text-align:center"><?php echo $value->netmask; ?></td>
            <td  style="text-align:center"><?php echo $value->gateway; ?></td>
            <td  style="text-align:center"><?php echo $value->type; ?></td>
            <td  style="text-align:center"><?php echo $value->reverse; ?></td>

    <?php
    }//end for  

    ?>
</tr>
<tr>
        <td colspan="2">
                <br><br><br><br><br>
        <?php echo "Load Time: " . "<font color=\"green\">" . (time() - $loadingtime) . "s </font><br />\n"; ?>
        </td>                       
        </tr>
    </table>     

</body>
</html>
theduck
  • 2,589
  • 13
  • 17
  • 23
cwhite
  • 15
  • 3

2 Answers2

0

what causes the issue is the "30953157" at the beginning of the JSON string.

Try

foreach ($data->{30953157} as $name => $value) { ... }
  • That is great! One thing, that string of numbers can change, but I think I can sub in a variable there. – cwhite Dec 04 '19 at 20:06
0

The problem is that the data you are after is 1 level down in the data. Displaying the data should give you something like...

stdClass Object
(
    [30953157] => Array
        (
            [0] => stdClass Object

As I'm not sure if the 30953157 is fixed, a fix is to convert it to an array and use array_values() to remove this key and then just access element [0]...

foreach (array_values((array)$data)[0] as $name => $value)

should do it.

Or from https://stackoverflow.com/a/18526290/1213708

foreach (reset($data) as $name => $value)
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55