Find Book

[insert_php]

$response = getAmazonRank(“com”, “B076VXXRPR”);

function getAmazonRank($region, $asin) {

$xml = aws_signed_request($region, array(
“Operation” => “ItemLookup”,
“ItemId” => $asin,
“IncludeReviewsSummary” => False,
“ResponseGroup” => “Medium,OfferSummary”,
));

$item = $xml->Items->Item;
$title = htmlentities((string) $item->ItemAttributes->Title);
$url = htmlentities((string) $item->DetailPageURL);
$rank = htmlentities((string) $item->ProductDetails->AmazonBestSellerRank);

if ($qty !== “0”) {
$response = array(
“rank” => $rank,
“url” => $url,
“title” => $title
);
}

return $response;
}

function getPage($url) {

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$html = curl_exec($curl);
curl_close($curl);
return $html;
}

function aws_signed_request($region, $params) {

$public_key = “AKIAIFYLJOG7GTN76CAA”;
$private_key = “x9k36o2l2TTdWxhUeEM+Z31W1V2HPFxnpvdthpZs”;

$method = “GET”;
$host = “ecs.amazonaws.” . $region;
$host = “webservices.amazon.” . $region;
$uri = “/onca/xml”;

$params[“Service”] = “AWSECommerceService”;
$params[“AssociateTag”] = “tridentpublis-20”;
$params[“AWSAccessKeyId”] = $public_key;
$params[“Timestamp”] = gmdate(“Y-m-d\TH:i:s\Z”);
$params[“Version”] = “2011-08-01”;

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;
$signature = base64_encode(hash_hmac(“sha256”, $string_to_sign, $private_key, True));
$signature = str_replace(“%7E”, “~”, rawurlencode($signature));

$request = “http://” . $host . $uri . “?” . $canonicalized_query . “&Signature=” . $signature;
$response = getPage($request);

var_dump($response);

$pxml = @simplexml_load_string($response);
if ($pxml === False) {
return False;// no xml
} else {
return $pxml;
}
}

[/insert_php]