|
|
|
AMAZON.COM CURL-REST Parser.
Copyright ©2004, Jon Ellis-Jones.
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.
see: http://www.amazon.com/webservices for more information.
NB : this script works for all amazon subsites, such as amazon.co.uk/amazon.de,
just modify the areas in the script. for more help, see the URL above.
PREREQUISITES:
--------------
You must have the cURL extension enabled in PHP. See below URL for details:
http://www.weberdev.com/Manuals/PHP/ref.curl.html
LICENCE:
--------
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.
|
<?php
//if(!extension_loaded('curl')) {
// die("this script requires the curl extension<br />see: <a href=\"http://www.php.net/manual/en/ref.curl.php\">http://www.php.net/manual/en/ref.curl.php</a>");
//}
class xml
{
public $xml_handle;
public $xml_temp_field;
public $xml_starting_trigger;
public $xml_depth;
public $xml_heirachy;
function __construct($plain_xml, $starting_trigger = null)
{
$this->xml_starting_trigger = $starting_trigger;
$this->xml_started = false;
$this->xml_handle = xml_parser_create();
$this->xml_to_parse = $plain_xml;
$this->xml_heirachy = array();
$this->xml_depth = 0;
// set object handlers.
xml_set_element_handler($this->xml_handle, array(&$this, "startingElement"), array(&$this, "endingElement"));
xml_set_character_data_handler($this->xml_handle, array(&$this, "elementData"));
// 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;
$this->xml_depth++;
$this->currentElementName = $name;
}
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;
$url = "http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&" . $atag . "&MerchantId=Amazon&ResponseGroup=" . $responseGroup . "&SubscriptionId=" . $subid . "&Operation=ItemLookup&ItemId=" . $itemID;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$raw_data = curl_exec($ch);
curl_close($ch);
$xml = new xml($raw_data, "Item");
// here is an assosiative array with
// all the data you require in it
echo "<pre>";
print_r($xml->data);
echo "</pre>";
?> | | |
|
| Sample AIM (Advanced Integration Method) PHP Script For Authorize.net using CURL Categories : CURL, Ecommerce, PHP | | | Freshmeat.net XML-RPC - This class is meant to query Freshmeat for information about registered projects. Categories : PHP, PHP Classes, XML, Web Services | | | Get the AppStore Ranking for any iPhone App Categories : PHP, Web Services, Regexps | | | Amazon book cover handling Categories : HTML and PHP, PHP, MySQL, Ecommerce | | | grab the result of any calculation you submit to the Google Calculator. Categories : PHP, Arrays, Web Services, Regexps, Math. | | | What's the weather? Categories : PHP, Web Services | | | shopping cart class with add/edit/delete product functionality. Categories : PHP, PHP Classes, Ecommerce | | | Credit Card Identification and Validation Class - The credit_card class provides methods for cleaning, validating and identifying the type of credit card numbers. Categories : PHP, PHP Classes, Credit Cards, Ecommerce, Algorithms | | | MySQL to XML. Categories : MySQL, XML, PHP | | | Dynamic Loading of XML array data into ComboBox and Display XML data using PHP + DOM + Javascript. Categories : PHP, Java Script, DOM XML, XML, Arrays | | | Example Shopping cart class Categories : Ecommerce, PHP, PHP Classes | | | simple shopping cart for php3 Categories : PHP, PHP Classes, Complete Programs, Ecommerce | | | file class , uploade file , download file already uploaded on another website Categories : PHP, PHP Classes, Filesystem, Web Services | | | Authorize.net AIM Interface Class v1.0.0 Categories : PHP, PHP Classes, Ecommerce, Payment Gateways | | | PHP-MySQL shopping cart
Categories : PHP, Ecommerce, Complete Programs | |
| | | | 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.
| |
|
|