Question

Im trying to display some data im receiving from the cloudstack API. I can display some of the data I want when I use the JSON as objects, with the following code;

stdClass Object
(
[listvirtualmachinesresponse] => stdClass Object
    (
        [count] => 2
        [virtualmachine] => Array
            (
                [0] => stdClass Object
                    (
                        [id] => bab03c9b-94e0-429f-906c-8853de306e9a
                        [name] => LinuxL1
                        [displayname] => LinuxL1
                        [account] => admin
                        [domainid] => a3121682-837b-11e3-964c-001372f94016
                        [domain] => ROOT
                        [created] => 2014-05-08T13:43:01+0200
                        [state] => Running
                        [haenable] => 
                        [zoneid] => 7a3ee2f0-540a-4232-8264-50a47a91001d
                        [zonename] => Amsterdam
                        [hostid] => 024b9ca2-4076-488e-ad13-86dfb7f2b766
                        [hostname] => ptvirt6
                        [templateid] => cf6abb42-574f-4840-886a-8662c5914166
                        [templatename] => CentOS 6.5
                        [templatedisplaytext] => good
                        [passwordenabled] => 
                        [isoid] => cf6abb42-574f-4840-886a-8662c5914166
                        [isoname] => CentOS 6.5
                        [isodisplaytext] => good
                        [serviceofferingid] => 4fc30c59-4600-44ed-b0bb-7fc48e795d90
                        [serviceofferingname] => L1
                        [cpunumber] => 1
                        [cpuspeed] => 1000
                        [memory] => 1024
                        [cpuused] => 0.02%
                        [networkkbsread] => 55
                        [networkkbswrite] => 1
                        [diskkbsread] => 1000680
                        [diskkbswrite] => 1301501
                        [diskioread] => 77192
                        [diskiowrite] => 8884
                        [guestosid] => a92508f4-837b-11e3-964c-001372f94016
                        [rootdeviceid] => 0
                        [rootdevicetype] => ROOT
                        [securitygroup] => Array
                            (
                                [0] => stdClass Object
                                    (
                                        [id] => edfe848c-837b-11e3-964c-001372f94016
                                        [name] => default
                                        [description] => Default Security Group
                                        [account] => admin
                                        [ingressrule] => Array
                                            (
                                            )

                                        [egressrule] => Array
                                            (
                                            )

                                        [tags] => Array
                                            (
                                            )

                                    )

                            )

                        [nic] => Array
                            (
                                [0] => stdClass Object
                                    (
                                        [id] => bfaeb76d-7b5a-46b1-aaa7-7dd15e86bf2e
                                        [networkid] => 83007a42-e4b7-4ea2-aa21-  20938ea3bc56  
                                        [networkname] => defaultGuestNetwork
                                        [netmask] => 111.111.111.111
                                        [gateway] => 111.111.111.111
                                        [ipaddress] => 111.111.111.111
                                        [isolationuri] => ec2://untagged
                                        [broadcasturi] => vlan://untagged
                                        [traffictype] => Guest
                                        [type] => Shared
                                        [isdefault] => 1
                                        [macaddress] => 06:a5:24:00:00:0a
                                    )

                            )

                        [hypervisor] => KVM
                        [instancename] => i-2-9-VM
                        [tags] => Array
                            (
                            )

                        [affinitygroup] => Array
                            (
                            )

                        [displayvm] => 1
                        [isdynamicallyscalable] => 
                    )

I can read the name / state from the list with a foreach loop

<?php
$api_content = file_get_contents("http://127.0.0.1:5950/client/api?command=listVirtualMachines&domainid=1&account=admin&response=json");
$api_data = json_decode($api_content);
$ap_machine = $api_data->listvirtualmachinesresponse->virtualmachine;
echo "<pre>";
print_r($api_data);
echo "</pre>";
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>
      <table border="1" width="300">
    <tr>
        <td>Naam</td>
        <td>Status</td>
        <td>Ip Address</td>
        <td>Health</td>
        </tr>
    <?php 
       echo "<tr>";
       foreach($ap_machine as $item){

       echo "<td>".$item->name."</td>";
       echo "<td>".$item->state."</td>";
       echo "<td>".$item->nic->ipaddress."</td>";
       if($item->state == 'Running'){
           echo '<td><img src="/600px-Approve_icon.svg.png" height="50" width="50">      </td>';
       }
       else{
           echo '<td><img src="/fail.png" height="25" width="25"></td>';
       }
      echo "</tr>";
       }

    ?>

I've read online to make an associative array from the json list, I did that aswell and didnt get a result.

Was it helpful?

Solution

In your PHP code you are accessing nice key as a normal element of json:

echo "<td>".$item->nic->ipaddress."</td>";

But, from your var_dump output I can see its of array type.

enter image description here

So, to access elements of nice array, you'l have to access it by specifying array index.

echo "<td>".$item->nic[0]->ipaddress."</td>";

OTHER TIPS

Look dude

change

  $api_data = json_decode($api_content);

to

  $api_data = json_decode($api_content, TRUE);

this makes it an associative array

check http://www.php.net/manual/en/function.json-decode.php

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top