2

What I have noticed is that responses from AWS PHP SDK are sometimes a single result other times an array of results.

The issue may be because of my lack of knowledge when dealing with CFSimpleXML/SimpleXML Objects.

I have tried several ways of doing this but each one is rather difficult and what I've learned is that after a while of doing things and they all seem difficult, then you are doing it wrong.

What's Happening

Example Call With describe_load_balancers

<?php

$elb      = new AmazonELB();
$response = $elb->describe_load_balancers();

if ($response->isOK())
{
    foreach($response->body->LoadBalancerDescriptions() AS $loadBalancer)
    {
        print_r($loadBalancer);
    }
}
?>

If it has only one result it prints something like this:

CFSimpleXML Object
(
    [member] => CFSimpleXML Object
        (
            [SecurityGroups] => CFSimpleXML Object
                (
                )

            [LoadBalancerName] => LBName1
            [CreatedTime] => 2012-08-01T12:22:03.910Z
            ...
        )
)

If it has multiple results it prints something like this:

CFSimpleXML Object
(
    [member] => Array
        (
            [0] => CFSimpleXML Object
                (
                    [SecurityGroups] => CFSimpleXML Object
                        (
                        )

                    [LoadBalancerName] => LBName1
                    [CreatedTime] => 2012-08-01T12:22:03.910Z
                    ...
                )

            [1] => CFSimpleXML Object
                (
                    [SecurityGroups] => CFSimpleXML Object
                        (
                        )

                    [LoadBalancerName] => LBName2
                    [CreatedTime] => 2012-08-01T16:17:21.030Z
                    ...
                )
        )
)

What I want if there is a single result

CFSimpleXML Object
(
    [member] => Array
        (
            [0] => CFSimpleXML Object
                (
                    [SecurityGroups] => CFSimpleXML Object
                        (
                        )

                    [LoadBalancerName] => LBName1
                    [CreatedTime] => 2012-08-01T12:22:03.910Z
                    ...
                )
        )
)

I have tried to loop through and see if member is an array but it still returns it as a CFSimpleXML Object so I was unable to detect the array that print_r says is there.

I want to be able to iterate through the list and either make Models of ELBs in my code from the SimpleXML or easily iterate over the attributes.

Matt R.
  • 2,209
  • 1
  • 17
  • 19

2 Answers2

2
$elb      = new AmazonELB();
$response = $elb->describe_load_balancers();

foreach($response->body->LoadBalancerDescriptions() AS $item)
{
    foreach($item->member() AS $member)
    {
        print_r($member);
    }
}

Prints

CFSimpleXML Object
(
[SecurityGroups] => CFSimpleXML Object
    (
    )

[LoadBalancerName] => LBName1
[CreatedTime] => 2012-08-01T12:22:03.910Z

Edit 9/14/12: Just wanted to do a quick update and share a link I found that helped: Konrad Kiss' Code & Tech Rant: Listing AWS instances in PHP

Matt R.
  • 2,209
  • 1
  • 17
  • 19
0

You can leverage to_json, to_stdClass and to_array methods of $response->body

<?php

require_once 'AWSSDKforPHP/sdk.class.php';

$ec2 = new AmazonEC2();

$response = $ec2->describe_instances(); 
$instances = $response->body->to_stdClass();

echo count($instances->reservationSet->item).PHP_EOL;

foreach ($instances->reservationSet->item as $reservation) {
    echo $reservation->instancesSet->item->instanceId.PHP_EOL; 
}

This already discussed here: iterating over SimpleXML Objext PHP

Community
  • 1
  • 1
Oleg
  • 961
  • 8
  • 16