This script is a nice and easy way of parsing REST data from Amazon. The
idea is, you can obtain data about Amazon products for display/storage in
a database, eg. If you had a website about books, you could store amazon's
review or price, images, dimensions etc about each book - the posibilities
are endless.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public Licence
as published by the Free Software Foundation; either version 2
of the Licence, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public Licence for more details.
You should have received a copy of the GNU General Public Licence
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
// parse the XML!
$this->parseXML($this->xml_to_parse);
}
function __destruct()
{
// free up memory.
xml_parser_free($this->xml_handle);
}
function startingElement($p, $name, $attrib)
{
// check to see if the starting tag
// has been encountered yet...
if(!$this->xml_started)
{
if(strtolower($name) == strtolower($this->xml_starting_trigger))
$this->xml_started = true;
return;
}
// found a starting XML tag.
// add this element to the heirachy
if($this->currentDepth < $this->xml_depth)
$this->xml_heirachy[$this->xml_depth] = $this->currentElementName;
function endingElement($p, $name)
{
// check to see if the starting tag
// has been encountered yet...
if($this->xml_started && $this->xml_depth > 0)
{
if(strtolower($name) == strtolower($this->xml_starting_trigger))
$this->xml_started = false;
// found a closing XML tag.
$this->xml_depth--;
$this->currentDepth = $this->xml_depth;
}
}
function elementData($p, $data)
{
if($this->xml_started)
{
if($this->xml_depth > 1)
{
// not a root element, so use the location variable.
// build a string of the depth of the multi-dimentional array
foreach($this->xml_heirachy as $level => $string)
$location .= "[" . strtolower(str_replace('$', '\$', $string)) . "]";
eval("\$this->data" . $location . "[" . strtolower($this->currentElementName) . "] .= \$data;");
}
else
{
// this is a root element.
// no location variable is needed, just add to array
eval("\$this->data[" . strtolower($this->currentElementName) . "] .= \$data;");
}
}
}
function parseXML($parse)
{
xml_parse($this->xml_handle, $parse);
}
}
$subid = "0BWWFMNWMZE8QPPS6GR2"; // Amazon Subcription ID (you MUST apply for one of these before you start. see URL in header)
$itemID = "B000084318"; // Product ID (this one is the GameCube game `Zelda: The Wind Waker`)
$associateTag = ""; // Assosiate tag (if you have one)
$responseGroup = "Medium"; // Response Group (see Amazon API for details)
// get data from amazon.
$atag = (empty($associateTag)) ? '' : 'AssociateTag=' . $associateTag;
Bill Seaton wrote :1252
I tried running this script on a curl-enabled server and got the following error message:
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or `}` on line 50
Werner Niedermeier wrote :1253
Same error here: Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or `}` in /homepages/16/d25751081/htdocs/amazon_xml.php on line 50
Jon Ellis-Jones wrote :1254
hi guys. This script is written for PHP5 only, it will __NOT__ work in PHP4, which I`m guessing is the problem. It has been tested only under Apache 2.x as well, I haven`t had the time to try it under IIS.
- Jon
Jon Ellis-Jones wrote :1256
If people would find it beneficial for me to write a PHP 4.x version of the same code, I will do so. just let me know here, or via e-mail, and i`ll sort that out for you.
angi kaplony wrote :1631
It`s much easier to parse the raw_data with simple_xml, than transforming all in an array.