Close Search Box
Search Box

Search: From:

Close
Newsletter

9Tutorials to your Inbox



Using PHP to get prices from Amazon.com

Using PHP to get prices from Amazon.com
Author lv1 (3900/5000)
1,668 views
1 Star2 Star3Star4 Star5 Star (2 votes, average: 4.50 out of 5)

This is a simple tutorial on how to get prices from amazon with their Web services SDK. First you need an amazon SDK account, hop on over here if you haven’t gotten one already.
Note: This tutorial is only valid for amazon USA, as that is the SDK I am using, Amazon UK and other divisions of amazon have their own SDK and this tutorial will not work for those.

This tutorial consists of two files and a SQL table.

First we need a connect.php file to define our database variable definitions and to define our amazon access key(you get the access key from the above link I
just mentioned.

In connect.php:

[source:php]

<?

$db = mysql_connect(”localhost”, “username”, “password”) or die(”Could not connect.”);

if(!$db)

die(”no db”);

if(!mysql_select_db(”database name”,$db))

die(”No database selected.”);

$acckey=”yourkey”; //your aws access key goes here

?>
[/source]
Put your mysql username, password, and databse name where indicated and put your access key in the $acckey variable.

Now we need to create a mySQL and populate it with amazon product info. This table will be called amz and needs these fields:

1. itemid = Primary, auto-increment, big int — This field does not need to be populated because it is autogenerated
2. ibsn = varchar 255 length — This field contains the amazon ibsn and is require to be populated for this script to work
3. productname = varchar 255 length — for this script, this isn’t needed, but if your going to sell products, you need to have a product name
4. price = varchar – length 50 — this is the field that the script will be populating, it contains the price of the items.

Now go and fill in some product info for productname and IBSN

Now the actual code for parsing the amazon data and getting the price:

[source:php]

<?php

include “connect.php”;

$getitems=”SELECT itemid,ibsn from amz”;

$getitems2=mysql_query($getitems) or die(”Could not get items”);

while($getitems3=mysql_fetch_array($getitems2))

{

$url=”http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=$acckey&Operation=ItemLookup&ItemId=$getitems3[ibsn]&ResponseGroup=Medium,Offers&Condition=All&MerchantID=Amazon”;

$var=file_get_contents($url);

$pricing=explode(”$”,$var);

$lastindex=sizeof($pricing)-1;

$newprice=$pricing[$lastindex];

$theprice=explode(”.”,$newprice);

$deci=substr($theprice[1],0,2);

$realprice=”$theprice[0].$deci”;

print “$realprice<br>”;

$updatecron=”Update amz set price=’$realprice’ where itemid=’$getitems3[itemid]‘”;

mysql_query($updatecron) or die(”Could not update cron”);

mysql_query(”Delete from bgook”);

}

?>
[/source]

The $url variable defines where we are going to get the information for the specific product. Note that ItemID in the URL is set to $getitems3[ibsn], the ibsn of the product your getting information for.

The PHP function file_get_contents converts the information in $url into a string that we can parse. The contents of the string is stored in $var.
The variable $pricing takes the string and splits in into an array using the explode() function. The delimiter is “$” which means anything in the string before the first “$” will be stored in $pricing[0], anything between the 1st and 2nd occurence of “$” in $pricing[1] and so on.
Lastindex uses the sizeof() function to count how many elements there are in pricing. It uses a minus one to get the value for the last element because arrays in PHP start at index 0 and not 1. We set $newprice to the last element in $pricing because that element has the data that we want.
$theprice splices $newprice further into an array by the delimieter “.” , the first element in $theprice has the dollar value of the item and the 2nd element has the cents item, which is stored in $deci. We only want the first two digits of the cents price so we use the substr() function and 0 an 2 as parameters to specify the string to chop off after the values between the 0th digit and the 2nd digit. Finally $realprice combines the dollar value and cents value together with a “.” to get a valid price. We then update the item with the $realprice in the price field in the sql table.

Noticed we did a mysql_fetch_array() loop so it will loop through and update all items in the table. A script like this is best used as a nightly update cron.

Note: If you want other information on the product such as description, you can use the same sort of process, however, the parameters will be different for splicing depending on where the information resides in the $url string retrieved. You should print out $var to see just how the xml output from amazon is formatted. Responsegroup in the URL is how much information you want, for this medium was necessary, check the amazon SDK FAQ to see which response group has the information you want in a product.

Note: Some items in the apparel section don’t work as intended with the amazon SDK. If you are retrieving the wrong price for those, talk to amazon, not me, as I don’t have any control over how their SDK actually works.

Copyright  @ Chipmunk 2007

del.icio.us:Using PHP to get prices from Amazon.com digg:Using PHP to get prices from Amazon.com spurl:Using PHP to get prices from Amazon.com newsvine:Using PHP to get prices from Amazon.com blinklist:Using PHP to get prices from Amazon.com furl:Using PHP to get prices from Amazon.com reddit:Using PHP to get prices from Amazon.com blogmarks:Using PHP to get prices from Amazon.com Y!:Using PHP to get prices from Amazon.com magnolia:Using PHP to get prices from Amazon.com segnalo:Using PHP to get prices from Amazon.com

Post a Comment »








Safari hates me

Comment Guidelines

  • Hyperlinks are automatically generated.
  • <em>italic</em>
  • <strong>bold</strong>