Amazon Web Services: A Brief Introduction Using PHP
By: Dan O'Brien, Published: 2004-02-02, Parent Categories: Affiliate Programs, PHP
Parse the XML Using PHP
Before we parse the XML, take a look at a close-up of the actual XML data that
is returned from AWS. Product data is found within the tags.
Let's take a look at some (not all) of the data returned from a sample XML file.
As you can see, AWS made the XML tag names quite self-explanatory:
<Details url="http://www.amazon.com/exec/obidos/ASIN/0764519832/[Your
Associates ID]?dev-t=[Your developer token]%26camp=2025%26link_code=xm2">
<Asin>0764519832</Asin>
<ProductName>The Unofficial Guide to Las Vegas 2004</ProductName>
<ImageUrlSmall>http://images.amazon.com/images/P/0764519832.01.THUMBZZZ.jpg</ImageUrlSmall>
<Availability>Usually ships within 24 hours</Availability>
<ListPrice>$17.99</ListPrice>
<OurPrice>$12.59</OurPrice>
</Details>
It's finally time
to parse the XML and display the data on our webpage. Fortuitously, PHP comes with some nice
XML parser functions to help us on our way. We'll parse the first page of a
lite BrowseNode search in the Las Vegas Travel Books category, sorted by featured
item. (Our Las Vegas Travel Books BrowseNode will come in handy here.) From the code
below, PHP will parse the contents of the XML into two arrays,
one containing the data of the XML file, the other containing the index of the
location to the data.
<?php
// --- Amazon Web Services Simple Example ---
/**
* XML Link:
* Lite BrowseNode search for Las Vegas Travel Books sorted by
Featured Item, Page 1
*
* REMEMBER to add you Associates ID and Developer Token
*/
$xmlFeed = 'http://xml.amazon.com/onca/xml3?t=[Your Associates
ID]&dev-t=[Your Developer
Token]&BrowseNodeSearch=17359&mode=books&sort=+pmrank&offer=All&type=lite&page=1&f=xml';
$data = @implode("",file($xmlFeed));
$parser = xml_parser_create();
xml_parser_set_option($parser,XML_OPTION_SKIP_WHITE,1);
xml_parser_set_option($parser,XML_OPTION_CASE_FOLDING,0);
xml_parse_into_struct($parser,$data,$d_ar,$i_ar);
xml_parser_free($parser);
/**
* The XML data is broken up into the two arrays,
* $d_ar and $i_ar, now we can loop through
* the arrays to display the data on our website:
*/
for($i=0; $i<count($i_ar['Details']); $i++) {
if($d_ar[$i_ar['Details'][$i]]['type']=='open') {
for($j=$i_ar['Details'][$i];
$j<$i_ar['Details'][$i+1]; $j++) {
/**
* Assign the data to a variable;
* to include more tags, add extra elseif statements
*/
if($d_ar[$j]['tag'] ==
'ProductName'){
$ProductName =
$d_ar[$j]['value'];
}elseif($d_ar[$j]['tag'] ==
'OurPrice'){
$OurPrice =
$d_ar[$j]['value'];
}
}
// --- Display the product's name and price ---
echo '<strong>' .$ProductName.
'</strong> <em>Price: ' .$OurPrice. '</em><br
/>';
}
}
?>
Based on the code above, my results are printed below. Be sure to plug your
Associates ID and Developer's Token into the XML link. Your results may vary
as prices fluctuate and featured items change.
Frommer's Las Vegas 2004 Price: $11.89
The Unofficial Guide to Las Vegas 2004 Price: $12.59
777 Cheap Eats in Las Vegas Price: $9.42
Las Vegas for Dummies, Second Edition Price: $11.19
Avant-Guide Las Vegas: Insider's Guide for Cosmopolitan Travelers Price: $13.97
Comp City: A Guide to Free Casino Vacations (2nd Edition) Price: $13.97
Southwest USA & Las Vegas (Eyewitness Travel Guides) Price: $14.00
Frommer's Las Vegas with Kids Price: $11.19
Frommer's Portable Las Vegas for Non-Gamblers Price: $8.79
Streetwise Las Vegas Price: $5.95