Release of source code for my ASIN to EAN, and EAN to ASIN converter!
Following this post, you were many to ask me for the source code of my ASIN to EAN and EAN to ASIN converter, I have decided to make it publicly available. I only dare ask you to do the same if you improve it or add new functionalities, such as a mass conversion (which is still on my roadmap, but if you do it before I’ll be glad not having to do it).
There are basically 3 files that are doing the job: 1 for the processing, 1 for the UI structure, and 1 for CSS styles.
1. processing.php. There are two functions that make REST requests to Amazon’s servers. For this you’ll need to put your own KeyID (or subscription ID) at the beginning of this file.
<?php
/* CUSTOM VARIABLES */
define('KEYID','MyKeyID'); // Put your own KeyID (or Subscription ID) instead of "MyKeyID". You need an Amazon developer account for that.
/* END CUSTOM VARIABLES */
extract($_POST);
switch ($mode) {
case "ASIN2EAN":
ASINItemLookup($ItemId,$locale,'');
break;
case "EAN2ASIN":
EANItemLookup($ItemId,$locale,$SearchIndex);
break;
}
define('AssocTag','httpwwwerwinm-21');
function ASINItemLookup($ItemId, $locale, $SearchIndex){
$request = "http://ecs.amazonaws.".$locale."/onca/xml?Service=AWSECommerceService&amp;amp;amp;AWSAccessKeyId=".KEYID."&amp;amp;amp;AssociateTag=".AssocTag."&amp;amp;amp;Version=2008-03-03&amp;amp;amp;Operation=ItemLookup&amp;amp;amp;ItemId=$ItemId&amp;amp;amp;IdType=ASIN&amp;amp;amp;ResponseGroup=ItemAttributes"; // ecs.amazonaws.com will give you the US locale.
// To see how a xml ouput looks like, use this URL in any browser (insert your KEYID):
//http://ecs.amazonaws.fr/onca/xml?Service=AWSECommerceService&amp;amp;amp;Version=2008-03-03&amp;amp;amp;Operation=ItemLookup&amp;amp;amp;SubscriptionId=KEYID&amp;amp;amp;ItemId=B000PMGQTM&amp;amp;amp;IdType=ASIN&amp;amp;amp;ResponseGroup=ItemAttributes
//The use of `file_get_contents` may not work on all servers because it relies on the ability to open remote URLs using the file manipulation functions.
//PHP gives you the ability to disable this functionality in your php.ini file and many administrators do so for security reasons.
//If your administrator has not done so, you can comment out the following 5 lines of code and uncomment the 6th.
$session = curl_init($request);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($session);
curl_close($session);
//$response = file_get_contents($request);
$parsed_xml = simplexml_load_string($response);
if(isset($parsed_xml->Items->Request->Errors->Error->Code)) {
echo "<em>Warning</em><br />";
echo "<b>Error code</b>: ".$parsed_xml->Items->Request->Errors->Error->Code."<br />";
echo "<b>Description</b>: ".$parsed_xml->Items->Request->Errors->Error->Message;
}
else {
print($parsed_xml->Items->Item->ItemAttributes->EAN);
}
}
function EANItemLookup($ItemId, $locale, $SearchIndex){
$request = "http://ecs.amazonaws.".$locale."/onca/xml?Service=AWSECommerceService&amp;amp;amp;AWSAccessKeyId=".KEYID."&amp;amp;amp;AssociateTag=".AssocTag."&amp;amp;amp;Version=2008-03-03&amp;amp;amp;Operation=ItemLookup&amp;amp;amp;ItemId=$ItemId&amp;amp;amp;IdType=EAN&amp;amp;amp;ResponseGroup=ItemAttributes&amp;amp;amp;SearchIndex=$SearchIndex";
//The use of `file_get_contents` may not work on all servers because it relies on the ability to open remote URLs using the file manipulation functions.
//PHP gives you the ability to disable this functionality in your php.ini file and many administrators do so for security reasons.
//If your administrator has not done so, you can comment out the following 5 lines of code and uncomment the 6th.
$session = curl_init($request);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($session);
curl_close($session);
//$response = file_get_contents($request);
$parsed_xml = simplexml_load_string($response);
if(isset($parsed_xml->Items->Request->Errors->Error->Code)) {
echo "<em>Warning</em><br />";
echo "<b>Error code</b>: ".$parsed_xml->Items->Request->Errors->Error->Code."<br />";
echo "<b>Description</b>: ".$parsed_xml->Items->Request->Errors->Error->Message;
}
else {
print($parsed_xml->Items->Item->ASIN);
}
}
?>
2. Here is the index.php file with the UI using AJAX.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>ASIN to EAN converter</title>
<link href="styles.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
var xhr_processing = null;
String.prototype.stripSpaces = function( ){
//return this.replace( /\s/g, "" );
return this.replace(/[^a-zA-Z0-9]/g,'');
};
function createObject_xhr_processing() {
if(window.XMLHttpRequest) // Firefox
xhr_processing = new XMLHttpRequest();
else if(window.ActiveXObject) // Internet Explorer
xhr_processing = new ActiveXObject("Microsoft.XMLHTTP");
else { // XMLHttpRequest non support par le navigateur
alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
}
}
//B000PMGQTM
function convertFunction(mode) {
xhr_processing.open("POST", "processing.php", true);
xhr_processing.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ItemId = encodeURIComponent(document.getElementById("ItemIdInput"+mode).value.stripSpaces());
locale = encodeURIComponent(document.getElementById("localeInput"+mode).value);
SearchIndex = encodeURIComponent(document.getElementById("SearchIndexInput").value);
Datagram = "ItemId="+ItemId+"&amp;amp;amp;locale="+locale+"&amp;amp;amp;SearchIndex="+SearchIndex+"&amp;amp;amp;mode="+mode;
xhr_processing.send(Datagram);
document.getElementById("ItemIdOutput"+mode).value = "Please wait..."
xhr_processing.onreadystatechange = function() {
if(xhr_processing.readyState == 4) {
var RegExp1 = /.*Warning.*/;
if (RegExp1.test(xhr_processing.responseText)) {
document.getElementById("ItemIdOutput"+mode).value = "An error occurred."
document.getElementById("debug"+mode).innerHTML = xhr_processing.responseText;
}
else {
element = document.getElementById("ItemIdOutput"+mode);
element.value = xhr_processing.responseText;
document.getElementById("debug"+mode).innerHTML = "";
//Si DIV : element.innerHTML = xhr_processing.responseText;
//innerHTML est compatible mais ne respecte pas les spécifications DOM
}
}
}
}
function retournepressepapier() {
return window.clipboardData.getData("Text").stripSpaces();
}
function copiedanspressepapier(mode) {
window.clipboardData.setData("Text",document.getElementById("ItemIdOutput"+mode).value);
}
function dispOnglet(onglet) {
document.getElementById(onglet).style.display = "block";
if (onglet == "ASIN2EAN") {
document.getElementById("EAN2ASIN").style.display = "none";
}
if (onglet == "EAN2ASIN") {
document.getElementById("ASIN2EAN").style.display = "none";
}
}
function changeSearchIndexes(locale) {
document.getElementById("SearchIndexInput").options.length = 0;
var SearchIndexes = new Array();
if (locale == "fr") {
SearchIndexes = ["Blended","Books","Classical","DSHelp","DVD","Electronics","ForeignBooks","Kitchen","Music","MusicTracks","Software","SoftwareVideoGames","Toys","VHS","Video","VideoGames","Watches"];
}
else if (locale == "de") {
SearchIndexes = ["Apparel","Baby","Blended","Books","Classical","DSHelp","DVD","Electronics","ForeignBooks","HealthPersonalCare","HomeGarden","Kitchen","Magazines","Music","MusicTracks","OutdoorLiving","PCHardware","Photo","Software","SoftwareVideoGames","SportingGoods","Tools","Toys","VHS","Video","VideoGames","Watches"];
}
else {
SearchIndexes = ["All"];
}
for (i=0; i<SearchIndexes.length; i++) {
document.getElementById('SearchIndexInput').options[i]=new Option(SearchIndexes[i],SearchIndexes[i]);
}
}
</script>
</head>
<body onload='createObject_xhr_processing(); dispOnglet("ASIN2EAN"); changeSearchIndexes("com")'>
<div class="ongletButton" onMouseOver="this.style.background='#CCC'" onMouseOut="this.style.background='#DDD'" onClick='dispOnglet("ASIN2EAN");'><a>ASIN to EAN</a></div><div class="ongletButton" onMouseOver="this.style.background='#CCC'" onMouseOut="this.style.background='#DDD'" onClick='dispOnglet("EAN2ASIN");'><a>EAN to ASIN</a></div>
<div class="onglet" id="ASIN2EAN">
<h1>ASIN to EAN converter</h1>
<form name='converterForm'>
<b>ASIN code</b><br />
<input type='text' id='ItemIdInputASIN2EAN' size='40' onblur="this.value = this.value.stripSpaces()"/><input type="button" value="Paste" onclick="document.getElementById('ItemIdInputASIN2EAN').value = retournepressepapier();"><br />
<input type='button' name='convertButton' value='Convert to EAN' onclick='convertFunction("ASIN2EAN");' />
<select name='localeInput' id='localeInputASIN2EAN'>
<option value='fr'>France</option>
<option value='com'>USA</option>
<option value='de'>Deutschland</option>
</select>
<br />
<b>EAN code</b><br />
<input style="background-color:#DDD" type='text' name='ItemIdOutput' id='ItemIdOutputASIN2EAN' size='40' onFocus="select()" readonly /><input type="button" value="Copy" onclick="copiedanspressepapier('ASIN2EAN');"><br />
</form>
<div id="debugASIN2EAN" width="200px"></div>
</div>
<div class="onglet" id="EAN2ASIN" style="display:none;">
<h1>EAN to ASIN converter</h1>
<form name='converterForm'>
<b>EAN code</b><br />
<input type='text' id='ItemIdInputEAN2ASIN' size='40' onblur="this.value = this.value.stripSpaces()"/><input type="button" value="Paste" onclick="document.getElementById('ItemIdInputEAN2ASIN').value = retournepressepapier();"><br />
<input type='button' name='convertButton' value='Convert to ASIN' onclick='convertFunction("EAN2ASIN");' />
<select name='localeInput' id='localeInputEAN2ASIN' onChange="changeSearchIndexes(this.value)">
<option value='fr'>France</option>
<option value='com' selected>USA</option>
<option value='de'>Deutschland</option>
</select>
<br />
<select name='SearchIndexInput' id='SearchIndexInput' style="width: 150px;">
</select>
<br />
<b>ASIN code</b><br />
<input style="background-color:#DDD" type='text' name='ItemIdOutput' id='ItemIdOutputEAN2ASIN' size='40' onFocus="select()" readonly /><input type="button" value="Copy" onclick="copiedanspressepapier('EAN2ASIN');"><br />
</form>
<div id="debugEAN2ASIN" width="200px"></div>
</div>
</body>
</html>
3. And finally, our beloved styles.css:
/* standard elements */
html {min-height: 100%;}
* {
margin: 0;
padding: 0;
}
a {color: #048;}
a:hover {color: #06C;}
body {
background: #E7E7E2 url('img/body.jpg') no-repeat center top;
color: #444;
font: normal 62.5% Tahoma,sans-serif;
padding-top: 20px;
padding-left: 20px;
}
p,code,ul {padding-bottom: 1.2em;}
li {list-style: none;}
h1 {font: normal 1.8em Tahoma,sans-serif;}
h2 {font: bold 1.4em sans-serif;}
h3 {font: bold 1em Tahoma,sans-serif;}
form,input {margin: 0; padding: 0; display: inline;}
code {
background: #FFF;
border: 1px solid #EEE;
border-left: 6px solid #CCC;
color: #666;
display: block;
font: normal 1em Tahoma,sans-serif;
line-height: 1.6em;
margin-bottom: 12px;
padding: 8px 10px;
white-space: pre;
}
blockquote {
background: url(img/quote.gif) no-repeat;
display: block;
font-weight: bold;
padding-left: 28px;
}
h1,h2,h3 {padding-top: 6px; color: #553; margin-bottom: 4px;}
/*Custom design */
.ongletButton {
width: 175px;
float: left;
border-top: 0px;
border-right: 1px;
border-bottom: 1px;
border-left: 0px;
border-color: #555;
border-style: solid;
/* display: inline; */
cursor: pointer;
text-align: center;
background-color: #DDD;
}
.ongletButton a {
color: black;
font-weight: bold;
}
.onglet {
width: 350px;
float:none;
clear: both;
}
Feel free to make any suggestions to improve it! Here again is the final output, brought to you by erwinmayer.com labs: