0

I have wrote a script that will return JSON output when it gets executed on the shell.

{
  "count": 1,
  "virtualmachine": [
    {
      "account": "admin",
      "affinitygroup": [],
      "cpunumber": 2,
      "cpuspeed": 1000,
      "created": "2015-04-02T12:11:41-0400",
      "details": {
        "hypervisortoolsversion": "xenserver56"
      },
      "diskofferingid": "6fec5275-9ec3-4033-8052-b743b3f89303",
      "diskofferingname": "Medium",
      "zoneid": "60763583-8ab3-436e-8acd-87c783729cdc",
      "zonename": "Toronto"
    }
  ]
}

I'm unable to print this output when I run this shell command from my php code.

exec('cloud_query " list virtualmachines listall=true account=$user"', $outputArray);
print_r ($outputArray, false);

I get the following:

Array
(
    [0] => {
    [1] =>   "count": 1,
    [2] =>   "virtualmachine": [
    [3] =>     {
    [4] =>       "account": "admin",
    [5] =>       "affinitygroup": [],
    [6] =>       "cpunumber": 2,
    [7] =>       "cpuspeed": 1000,
    [8] =>       "created": "2015-04-02T12:11:41-0400",
    [9] =>       "details": {
    [10] =>         "hypervisortoolsversion": "xenserver56"
    [11] =>       },
    [12] =>       "diskofferingid": "6fec5275-9ec3-4033-8052-b743b3f89303",
    [13] =>       "diskofferingname": "Medium",
    [52] =>       "serviceofferingid": "76d3bacc-eba4-4080-ba72-7c0b524f8027",
    [53] =>       "serviceofferingname": "Large Instance",
    [54] =>       "state": "Running",
    [55] =>       "tags": [],
    [56] =>       "templatedisplaytext": "ubuntu-12.04.4-server-amd64",
    [57] =>       "templateid": "cbd3ceb0-b615-440e-a0fa-dbcaa234cb8f",
    [58] =>       "templatename": "ubuntu-12.04.4-server-amd64",
    [59] =>       "zoneid": "60763583-8ab3-436e-8acd-87c783729cdc",
    [60] =>       "zonename": "Toronto"
    [61] =>     }
    [62] =>   ]
    [63] => }
)

How can I strip out the array and print plain JSON from the shell command?

Thank you

0

2 Answers 2

2

You're catching the output of the exec-ed command to an array of lines. To convert it to a string, simply join all its values with a newline character:

implode("\n", $outputArray);
Sign up to request clarification or add additional context in comments.

Comments

0

The specified array will be filled with every line of output from the command.

To separate out this use:

echo implode("\n", $output);

Comments

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.