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
| <?php
/**
* Query Amazon about a particular book by ISBN and obtain metadata.
* The author disclaims all copyright and places this in the public domain.
*
* Amazon's Terms of Use for this service require you to:
* - Send no more than 1 request every second
* - Direct traffic to them in some way. You can use the URL provided in the
* resulting metadata to achieve this.
*/
class ISBN {
function getMetadataFromIsbn($isbn) {
// Get your own accesskey at http://aws.amazon.com/
$awsAccessKeyID = 'YOUR_ACCESS_KEY_ID_HERE';
$awsSecretKey = 'YOUR_SECRET_KEY_HERE';
$awsAssociateTag = 'YOUR_ASSOCIATE_TAG_HERE';
$host = 'ecs.amazonaws.com';
$path = '/onca/xml';
$args = array(
'AssociateTag' => $awsAssociateTag,
'AWSAccessKeyId' => $awsAccessKeyID,
'IdType' => 'ISBN',
'ItemId' => $isbn,
'Operation' => 'ItemLookup',
'ResponseGroup' => 'Medium',
'SearchIndex' => 'Books',
'Service' => 'AWSECommerceService',
'Timestamp' => gmdate('Y-m-d\TH:i:s\Z'),
'Version'=> '2009-01-06'
);
ksort($args);
$parts = array();
foreach(array_keys($args) as $key) {
$parts[] = $key . "=" . $args[$key];
}
// Construct the string to sign
$stringToSign = "GET\n" . $host . "\n" . $path . "\n" . implode("&", $parts);
$stringToSign = str_replace('+', '%20', $stringToSign);
$stringToSign = str_replace(':', '%3A', $stringToSign);
$stringToSign = str_replace(';', urlencode(';'), $stringToSign);
// Sign the request
$signature = hash_hmac("sha256", $stringToSign, $awsSecretKey, TRUE);
// Base64 encode the signature and make it URL safe
$signature = base64_encode($signature);
$signature = str_replace('+', '%2B', $signature);
$signature = str_replace('=', '%3D', $signature);
// Construct the URL
$url = 'http://' . $host . $path . '?' . implode("&", $parts) . "&Signature=" . $signature;
$rawData = file_get_contents($url);
$metadata = simplexml_load_string($rawData);
if (isset($metadata->Items->Request->Errors)) {
return $metadata->Items->Request->Errors;
} else {
return $metadata->Items->Item;
}
}
}
?> |
Partager