Created
August 4, 2010 14:27
-
-
Save hubgit/508210 to your computer and use it in GitHub Desktop.
fetch item details and track listing from Amazon for an ASIN
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// documentation: http://developer.amazonwebservices.com/connect/kbcategory.jspa?categoryID=19 | |
ini_set('arg_separator.output', '&'); // for http_build_query | |
class Amazon { | |
// Fill these in; the associate tag is optional | |
public $key = ''; // https://aws-portal.amazon.com/gp/aws/developer/account/index.html?action=access-key | |
public $secret = ''; // https://aws-portal.amazon.com/gp/aws/developer/account/index.html?action=access-key | |
public $tag = ''; // https://affiliate-program.amazon.com/ | |
public $host = 'ecs.amazonaws.com'; | |
private $path = '/onca/xml'; | |
function sign($params){ | |
uksort($params, 'strnatcmp'); | |
foreach ($params as $key => &$value) | |
$value = $key . '=' . rawurlencode($value); // http_build_query uses urlencode rather than rawurlencode | |
return base64_encode(hash_hmac('sha256', implode("\n", array('GET', $this->host, $this->path, implode('&', $params))), $this->secret, TRUE)); | |
} | |
function lookup($asin) { | |
$params = array( | |
'Service' => 'AWSECommerceService', | |
'Version' => '2009-10-01', | |
'Timestamp' => date(DATE_ISO8601), | |
'AWSAccessKeyId' => $this->key, | |
'AssociateTag' => $this->tag, | |
'Operation' => 'ItemLookup', | |
'IdType' => 'ASIN', | |
'ItemId' => $asin, | |
'ResponseGroup' => 'ItemAttributes,Images,Tracks', | |
); | |
$params['Signature'] = $this->sign($params); | |
$xml = simplexml_load_file('http://' . $this->host . $this->path . '?' . http_build_query($params)); | |
if (!is_object($xml) || (string) $xml->Items->Request->IsValid != 'True') | |
return array(); | |
$meta = NULL; | |
if (!empty($xml->Items->Item)){ | |
$item = $xml->Items->Item; | |
$tracks = array(); | |
foreach ($item->Tracks->Disc->Track as $track) // won't work properly with multiple discs; need to use DOM to cover those | |
$tracks[] = (string) $track; | |
$meta = array( | |
'artist' => (string) $item->ItemAttributes->Artist, | |
'title' => (string) $item->ItemAttributes->Title, | |
'info' => (string) $item->DetailPageURL, | |
'tracks' => $tracks, | |
'images' => array(), | |
); | |
foreach (array('LargeImage', 'MediumImage', 'SmallImage') as $image) | |
if (isset($item->{$image})) | |
$meta['images'][$image] = (string) $item->{$image}->URL; | |
} | |
return $meta; | |
} | |
} | |
$asin = 'B000003TA4'; | |
$api = new Amazon; | |
$items = $api->lookup($asin); | |
print_r($items); | |
/* | |
Array | |
( | |
[artist] => Nirvana | |
[title] => Nevermind | |
[info] => http://www.amazon.com/Nevermind-Nirvana/dp/B000003TA4... | |
[tracks] => Array | |
( | |
[0] => Smells Like Teen Spirit | |
[1] => In Bloom | |
[2] => Come as You Are | |
[3] => Breed | |
[4] => Lithium | |
[5] => Polly | |
[6] => Territorial Pissings | |
[7] => Drain You | |
[8] => Lounge Act | |
[9] => Stay Away | |
[10] => On a Plain | |
[11] => Something in the Way | |
) | |
[images] => Array | |
( | |
[LargeImage] => http://ecx.images-amazon.com/images/I/514q1roTpwL.jpg | |
[MediumImage] => http://ecx.images-amazon.com/images/I/514q1roTpwL._SL160_.jpg | |
[SmallImage] => http://ecx.images-amazon.com/images/I/514q1roTpwL._SL75_.jpg | |
) | |
) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment