Amazon: PHP API
Amazon’s API is a good one. It is well documented API that is unbeatable when building an ecommerce site. Here’s a simple PHP class to handle your Amazon requests.

The code
config.php
This doesn’t have to go in config.php – it can go anywhere. Preferably, put it with other configuration values for your script.
<?php // This would go in your configuration file define( 'AMAZON_ASSOC_TAG', '' ); define( 'AMAZON_ACCESS_KEY', '' ); define( 'AMAZON_SECRET_KEY', '' ); // This isn't required for this simple API define( 'AMAZON_REQUEST_URL', 'http://ecs.amazonaws.com/onca/xml?Service=AWSECommerceService&Version=2007-07-16' ); ?>
If you are using a different format for configuration (not constants), it should be pretty easy to modify things to use that format.
amazon.php
Here’s the handler class. It’s a simple one. I’ll explain how to use it after.
<?php
class AmazonHandler {
var $pubKey, $secKey, $requestURL;
function __construct() {
$this->assocTag = AMAZON_ASSOC_TAG;
$this->pubKey = AMAZON_ACCESS_KEY;
$this->secKey = AMAZON_SECRET_ACCCESS_KEY;
$this->requestURL = AMAZON_REQUEST_URL;
}
function request( $operation, $data ) {
$url = "{$this->requestURL}&Operation={$operation}&AWSAccessKeyId={$this->pubKey}&AssociateTag={$this->assocTag}" . $this->buildData( $data );
$response = @file_get_contents( $url );
$xml = simplexml_load_string( $response );
return $xml;
}
function buildData( $data ) {
$return = NULL;
$i = 0;
$t = count( $data );
foreach( $data as $k => $v ) {
$k = urlencode( $k );
$v = urlencode( $v );
$return .= "&{$k}={$v}";
}
return $return;
}
}
?>
An explanation
This handler is quite simple. It only offers the request() method to request data from Amazon. Here’s a few examples:
<?php $am = new AmazonHandler; $nodes = $am->request( 'BrowseNodeLookup', array( 'BrowseNodeId' => 1 ) ); $items = $am->request( 'ItemSearch', array( 'BrowseNode' => 1, 'SearchIndex' => 'Books', 'Keywords' => 'Harry Potter', 'ItemPage' => 1 ) ); ?>
The request() method returns a SimpleXML object.
You can learn about all of the API methods here. (Look under API Reference > Operations.) You will need to know the different API operations and parameters to use this handler.
Tags: Amazon AWS, Amazon PHP API, Learm PHP, PHP Classes










Comments:
Maybe you should say ‘a simple API which isn’t *actually* an API’?
Also, isn’t it time to drop support for PHP 4?
When did I say that this class is an API? The title is the only part that could be misleading. It’s an API handler, not an API.
Also, what are you referring to that could be changed to remove support for PHP4?
Great info Austin. Keep up the great work.
As far as I can tell he’s referring to line 5 in amazon.php, where you use var to define the class properties instead of the suitable visibility keywords private or protected.
Oh, I see. Thanks, Raphael. I don’t see any disadvantages of leaving it compatible with PHP4, though.
Thanks, Make! You obviously know where this is coming from. :)
can you give an example on how to use it ?
Remi, there are two examples in the post. I’ll explain things a little better, though.
Just replace ‘AmazonMethod’ with the name of an Amazon method (here) and then fill the parameters array with any parameters. I hope that helped!
great, except it doesnt work.. or at least the copy and paste method for your examples wasn’t what you wanted us to do
@David
There was a typo in the demo. Are things working now?
“on your configuration file”
I tried putting it “ON” my config file, but nothing happened.
Then I put it “IN” my file and it worked.
@Jim
Thanks for being a grammar Nazi!
Trackbacks: