Reputation: 49
I am trying to do following: using core PHP (without any framework or plugin) & AMAZON API
Parse the XML & display in a tabular format in HTML
So far I have been able to complete upto step 3, but unable to complete step 4 of parsing the XML & displaying the data.
Attachments:
Code sample:
$file = file_get_contents($request_url);
$xml = simplexml_load_string($file);
print_r($xml);
Output: I am pasting the first 2 of 10 array elements that I am getting from above print_r;
( [0] => SimpleXMLElement Object ( [ASIN] => 8184954018 [DetailPageURL] => amazon.in/Chanakyas-Secrets-Leadership-Sivanandhan-Radhakrishnan/dp/8184954018?SubscriptionId=AKIAJ5B7EPH4TJ6YEUUQ&tag=30061200-21&linkCode=xm2&camp=2025&creative=165953&creativeASIN=8184954018 [ItemLinks] => SimpleXMLElement Object ( [ItemLink] => Array ( [0] => SimpleXMLElement Object ( [Description] => Add To Wishlist [URL] => amazon.in/gp/registry/wishlist/add-item.html?asin.0=8184954018&SubscriptionId=AKIAJ5B7EPH4TJ6YEUUQ&tag=30061200-21&linkCode=xm2&camp=2025&creative=165953&creativeASIN=8184954018 ) [1] => SimpleXMLElement Object ( [Description] => Tell A Friend [URL] => amazon.in/gp/pdp/taf/8184954018?SubscriptionId=AKIAJ5B7EPH4TJ6YEUUQ&tag=30061200-21&linkCode=xm2&camp=2025&creative=165953&creativeASIN=8184954018 ) [2] => SimpleXMLElement Object ( [Description] => All Customer Reviews [URL] => amazon.in/review/product/8184954018?SubscriptionId=AKIAJ5B7EPH4TJ6YEUUQ&tag=30061200-21&linkCode=xm2&camp=2025&creative=165953&creativeASIN=8184954018 ) [3] => SimpleXMLElement Object ( [Description] => All Offers [URL] => amazon.in/gp/offer-listing/8184954018?SubscriptionId=AKIAJ5B7EPH4TJ6YEUUQ&tag=30061200-21&linkCode=xm2&camp=2025&creative=165953&creativeASIN=8184954018 ) ) ) [ItemAttributes] => SimpleXMLElement Object ( [Author] => D. Sivanandhan Radhakrishnan Pillai [Manufacturer] => Jaico Publishing House [ProductGroup] => Book [Title] => Chanakya's 7 Secrets of Leadership )
[1] => SimpleXMLElement Object ( [ASIN] => B00EA0Q3PW [DetailPageURL] => amazon.in/Improve-Your-Leadership-Management-Skills-ebook/dp/B00EA0Q3PW?SubscriptionId=AKIAJ5B7EPH4TJ6YEUUQ&tag=30061200-21&linkCode=xm2&camp=2025&creative=165953&creativeASIN=B00EA0Q3PW [ItemLinks] => SimpleXMLElement Object ( [ItemLink] => Array ( [0] => SimpleXMLElement Object ( [Description] => Add To Wishlist [URL] => amazon.in/gp/registry/wishlist/add-item.html?asin.0=B00EA0Q3PW&SubscriptionId=AKIAJ5B7EPH4TJ6YEUUQ&tag=30061200-21&linkCode=xm2&camp=2025&creative=165953&creativeASIN=B00EA0Q3PW ) [1] => SimpleXMLElement Object ( [Description] => Tell A Friend [URL] => amazon.in/gp/pdp/taf/B00EA0Q3PW?SubscriptionId=AKIAJ5B7EPH4TJ6YEUUQ&tag=30061200-21&linkCode=xm2&camp=2025&creative=165953&creativeASIN=B00EA0Q3PW ) [2] => SimpleXMLElement Object ( [Description] => All Customer Reviews [URL] => www.amazon.in/review/product/B00EA0Q3PW?SubscriptionId=AKIAJ5B7EPH4TJ6YEUUQ&tag=30061200-21&linkCode=xm2&camp=2025&creative=165953&creativeASIN=B00EA0Q3PW ) [3] => SimpleXMLElement Object ( [Description] => All Offers [URL] => amazon.in/gp/offer-listing/B00EA0Q3PW?SubscriptionId=AKIAJ5B7EPH4TJ6YEUUQ&tag=30061200-21&linkCode=xm2&camp=2025&creative=165953&creativeASIN=B00EA0Q3PW ) ) ) [ItemAttributes] => SimpleXMLElement Object ( [Author] => Meir Liraz [Manufacturer] => Liraz Publishing [ProductGroup] => eBooks [Title] => How to Improve Your Leadership and Management Skills - Effective Strategies for Business Managers )
What help I require: Out of above two array elements what PHP code will get me the output table with following columns
Upvotes: 0
Views: 1602
Reputation: 57121
The basis of what you want can be processed easily by using DOMDocument rather than with SimpleXML. The default namespace (xmlns definition in the ItemSearchResponse element) makes SimpleXML not so simple.
But as a start, the following code should help...
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$file = file_get_contents("t1.xml");
$xml = new DOMDocument();
$xml->loadXML($file);
$items = $xml->getElementsByTagName("Items")[0];
foreach ( $items->getElementsByTagName("Item") as $item ) {
echo "ASIN:".$item->getElementsByTagName("ASIN")[0]->nodeValue.PHP_EOL;
echo "Author:".$item->getElementsByTagName("Author")[0]->nodeValue.PHP_EOL;
}
All this does is to first extract the <items>
element and the uses a loop to fetch each <item>
element in that. In the loop you can see that it's extracting individual elements data by using the appropriate name.
As each call to getElementsByTagName
returns an array of elements, thats why you have to say use just the first one (assuming this is correct, if not you can foreach
over them).
Upvotes: 1