Skip to content

Instantly share code, notes, and snippets.

@Exeu
Last active June 20, 2017 16:15
Show Gist options
  • Save Exeu/4674423 to your computer and use it in GitHub Desktop.
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);
@erj1
Copy link

erj1 commented Apr 24, 2013

@Exeu, Just stumbled upon this after visiting your Amazon ECS PHP Library. I think the previous developers are right to a degree. There are a few items in the code above that prevents the simple drag-n-drop onto my own web server and have it running.

In the JavaScript file:

  • I am getting an error for the variable _gaq. Not sure where this is coming from.
  • Depending where you put the above code, the url in the ajax call should be updated.

In the HTML file:

  • Depending on where you put the above code, the url to the JavaScript file should be updated.

In the PHP file:

  • not sure what should be included in the file "include/sampleSettings.php" -- maybe I need to RTFM. Plus not having this file precludes the drag-n-drop.

I maybe completely lost in the purpose of the above gist, if so forget my suggestions.

In any case, keep up the good work this library is awesome and well-built!

@other_developers, If you are wanting to use the above code, take note of my above comments that prevent a simple drag-n-drop into your web server to quickly see the functionality of the library. Also use composer to install the library [ https://github.com/Exeu/Amazon-ECS-PHP-Library/blob/master/composer.json ]

@Exeu
Copy link
Author

Exeu commented Apr 27, 2013

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
Copy link

trndee commented May 13, 2013

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

@abedkassem
Copy link

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

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

How to get product category name in response please help me

@Inamullahbuksh
Copy link

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

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

@raj-IV
Copy link

raj-IV commented Nov 18, 2014

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

@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
Copy link

vikky401 commented Apr 7, 2015

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

@mathesh
Copy link

mathesh commented Feb 24, 2016

It's Working Good.

@sekhar15
Copy link

sekhar15 commented May 12, 2016

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

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

exeu please help i request you

@zeeshanjamil786
Copy link

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