Skip to content

Instantly share code, notes, and snippets.

@Exeu
Last active June 20, 2017 16:15
Show Gist options
  • Select an option

  • Save Exeu/4674423 to your computer and use it in GitHub Desktop.

Select an option

Save Exeu/4674423 to your computer and use it in GitHub Desktop.
Amazon ECS Sample Page: http://amazonecs.pixel-web.org
$(document).ready(function() {
var globalRequest = 0;
$('#search').bind('keyup', function(event) {
if (event.keyCode == 13) {
searchAction();
}
});
$('#search-link').bind('click', function(event) {
searchAction();
});
var searchAction = function() {
var value = $('#search').val();
var cat = $('#category').val();
var country = $('#country').val();
var page = $('#page').val();
var track = value + " - " + cat + " - " + country;
var resultContainer = $('#results');
if (value.length < 3 && globalRequest == 1) {
return;
}
//_gaq.push(['_trackEvent', 'Search', track]);
globalRequest = 1;
$.ajax({
url: "search.php",
dataType: 'json',
type: 'GET',
data: "q="+value+"&category="+cat+"&country="+country+"&page="+page,
success: function(data){
globalRequest = 0;
resultContainer.fadeOut('fast', function() {
resultContainer.html('');
for (var x in data) {
if (!data[x].price)
data[x].price = 'kA';
if (!data[x].img)
data[x].img = 'assets/images/no.gif';
var html = '<div class="res-container">';
html += '<h2><a href="'+data[x].url+'" target="_blank">'+data[x].Title+'</a></h2>';
html += '<img src="'+data[x].img+'">';
html += '<h3>Price: '+data[x].price+'</h3>';
html += '</div>';
resultContainer.append(html);
}
resultContainer.fadeIn('fast');
});
}
});
};
});
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Amazon ECS Demo - Product Search</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript" src="assets/js/ama_functions.js"></script>
<style>
body {
font-family: Arial, Tahoma, Verdana;
padding: 0px;
margin: 0px;
}
.res-container {
width: 165px;
height: 350px;
border: 1px solid #cfcfcf;
padding: 10px;
text-align: center;
float: left;
margin-left: 10px;
margin-top: 10px;
}
.res-container h2 {
padding: 0px;
margin: 0 0 10px 0;
height: 115px;
}
.res-container h2 a {
color: #000;
}
#search-bar {
width: 100%;
background: #d3d3d3;
border-bottom: 1px solid #000;
margin-bottom: 30px;
height: 70px;
}
#search-bar input, select {
width: 300px;
font-size: 15px;
font-weight: bold;
border: 0px dashed #000;
padding: 10px;
}
#search-bar select {
width: 100px !important;
}
#page {
width: 20px !important;
}
#results, #search-bar {
padding-top: 20px;
padding-left: 30px;
}
#search-link {
font-size: 25px;
font-weight: bold;
margin-left: 10px;
}
span.caption {
margin-left: 30px;
font-size: 15px;
font-weight: bold;
}
</style>
</head>
<body>
<div id="search-bar">
<span class="caption" style="margin-left: 2px !important">Search:</span>
<input type="text" name="search" id="search">
<span class="caption">Category:</span>
<select name="category" id="category">
<option value="Blended">ALL</option>
<option value="Books">Books</option>
<option value="DVD">DVD</option>
<option value="Apparel">Apparel</option>
<option value="Automotive">Automotive</option>
<option value="Electronics">Electronics</option>
<option value="GourmetFood">GourmetFood</option>
<option value="Kitchen">Kitchen</option>
<option value="Music">Music</option>
<option value="PCHardware">PCHardware</option>
<option value="PetSupplies">PetSupplies</option>
<option value="Software">Software</option>
<option value="SoftwareVideoGames">SoftwareVideoGames</option>
<option value="SportingGoods">SportingGoods</option>
<option value="Tools">Tools</option>
<option value="Toys">Toys</option>
<option value="VHS">VHS</option>
<option value="VideoGames">VideoGames</option>
</select>
<span class="caption">Country:</span>
<select name="country" id="country">
<option value="de">DE</option>
<option value="com">USA</option>
<option value="co.uk">ENG</option>
<option value="ca">CA</option>
<option value="fr">FR</option>
<option value="co.jp">JP</option>
<option value="it">IT</option>
<option value="cn">CN</option>
<option value="es">ES</option>
</select>
<span class="caption">Browse page:</span>
<input type="text" name="page" id="page" value="1">
<a href="#" id="search-link">Search</a>
</div>
<div id="results"></div>
</body>
</html>
<?php
require_once "include/sampleSettings.php";
require_once "vendor/lib/AmazonECS.class.php";
$amazonEcs = new AmazonECS(AWS_API_KEY, AWS_API_SECRET_KEY, 'DE', AWS_ASSOCIATE_TAG);
$amazonEcs->category($_GET['category']);
$amazonEcs->responseGroup('Large');
$amazonEcs->returnType(AmazonECS::RETURN_TYPE_ARRAY);
$amazonEcs->country($_GET['country']);
$page = (!empty($_GET['page'])) ? (int) $_GET['page'] : 1;
if ($page === 0)
$page = 1;
$output = array();
$amazonEcs->page($page);
$response_final = $amazonEcs->search($_GET['q']);
foreach ($response_final['Items']['Item'] as $singleItem)
{
$data = array();
$title = $singleItem['ItemAttributes']['Title'];
if (mb_strlen($title) > 30)
{
$title = substr($title,0, 30);
}
$data['Title'] = $title;
$data['url'] = $singleItem['DetailPageURL'];
$data['img'] = $singleItem['MediumImage']['URL'];
$data['price'] = $singleItem['ItemAttributes']['ListPrice']['FormattedPrice'];
$output[] = $data;
}
echo json_encode($output);
@Exeu

