Amazon: PHP API

posted on August 13, 2009

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.

amazon-2

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: , , ,

Like this post? Someone else might, too.

  • RSS
  • Reddit
  • del.icio.us
  • Facebook
  • StumbleUpon
  • Technorati
  • Twitter
  • Digg

11 Responses to “Amazon: PHP API”

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?

Austin says:

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?

Makeithappen says:

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.

Austin says:

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 ?

Austin says:

Remi, there are two examples in the post. I’ll explain things a little better, though.

<?php

	$am = new AmazonHandler;

	$nodes = $am->request( 'AmazonMethod', array(
		'ParameterOne' => 10,
		'ParameterTwo' => 41
	) );

?>

Just replace ‘AmazonMethod’ with the name of an Amazon method (here) and then fill the parameters array with any parameters. I hope that helped!

David says:

great, except it doesnt work.. or at least the copy and paste method for your examples wasn’t what you wanted us to do

Austin says:

@David

There was a typo in the demo. Are things working now?

Jim E. Jam says:

“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.

Austin says:

@Jim

Thanks for being a grammar Nazi!

Trackbacks:

Leave a Reply

Subscribe to gulati.info

Subscribe via RSS (?) Subscribe to our RSS feed Subscribe via Email
Follow me on Twitter Follow me on Twitter