forked from prabintp/Amazon-Codeigniter-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
amazon_api.php
263 lines (211 loc) · 8.88 KB
/
amazon_api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
// -------------------------------------------------------------------------------------------------
/**
* Amazon Product Advertising API library
*
* This is a very basic libary allowing you to search the Amazon Product Advertising API
*
* Based on and inspired by original CI Amazon library by Zac G. (http://zgordon.org) and on code by Richard Cummings (http://richardcummings.info/the-request-must-contain-the-parameter-signature-quick-fix-for-your-php-rest-requests/)
*
* @package CodeIgniter
* @subpackage Libraries
* @author prabintp <http://www.crashcoder.com/contact/>
* @version 2.0
* @license http://www.opensource.org/licenses/bsd-license.php BSD licensed.
*
*/
// -------------------------------------------------------------------------------------------------
class Amazon_api
{
/* Your Amazon Access Key Id
* @access private
* @var string
*/
var $public_key = "<AMAZON ACCESS KEY ID>";
/**
* Your Amazon Secret Access Key
* @access private
* @var string
*/
var $private_key = "<AMAZON SECRET KEY>";
/**
* Your Amazon Associate Tag
* @access private
* @var string
*/
var $associate_tag = "<ASSOC TAG>";
/*
Only three categories are listed here.
More categories can be found here:
http://docs.amazonwebservices.com/AWSECommerceService/latest/DG/APPNDX_SearchIndexValues.html
*/
function Amazon_api()
{
}
function aws_signed_request($region,$params,$public_key,$private_key,$associate_tag)
{
$method = "GET";
$host = "ecs.amazonaws.".$region; // must be in small case
$uri = "/onca/xml";
$params["Service"] = "AWSECommerceService";
$params["AWSAccessKeyId"] = $public_key;
$params["AssociateTag"] = $associate_tag;
$params["Timestamp"] = gmdate("Y-m-d\TH:i:s\Z");
$params["Version"] = "2009-03-31";
/* The params need to be sorted by the key, as Amazon does this at
their end and then generates the hash of the same. If the params
are not in order then the generated hash will be different thus
failing the authetication process.
*/
ksort($params);
$canonicalized_query = array();
foreach ($params as $param=>$value)
{
$param = str_replace("%7E", "~", rawurlencode($param));
$value = str_replace("%7E", "~", rawurlencode($value));
$canonicalized_query[] = $param."=".$value;
}
$canonicalized_query = implode("&", $canonicalized_query);
$string_to_sign = $method."\n".$host."\n".$uri."\n".$canonicalized_query;
/* calculate the signature using HMAC with SHA256 and base64-encoding.
The 'hash_hmac' function is only available from PHP 5 >= 5.1.2.
*/
$signature = base64_encode(hash_hmac("sha256", $string_to_sign, $private_key, True));
/* encode the signature for the request */
$signature = str_replace("%7E", "~", rawurlencode($signature));
/* create request */
$request = "http://".$host.$uri."?".$canonicalized_query."&Signature=".$signature;
/* I prefer using CURL */
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$request);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$xml_response = curl_exec($ch);
/* If cURL doesn't work for you, then use the 'file_get_contents'
function as given below.
*/
if ($xml_response === False)
{
return False;
}
else
{
/* parse XML */
$parsed_xml = @simplexml_load_string($xml_response);
return ($parsed_xml === False) ? False : $parsed_xml;
}
}
/**
* Check if the xml received from Amazon is valid
*
* @param mixed $response xml response to check
* @return bool false if the xml is invalid
* @return mixed the xml response if it is valid
* @return exception if we could not connect to Amazon
*/
private function verifyXmlResponse($response)
{
if ($response === False)
{
throw new Exception("Could not connect to Amazon");
}
else
{
if (isset($response->Items->Item->ItemAttributes->Title))
{
return ($response);
}
else
{
throw new Exception("Invalid xml response.");
}
}
}
/**
* Query Amazon with the issued parameters
*
* @param array $parameters parameters to query around
* @return simpleXmlObject xml query response
*/
private function queryAmazon($parameters)
{
return $this->aws_signed_request("com", $parameters, $this->public_key,$this->private_key, $this->associate_tag);
}
/**
* Return details of products searched by various types
*
* @param string $search search term
* @param string $category search category
* @param string $searchType type of search
* @return mixed simpleXML object
*/
public function searchProducts($search, $category, $searchType = "UPC")
{
$allowedTypes = array("UPC", "TITLE", "ARTIST", "KEYWORD");
$allowedCategories = array("Music", "DVD", "VideoGames");
switch($searchType)
{
case "UPC" : $parameters = array("Operation" => "ItemLookup",
"ItemId" => $search,
"SearchIndex" => $category,
"IdType" => "UPC",
"ResponseGroup" => "Medium");
break;
case "TITLE" : $parameters = array("Operation" => "ItemSearch",
"Title" => $search,
"SearchIndex" => $category,
"ResponseGroup" => "Medium");
break;
}
$xml_response = $this->queryAmazon($parameters);
return $this->verifyXmlResponse($xml_response);
}
/**
* Return details of a product searched by UPC
*
* @param int $upc_code UPC code of the product to search
* @param string $product_type type of the product
* @return mixed simpleXML object
*/
public function getItemByUpc($upc_code, $product_type)
{
$parameters = array("Operation" => "ItemLookup",
"ItemId" => $upc_code,
"SearchIndex" => $product_type,
"IdType" => "UPC",
"ResponseGroup" => "Medium");
$xml_response =$this->queryAmazon($parameters);
return $this->verifyXmlResponse($xml_response);
}
/**
* Return details of a product searched by ASIN
*
* @param int $asin_code ASIN code of the product to search
* @return mixed simpleXML object
*/
public function getItemByAsin($asin_code)
{
$parameters = array("Operation" => "ItemLookup",
"ItemId" => $asin_code,
"ResponseGroup" => "Medium");
$xml_response = $this->queryAmazon($parameters);
return $this->verifyXmlResponse($xml_response);
}
/**
* Return details of a product searched by keyword
*
* @param string $keyword keyword to search
* @param string $product_type type of the product
* @return mixed simpleXML object
*/
public function getItemByKeyword($keyword, $product_type)
{
$parameters = array("Operation" => "ItemSearch",
"Keywords" => $keyword,
"SearchIndex" => $product_type);
$xml_response = $this->queryAmazon($parameters);
return $this->verifyXmlResponse($xml_response);
}
}
?>