Friday, December 28, 2007

 

Using Amazon Web Services with REST Part 2: Binding Data To A Web Page With ASP.net

In Part 1, I showed how to construct a URL which would pull out data from Amazon's web services on the best selling Power Ranger toys. With that part done, it's now time to think about how we make use of that data by using it in a web page.

For this example, I am going to show you how I do this using VB in ASP.net. I realise that ASP.net is a bit of a minority interest and it seems like most people out there are using PHP. I will add tutorials for other options later on, but today I will just do ASP.net and for that you will need web hosting space which supports the .net framework.

Overview

ASP.net comes with some nice controls which you can easily bind to an XML data source. The only challenge here is that to use them in their simplest form, they like the XML source to be rather flat in structure. Unfortunately the XML from Amazon is quite complex and has many layers to it which makes it trickier than I like to bind to a data control. So the way I do it is in two steps:

1) Apply an XSLT transformation to the XML source to "flatten it" and just give me the data I want.
2) Bind the transformed XML to my data control.

There will surely be other ways to achieve the same goal, but this is the way I to do it to achieve the result I want with the skillset I have.

Use XSLT to Transform and Flatten the XML from Amazon

Amazon's Web Services have an XSLT processor which means you are able to apply an set of XSL rules to the XML it generates. I have written the following XSL tranformation which extracts key information from the AWS Large Response I mentioned in the previous post and outputs it as more simplified, flatter XML.

See XSL Transformation file (save with .xslt extension to use)

If you want to try following this example, you can save that text file as something like transform.xslt and upload it to your own webspace. I will also zip it up with and make it available for download with the web page source code which I will come to shortly.

Once it has been uploaded to some webspace that you have accessible to you, you then add a Style parameter to the querystring that we built in Part 1.

http://xml-uk.amznxslt.com/onca/xml?Service=AWSECommerceService&
Operation=ItemSearch&AWSAccessKeyId=XXXXXXXXXXXXXXXXXXXXX&
AssociateTag=toysandvideogames-21&&SearchIndex=Toys&
Title=Power%20Rangers&Sort=salesrank&ResponseGroup=Large&Style=http://mydomainname/transform.xslt

Bind the Data to a Web Page Control

Now that the source XML is a simplified form, it's very easy to bind to a data control in a webpage in asp.net. I won't explain too much but just give you the source code. It's really hard to paste code into Blogger without it kicking up a fuss so here's a link to the source code in a text file.

ASP.net Example Source Code

Replace the XXXXX in the code with the URL from Part 1 and then save that with a .aspx extension, upload it and then access it in the browser and it should look something like this.



There we have it - live product information from Amazon with the best selling Power Rangers toys. That's one way you can do it in asp.net using VB - I'm sure there are even better ways. I'll soon post up instruction on achieving the same using different development environments.

Use at your own risk - remember your web server needs to support asp.net

Any questions?

Labels: , , , , ,


Thursday, December 27, 2007

 

Using Amazon Web Services with REST Part 1: Constructing The URL

Amazon's Web Services expose all their product information to developers using a couple of different methods: SOAP and REST. In this guide, I will look at how you can use REST, or XML over HTTP, to retrieve information. XML over HTTP is a URL-centric approach so the key to querying the Amazon data store is to construct a querying URL that has the right parameters to pull out the information you want. In this example, I am going to extract a list of bestselling Power Ranger toys from Amazon UK.

We start our URL with

http://

The first thing you need to do is then identify the right resource to connect to, which will depend on the regionality of the results you want. To extract information from Amazon UK web services, use:

xml-uk.amznxslt.com/onca/xml?Service=AWSECommerceService

The next step is to define the mode of operation. For instance, are you searching for a set of results or looking up a specific item. For this example, we are searching for a set of results, so add in:

&Operation=ItemSearch

It's at this point that I add in the developer's key which you get when you sign up to Amazon Web Services.

&AWSAccessKeyId=XXXXXXXXXXXXXXXXXXX (replace the Xs with your own)

You also need to add a parameter with the Amazon associate ID you want to use, so that the results have it embedded in them, creating the trackable deeplinks which will earn you money.

&AssociateTag=toysandvideogames-21

Next step is to define the "SearchIndex" in which you want to search. Examples include DVDs, VideoGames, Kitchen or Blended to search across indices. In this example, I will use toys:

&SearchIndex=Toys

The next important parameter is to define the search text which will bring back the appropriate results. The parameter is the "Title":

&Title=Power%20Rangers (notice that spaces are replaced with "%20" so the URL is not broken)

I'm next going to define the order of the results. Like when you are on the Amazon site itself, you can see the results in all manner of ways such as alphabetically sorted, in price order or see the bestselling first. I think being able to get the data in order of sales rank is such a useful thing compared to the functionality of static product feed files provided by most affiliate networks. Here's how you do that:

&Sort=salesrank

There's one more parameter we need to add in which defines the amount of information that Amazon sends back in response to your query. There's quite a few differing options, but for this example and for simplicity, I will just use this parameter, which asks for a "Large" response.

&ResponseGroup=Large

Ok, now if you concatenate all the bold snippets above into one long URL, you will have your completed querystring for pulling out information on the bestselling Power Rangers toys at Amazon, and it should look something like this:

http://xml-uk.amznxslt.com/onca/xml?Service=AWSECommerceService&
Operation=ItemSearch&AWSAccessKeyId=XXXXXXXXXXXXXXXXXXXXX&
AssociateTag=toysandvideogames-21&SearchIndex=Toys&
Title=Power%20Rangers&
Sort=salesrank&ResponseGroup=Large

The beauty of this is that you can test it instantly -just copy and paste what's above into the address bar of your internet browser and press enter and you will see XML data returned from Amazon with all the information on best selling Power Rangers toys (don't forget to watch out for line breaks when copying, and replacing the XXXs with your developer key from AWS).

Now that you have got the product data from Amazon in XML format, the next stage is to format it and display it on a web page so you can actually use it. There's a range ways to do that and I'll write about them in Part 2.

Labels: , , , ,


Wednesday, December 26, 2007

 

Update Your Sites By Having A REST

REST (Representational State Transfer) allows for distributed computing with data transactions available using HTTP, URLs and XML. What this means is that you can call information from a data source over HTTP, using a correctly formed URL and get your results delivered to you in XML format, which you can work with using something like ASP.net or PHP to format and display on a web page.

Because URLs are central to how we use the web already, the URL-driven approach of REST makes accessing web services really easy to understand. In essence, you construct a URL where the parameters define the data you want, and the data store sends back the results as XML which you can the process and display as you wish. An example might look like this:

http://domainabouttoys/webservices.aspx?toyname=disney&maxprice=20

Even a non developer could look at that and have a fairly good idea that it's attempting to retrieve a list of Disney toys under £20 from a data store.. This is great because it makes tapping into data nice and easy, and performance is good because requests are cached.

Amazon's web services are already available using REST (I'm still planning to post a nice example soon), and I've also used XML over HTTP from LateRooms.com to create a hotel site with an always up to date list of hotels. Once you have coded your site, the data is pulled live from the the source which means you can build the site once and forget it. Compare and contrast this with needing to download and upload a product feed file.

Microsoft seem to have bought into how effective this is, and are pushing these methods with the recent announcement of ADO.NET Data Services (previously known as project "Astoria"), which provides the libraries and tools for developers to make their data stores available in this flexible way.

I hope to see REST used more often in affiliate marketing in 2008. If a network already stores all their merchant product feeds in a database, implementing this interface will mean affiliates can use up to date product information without the need for working with large, cumbersome product feed files. Credit must, of course, go to networks already making product feed web services available such as Affiliate Window, who at the moment support SOAP/WSDL for their API. I read an interesting statistic that where REST and SOAP are both available on Amazon Web Services, 85% of users opt for REST.

Find out more about REST
REST and the Real World
REST: Simplicity in Web Services Design
REST vs SOAP with Amazon API

Find out more about ADO.Net Data Services
MS Live Labs
Wikipedia

Labels: , ,


Monday, August 20, 2007

 

Do any mobile phone merchants offer live XML feeds?

From what I can see, mobile phone merchants frequently update their deals and tariffs, perhaps more so than most other sectors. This means as an affiliate you often need to download a product feed and then put it up on your site or update your database. Ok, you could look at automating that process with some scripts but isn't there a better way? Web services, or live XML feeds are a great way to put merchant data on your website without having to go through the rigmarole of downloading/uploading product feeds.

Live XML feeds seem to be quite common in the travel vertical, but I'm not aware of any merchants in the Mobile Phone vertical offering this - I think they should. If there are any who are already doing it, please use the comments and let me know who's doing it.

Speaking of XML feeds, the one I use the most is for hotel information from Laterooms who I promote through their independent program. It would be really nice to see my stats but their control panel has displayed an error for well over a week and they haven't replied to email about it. I guess they have a problem and don't have the resource to fix it? Maybe sometimes it pays to go through a network after all...

Labels: , , ,


Sunday, April 22, 2007

 

Amazon Web Services really is a doddle

I've been planning for as long as I can remember to get my head round Amazon's API.
It was one of my new year's resolutions and I mentioned it again in a post last week.

David Fiske replied in the comments and assured me it was a doddle.

Turns out he was right!

After David's useful encouragement, I spent the latter part of last week getting into it. I had assumed it would be be a complicated development project so fired up my Visual Web Developer (which I save for what I consider *proper coding* tasks) and started off down that road.

Then with a little bit more reading, I discovered that the easier way to work with Amazon's Web Services was using XML over HTTP (known as REST). If you're not familiar with it, it basically means you construct a URL which points at Amazon's Web Services and the parameters in the query string allow you to return data you want which comes back in XML form.

You then need to work with the XML anyway you feel comfortable to display it on your page. I work in .net and the nested XML that was returned didn't bind too easily to .net controls like repeaters and datagrids etc, so I then had to go off on a tangent learning about XSLT which enabled me to control the layout of the XML returned and make it more suitable for how I wish to use it. Once I had "flattened" the XML results and only included info I required, I was able to simply bind them to .net controls.

I'm not entirely sure I went about things the best way, but managed to end up doing exactly what I wanted and getting the Amazon toy section listed on my new site, Toys And Videogames. It was actually really enjoyable to get my teeth into some new technologies and actually learn what feels like a new skill. I'll now be looking at adding Amazon Web Services to more sites so they can be live without me having to update them myself. In my researching, I didn't find any really useful sites that explained fully about this route I've taken, so look out for a walkthrough soon.

Labels: , , ,


This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]