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>
6
  • Have you tried true as the second parm on the decode? Commented Dec 4, 2019 at 19:07
  • @JasonK OP is using the correct notation for an Object, so that isn't the problem Commented Dec 4, 2019 at 19:14
  • Can you show us the output of print_r($data);? Commented Dec 4, 2019 at 19:15
  • I'm guessing your loop should be foreach($data['30953157'] as $name => $value) Commented Dec 4, 2019 at 19:17
  • Jason: Correct me if I'm wrong, it doesn't have to have that param unless its false. Commented Dec 4, 2019 at 20:11

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

1 Comment

This is perfect! Thank you!
0

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

Try

foreach ($data->{30953157} as $name => $value) { ... }

1 Comment

That is great! One thing, that string of numbers can change, but I think I can sub in a variable there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.