PHP Development Board php divider

User Options
Register--Login--Top 20 Posters--Search Topics


Forum Main>>Tutorials>>Getting prices from amazon

New Topic-Reply




Author
Post
Chipmunk

Rank:Settler of Bobland
Group: Head Administrator
Posts: 2867
IP Logged

PM ID and RPS ID: 1
PM [Chipmunk]

View Member Photo

Last replied to on Mon Feb 01, 2010 04:08:12
Edit Post|Quote
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 to https://aws-portal.amazon.com/gp/aws/developer/registration/index.html? 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:

Code:

<?
$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
?>



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:

Code:

<?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");



}
?>


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.

-----------------------------
Chipmunk,
Supreme Administrator

Chipmunk

Rank:Settler of Bobland
Group: Head Administrator
Posts: 2867
IP Logged
PM ID and RPS ID: 1
[PM Chipmunk]

View Member Photo

Posted at Thu Mar 15, 2007 23:14:36
Edit post|Quote
Guess the API's are more similiar than I thought.
-----------------------------
Chipmunk,
Supreme Administrator

butch
Rank:acorn
Group: members
Posts: 2
IP Logged
PM ID and RPS ID: 12852
[PM butch]

RPS score: 0
RPS challenge

Posted at Thu May 17, 2007 07:30:56
Edit post|Quote
hi there after long search for solutions i found your site. well - believe me or not. i am a friend of aws. i look forward see an aws amazon web services or amazon product feed script for Postnuke. i am glad i found your site. Well i am interested in a script that supports PostNuke. I will pay up to 30 Dollars if we get the thing up and running in a Postnuke. my interests are only non commercial. if you write the bridge  then you have all rights. i only want to have the copy to run it on my site. For you: That would open up a great path to a large community that is interested in your coding and in your  script. can u help me to get the amazon web services-script running in postnuke i look forward to hear from you best regards butcher 
-----------------------------
great site

Chipmunk

Rank:Settler of Bobland
Group: Head Administrator
Posts: 2867
IP Logged
PM ID and RPS ID: 1
[PM Chipmunk]

View Member Photo

Posted at Thu May 17, 2007 15:43:28
Edit post|Quote
Are you looking to make this aws script an add-on for post-nuke or are you just looking for an XML feed?
-----------------------------
Chipmunk,
Supreme Administrator

qatrimong
Rank:acorn
Group: members
Posts: 1
IP Logged
PM ID and RPS ID: 24758
[PM qatrimong]

RPS score: 0
RPS challenge

Posted at Wed Oct 14, 2009 05:23:37
Edit post|Quote
Amazon has steadily branched into retail sales of music CDs, videotapes and DVDs, software, consumer electronics, kitchen items, tools, lawn and garden items, toys & games, baby products, apparel, sporting goods, gourmet food, jewelry, watches, health and personal-care items, beauty products, musical instruments, clothing, industrial & scientific supplies, groceries, and more.The company launched Amazon.com Auctions, its own Web auctions service, in March 1999. However, it failed to chip away at industry pioneer eBay's juggernaut growth. Amazon Auctions was followed by the launch of a fixed-price marketplace business called zShops in September 1999, and a failed Sotheby's/Amazon partnership called sothebys.amazon.com in November.Amazon no longer mentions either Auctions or zShops on its main pages and the help page for sellers now only mentions the Marketplace. Old links to zShops now simply redirect to the Amazon home page, while old links to Auctions take users to a transactions history page. New product listings are no longer possible for either service.
-----------------------------
domain registration - viagra - watches

sadydude
Rank:acorn
Group: members
Posts: 1
IP Logged
PM ID and RPS ID: 26845
[PM sadydude]

RPS score: 0
RPS challenge

Posted at Mon Feb 01, 2010 04:08:12
Edit post|Quote
There is one more site that is good and I also have visited it,this has provided mcsa 2008 me such information that is really unique and the service that they offer also I have experienced, really I enjoy this service so I want to make an appeal to others please visit this and I want to tell you that after experiencing this service you’ll enjoy it again and again. It's an good idea i really love this and appreciate your work, you know time is money an a person is successful cisco certification
if he time timely en cash it, right now now i m doing mcse and hope a great career waits for me with a career oriented job, I hope, May IT gives you a great career and I suggest the people to get involved in this certification to serve the nation with blood and intellect.ccsp


Page: 1



Powered by © Chipmunk Board

Flash games Ninja games-Web Design New York