Exeu commented Apr 27, 2013

Copy link
Copy Markdown
Author

Yes. Copy and Paste wouldn't work until you have exactly the same folder structure on your server.

If you are going to build a webapplication i expect that you know what these lines of code means..

So you have to know that:

require_once "include/sampleSettings.php";
require_once "vendor/lib/AmazonECS.class.php";

means that there are 2 files in these subfolders which you want to include here.

and that AWS_API_KEY, AWS_API_SECRET_KEY, AWS_ASSOCIATE_TAG are PHP-constants which have to be setted up before (http://de1.php.net/manual/en/language.constants.php).

the _gaq variable is coming from google analytics. So please comment it out.

@trndee

trndee commented May 13, 2013

Copy link
Copy Markdown

Thank for your work Jan. I can hardly program anything but your code worked perfectly :)

@abedkassem

Copy link
Copy Markdown

hii, i have this problem
Fatal error: Uncaught exception 'Exception' with message 'No Category given: Please set it up before' in /home/content/25/7931125/html/sport/amz/lib/AmazonECS.class.php:99 Stack trace: #0 /home/content/25/7931125/html/sport/amz/search.php(21): AmazonECS->search(NULL) #1 {main} thrown in /home/content/25/7931125/html/sport/amz/lib/AmazonECS.class.php on line 99
plz help me
10x for your support
best regards

@visionez

Copy link
Copy Markdown

Hi There,
Thank you for sharing this code with us.
I put all files in the same folder and change the require link also I have set my keys but when I click on the search link nothing happen, can any help us with that.
Regards

@Inamullahbuksh

Copy link
Copy Markdown

How to get product category name in response please help me

@Inamullahbuksh

Copy link
Copy Markdown

I am add this line in search.php file
$data['category'] = $singleItem['ProductGroup'];

and ama_functions.js

html += '

Category: '+data[x].category+'

';

output:
null

please help me
Thanks!
Inamullah Buksh

@Karthick-Sugumar

Copy link
Copy Markdown

Is it possible to do advance search filters?
Like searching Dress-> mens wear -> t shirts -> size, brand, color etc...

@raj-IV

raj-IV commented Nov 18, 2014

Copy link
Copy Markdown

This is really a nice code to use. Can anybody please tell me from where i can pick the
AWS_API_KEY, AWS_API_SECRET_KEY and AWS_ASSOCIATE_TAG.

Thank You

@rupeshgandhi

Copy link
Copy Markdown

@Exeu thanks for sharing this tutorial but bro i am facing an error.your given code it does not work. so please if it possible to u send me the whole code on my email id.

@vikky401

vikky401 commented Apr 7, 2015

Copy link
Copy Markdown

Thanks this code is working perfectly but displaying only ten pages how can i display more please give me solution

@mathesh

mathesh commented Feb 24, 2016

Copy link
Copy Markdown

It's Working Good.

@sekhar15

sekhar15 commented May 12, 2016

Copy link
Copy Markdown

Its working properly. But i need number of customer reviews like 238 reviews, but i not get that particular number please help me solution. I get the customer reviews link but not getting that particular number

@zeeshanjamil786

Copy link
Copy Markdown

this code not run on wamp server.
when i click on search button not show anything.
i run this code on local wamp server.
please help

@zeeshanjamil786

Copy link
Copy Markdown

exeu please help i request you

@zeeshanjamil786

Copy link
Copy Markdown

Please reply
i request you.
Associate_tag necessary required

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment