55 * @author Marc Littlemore
66 * @link http://www.marclittlemore.com
77 *
8- */
8+ */
99
1010class AmazonAPI
1111{
@@ -35,7 +35,7 @@ class AmazonAPI
3535
3636 // AWS associate tag
3737 private $ m_associateTag = NULL ;
38-
38+
3939 // Valid names that can be used for search
4040 private $ mValidSearchNames = array (
4141 'All ' ,'Apparel ' ,'Appliances ' ,'Automotive ' ,'Baby ' ,'Beauty ' ,'Blended ' ,'Books ' ,'Classical ' ,'DVD ' ,'Electronics ' ,'Grocery ' ,'HealthPersonalCare ' ,'HomeGarden ' ,'HomeImprovement ' ,'Jewelry ' ,'KindleStore ' ,'Kitchen ' ,'Lighting ' ,'Marketplace ' ,'MP3Downloads ' ,'Music ' ,'MusicTracks ' ,'MusicalInstruments ' ,'OfficeProducts ' ,'OutdoorLiving ' ,'Outlet ' ,'PetSupplies ' ,'PCHardware ' ,'Shoes ' ,'Software ' ,'SoftwareVideoGames ' ,'SportingGoods ' ,'Tools ' ,'Toys ' ,'VHS ' ,'Video ' ,'VideoGames ' ,'Watches ' ,
@@ -58,7 +58,7 @@ public function __construct( $keyId, $secretKey, $associateTag )
5858 * Enable or disable SSL endpoints
5959 *
6060 * @param useSSL True if using SSL, false otherwise
61- *
61+ *
6262 * @return None
6363 */
6464 public function SetSSL ( $ useSSL = true )
@@ -70,7 +70,7 @@ public function SetSSL( $useSSL = true )
7070 * Enable or disable retrieving items array rather than XML
7171 *
7272 * @param retrieveArray True if retrieving as array, false otherwise.
73- *
73+ *
7474 * @return None
7575 */
7676 public function SetRetrieveAsArray ( $ retrieveArray = true )
@@ -83,7 +83,7 @@ public function SetRetrieveAsArray( $retrieveArray = true )
8383 *
8484 * @param locale Set to a valid AWS locale - see link below.
8585 * @link http://docs.amazonwebservices.com/AWSECommerceService/latest/DG/Locales.html
86- *
86+ *
8787 * @return None
8888 */
8989 public function SetLocale ( $ locale )
@@ -94,7 +94,7 @@ public function SetLocale( $locale )
9494 // If not then just assume it's US
9595 $ locale = 'us ' ;
9696 }
97-
97+
9898 // Set the URL for this locale
9999 $ this ->m_locale = $ locale ;
100100
@@ -104,12 +104,12 @@ public function SetLocale( $locale )
104104 else
105105 $ this ->m_amazonUrl = 'http:// ' . $ this ->m_localeTable [$ locale ];
106106 }
107-
107+
108108 /**
109109 * Return valid search names
110110 *
111111 * @param None
112- *
112+ *
113113 * @return Array Array of valid string names
114114 */
115115 public function GetValidSearchNames ()
@@ -121,7 +121,7 @@ public function GetValidSearchNames()
121121 * Return data from AWS
122122 *
123123 * @param url URL request
124- *
124+ *
125125 * @return mixed SimpleXML object or false if failure.
126126 */
127127 private function MakeRequest ( $ url )
@@ -154,26 +154,26 @@ private function MakeRequest( $url )
154154
155155 // Interpret data as XML
156156 $ parsedXml = simplexml_load_string ( $ response );
157-
157+
158158 return ( $ parsedXml );
159159 }
160-
160+
161161 /**
162162 * Search for items
163163 *
164164 * @param keywords Keywords which we're requesting
165165 * @param searchIndex Name of search index (category) requested. NULL if searching all.
166166 * @param sortBySalesRank True if sorting by sales rank, false otherwise.
167- * @param condition Condition of item. Valid conditions : Used, Collectible, Refurbished, All
168- *
167+ * @param condition Condition of item. Valid conditions : Used, Collectible, Refurbished, All
168+ *
169169 * @return mixed SimpleXML object, array of data or false if failure.
170170 */
171171 public function ItemSearch ( $ keywords , $ searchIndex = NULL , $ sortBySalesRank = true , $ condition = 'New ' )
172172 {
173173 // Set the values for some of the parameters.
174174 $ operation = "ItemSearch " ;
175175 $ responseGroup = "ItemAttributes,Offers,Images " ;
176-
176+
177177 //Define the request
178178 $ request = $ this ->GetBaseUrl ()
179179 . "&Operation= " . $ operation
@@ -199,12 +199,12 @@ public function ItemSearch( $keywords, $searchIndex = NULL, $sortBySalesRank = t
199199
200200 // Need to sign the request now
201201 $ signedUrl = $ this ->GetSignedRequest ( $ this ->m_secretKey , $ request );
202-
202+
203203 // Get the response from the signed URL
204204 $ parsedXml = $ this ->MakeRequest ( $ signedUrl );
205205 if ( $ parsedXml === false )
206206 return ( false );
207-
207+
208208 if ( $ this ->m_retrieveArray )
209209 {
210210 $ items = $ this ->RetrieveItems ( $ parsedXml );
@@ -216,13 +216,13 @@ public function ItemSearch( $keywords, $searchIndex = NULL, $sortBySalesRank = t
216216
217217 return ( $ items );
218218 }
219-
219+
220220 /**
221221 * Lookup items from ASINs
222222 *
223223 * @param asinList Either a single ASIN or an array of ASINs
224224 * @param onlyFromAmazon True if only requesting items from Amazon and not 3rd party vendors
225- *
225+ *
226226 * @return mixed SimpleXML object, array of data or false if failure.
227227 */
228228 public function ItemLookup ( $ asinList , $ onlyFromAmazon = false )
@@ -232,14 +232,14 @@ public function ItemLookup( $asinList, $onlyFromAmazon = false )
232232 {
233233 $ asinList = implode ( ', ' , $ asinList );
234234 }
235-
235+
236236 // Set the values for some of the parameters.
237237 $ operation = "ItemLookup " ;
238238 $ responseGroup = "ItemAttributes,Offers,Reviews,Images,EditorialReview " ;
239-
239+
240240 // Determine whether we just want Amazon results only or not
241241 $ merchantId = ( $ onlyFromAmazon == true ) ? 'Amazon ' : 'All ' ;
242-
242+
243243 $ reviewSort = '-OverallRating ' ;
244244 //Define the request
245245 $ request = $ this ->GetBaseUrl ()
@@ -248,15 +248,15 @@ public function ItemLookup( $asinList, $onlyFromAmazon = false )
248248 . "&ResponseGroup= " . $ responseGroup
249249 . "&ReviewSort= " . $ reviewSort
250250 . "&MerchantId= " . $ merchantId ;
251-
251+
252252 // Need to sign the request now
253253 $ signedUrl = $ this ->GetSignedRequest ( $ this ->m_secretKey , $ request );
254-
254+
255255 // Get the response from the signed URL
256256 $ parsedXml = $ this ->MakeRequest ( $ signedUrl );
257257 if ( $ parsedXml === false )
258258 return ( false );
259-
259+
260260 if ( $ this ->m_retrieveArray )
261261 {
262262 $ items = $ this ->RetrieveItems ( $ parsedXml );
@@ -272,7 +272,7 @@ public function ItemLookup( $asinList, $onlyFromAmazon = false )
272272 * Basic method to retrieve only requested item data as an array
273273 *
274274 * @param responseXML XML data to be passed
275- *
275+ *
276276 * @return Array Array of item data. Empty array if not found
277277 */
278278 private function RetrieveItems ( $ responseXml )
@@ -307,7 +307,7 @@ private function RetrieveItems( $responseXml )
307307 $ item ['url ' ] = (string ) $ responseItem ->DetailPageURL ;
308308 $ item ['rrp ' ] = ( (float ) $ responseItem ->ItemAttributes ->ListPrice ->Amount ) / 100.0 ;
309309 $ item ['title ' ] = (string ) $ responseItem ->ItemAttributes ->Title ;
310-
310+
311311 if ( $ responseItem ->OfferSummary )
312312 {
313313 $ item ['lowestPrice ' ] = ( (float ) $ responseItem ->OfferSummary ->LowestNewPrice ->Amount ) / 100.0 ;
@@ -325,14 +325,14 @@ private function RetrieveItems( $responseXml )
325325 array_push ( $ items , $ item );
326326 }
327327
328- return ( $ items );
328+ return ( $ items );
329329 }
330330
331331 /**
332332 * Determines the base address of the request
333333 *
334334 * @param None
335- *
335+ *
336336 * @return string Base URL of AWS request
337337 */
338338 private function GetBaseUrl ()
@@ -343,12 +343,12 @@ private function GetBaseUrl()
343343 . "?Service=AWSECommerceService "
344344 . "&AssociateTag= " . $ this ->m_associateTag
345345 . "&AWSAccessKeyId= " . $ this ->m_keyId ;
346-
346+
347347 return ( $ request );
348348 }
349-
349+
350350 /**
351- * This function will take an existing Amazon request and change it so that it will be usable
351+ * This function will take an existing Amazon request and change it so that it will be usable
352352 * with the new authentication.
353353 *
354354 * @param string $secret_key - your Amazon AWS secret key
@@ -362,42 +362,42 @@ private function GetSignedRequest( $secret_key, $request, $access_key = false, $
362362 {
363363 // Get a nice array of elements to work with
364364 $ uri_elements = parse_url ($ request );
365-
365+
366366 // Grab our request elements
367367 $ request = $ uri_elements ['query ' ];
368-
368+
369369 // Throw them into an array
370370 parse_str ($ request , $ parameters );
371-
371+
372372 // Add the new required paramters
373373 $ parameters ['Timestamp ' ] = gmdate ( "Y-m-d\TH:i:s\Z " );
374374 $ parameters ['Version ' ] = $ version ;
375375 if ( strlen ($ access_key ) > 0 )
376376 {
377377 $ parameters ['AWSAccessKeyId ' ] = $ access_key ;
378- }
379-
378+ }
379+
380380 // The new authentication requirements need the keys to be sorted
381381 ksort ( $ parameters );
382-
382+
383383 // Create our new request
384384 foreach ( $ parameters as $ parameter => $ value )
385385 {
386386 // We need to be sure we properly encode the value of our parameter
387387 $ parameter = str_replace ( "%7E " , "~ " , rawurlencode ( $ parameter ) );
388388 $ value = str_replace ( "%7E " , "~ " , rawurlencode ( $ value ) );
389389 $ request_array [] = $ parameter . '= ' . $ value ;
390- }
391-
390+ }
391+
392392 // Put our & symbol at the beginning of each of our request variables and put it in a string
393393 $ new_request = implode ( '& ' , $ request_array );
394-
394+
395395 // Create our signature string
396396 $ signature_string = "GET \n{$ uri_elements ['host ' ]}\n{$ uri_elements ['path ' ]}\n{$ new_request }" ;
397-
397+
398398 // Create our signature using hash_hmac
399399 $ signature = urlencode ( base64_encode ( hash_hmac ( 'sha256 ' , $ signature_string , $ secret_key , true ) ) );
400-
400+
401401 // Return our new request
402402 return "http:// {$ uri_elements ['host ' ]}{$ uri_elements ['path ' ]}? {$ new_request }&Signature= {$ signature }" ;
403403 }
@@ -406,7 +406,7 @@ private function GetSignedRequest( $secret_key, $request, $access_key = false, $
406406 * Adds error to an error array
407407 *
408408 * @param error Error string
409- *
409+ *
410410 * @return None
411411 */
412412 private function AddError ( $ error )
@@ -418,12 +418,12 @@ private function AddError( $error )
418418 * Returns array of errors
419419 *
420420 * @param None
421- *
421+ *
422422 * @return Array Array of errors. Empty array if none found
423423 */
424424 public function GetErrors ()
425425 {
426426 return ( $ this ->mErrors );
427427 }
428428}
429- ?>
429+ ?>
0 commit comments