This tutorial is for people who would rather work with multi-dimensional arrays than learn how to use SimpleXML.
By default, CloudFusion uses SimpleXML to parse the XML content that Amazon sends back from their web services. The SimpleXML response is not an array, but rather a SimpleXML object that you can traverse similarly to multi-dimensonal arrays.
Fortunately, there is a sneaky way to convert SimpleXML objects into arrays: json_encode(). The native JSON functionality that is built into PHP 5.2 and newer is designed to take a PHP object or array and encode it into a JSON string. However, when you decode it, the JSON functions translate the JSON string back into an everyday, run-of-the-mill array.
When I discovered this, I added a new utility to the CFUtilities() class as convert_response_to_array(). Here's an example of how you'd use it with the AmazonS3() class.
<?php require_once 'cloudfusion.class.php'; // Get data $s3 = new AmazonS3(); $response = $s3->list_buckets(); // Convert to an array $response = $s3->util->convert_response_to_array($response); // Do a print_r() to look at the array structure print_r($response); ?>
Tarzan users who haven't yet upgraded to CloudFusion 2.5 can still use this cool trick, as long as they have PHP 5.2 or newer. Here's how:
<?php require_once 'tarzan.class.php'; // Get data $s3 = new AmazonS3(); $response = $s3->list_buckets(); // Convert to an array $response = json_decode(json_encode($response)); // Do a print_r() to look at the array structure print_r($response); ?>
CloudFusion™ is © 2007–2010 Ryan Parman, Foleeo Inc., and contributors. The CloudFusion name and logo are trademarks of Ryan Parman. WarpShare™ is a trademark of Foleeo Inc. The source code is provided under the New BSD License. Documentation and tutorial text is provided under the [CC] BY-NC-SA license. All other rights are reserved.