|
|
|
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>";
?> | | |
|
| Freshmeat.net XML-RPC - This class is meant to query Freshmeat for information about registered projects. Categories : PHP, PHP Classes, XML, Web Services | | | Sample AIM (Advanced Integration Method) PHP Script For Authorize.net using CURL Categories : CURL, Ecommerce, PHP | | | 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 | | | phpAds, a complete banner and ad management system with detailled tracking and stats. Categories : MySQL, Complete Programs, Ecommerce, PHP, Databases | | | open source online php shop project ecommerce commerce Categories : PHP, Ecommerce | | | RSS parser.
Parses RSS into an array. Quick and nasty but does the job.
No checking is done for correct Tags, only correct XML.
PHP4 needed to display result (uses print_r). Categories : PHP, XML, PHP Classes, Rich Site Summary (RSS) | | | PHPCatalog Categories : Ecommerce, PHP, Complete Programs | | | an example of the cyberlib payment class Categories : PHP, PHP Classes, Ecommerce, Credit Cards | | | Stock quotes from yahoo! Categories : PHP, Web Services, Arrays | | | XML To Array Categories : PHP, PHP Classes, XML, Arrays | | | Newbie Notes #8 - A cron trick Categories : PHP, CURL, Beginner Guides | | | Amazon book cover handling Categories : HTML and PHP, PHP, MySQL, Ecommerce | | | shopping cart class with add/edit/delete product functionality. Categories : PHP, PHP Classes, Ecommerce | | | ECHO-PHP Class Real Time Transaction Processor v1.4.4 for Credit Cards and
Checks / ACH Categories : PHP Classes, Cybercash, Classes and Objects, Ecommerce, PHP | |
| | | | 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.
| |
|
|
|