Get Amazon Product Data using PHP + Amazon API

get_amazon_products('ASIN_ID');
get_amazon_products('ASIN_ID,ASIN_ID'); // Up to 10 asins
function get_amazon_products($asin){ 
    
    // Your AWS Access Key ID, as taken from the AWS Your Account page
	$aws_access_key_id = 'your-access-key-id';
	
	// Your AWS Secret Key corresponding to the above ID, as taken from the AWS Your Account page
	$aws_secret_key = 'your-secret-key';
	
	$aws_secret_code = 'your-secret-code';
	
	
	// The region you are interested in
	$endpoint = "webservices.amazon.com";
	
	$uri = "/onca/xml";
	
	$params = array(
	    "Service" => "AWSECommerceService",
	    "Operation" => "ItemLookup",
	    "AWSAccessKeyId" => '' . $aws_access_key_id,
	    "AssociateTag" => $aws_secret_code,
	    "ItemId" => $asin,
	    "IdType" => "ASIN",
	    "ResponseGroup" => "ItemAttributes,Offers,OfferSummary,OfferFull,Images"
	);
	
	// Set current timestamp if not set
	if (!isset($params["Timestamp"])) {
	    $params["Timestamp"] = gmdate('Y-m-d\TH:i:s\Z');
	}
	
	// Sort the parameters by key
	ksort($params);
	
	$pairs = array();
	
	foreach ($params as $key => $value) {
	    array_push($pairs, rawurlencode($key)."=".rawurlencode($value));
	}
	
	// Generate the canonical query
	$canonical_query_string = join("&", $pairs);
	
	// Generate the string to be signed
	$string_to_sign = "GET\n".$endpoint."\n".$uri."\n".$canonical_query_string;
	
	// Generate the signature required by the Product Advertising API
	$signature = base64_encode(hash_hmac("sha256", $string_to_sign, $aws_secret_key, true));
	
	// Generate the signed URL
	$request_url = 'http://'.$endpoint.$uri.'?'.$canonical_query_string.'&Signature='.rawurlencode($signature);
	
	$data =  file_get_contents($request_url);
	return $data;
}

Leave a Reply

Your email address will not be published. Required fields are marked *