Jump to content


Photo

Get Amazon Price of a product


  • Please log in to reply
8 replies to this topic

#1 Daniel.de

Daniel.de

    Advanced Member

  • Members
  • PipPipPip
  • 186 posts
  • LocationGermany

Posted 04 April 2017 - 01:32 PM

Hi there,

 

I use Amazon Affiliate Links on some listings. At the moment I copy just the Amazon Partner Link in a field (website). Now I was thinking if it is possible to show the current price of the product.

 

So I found this php code:

<?php

// Region code and Product ASIN
$response = getAmazonPrice("com", "B00KQPGRRE");

function getAmazonPrice($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);
	$image = htmlentities((string) $item->MediumImage->URL);
	$price = htmlentities((string) $item->OfferSummary->LowestNewPrice->Amount);
	$code = htmlentities((string) $item->OfferSummary->LowestNewPrice->CurrencyCode);
	$qty = htmlentities((string) $item->OfferSummary->TotalNew);

	if ($qty !== "0") {
		$response = array(
			"code" => $code,
			"price" => number_format((float) ($price / 100), 2, '.', ''),
			"image" => $image,
			"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 = "PUBLIC_KEY";
	$private_key = "PRIVATE_KEY";

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

	$params["Service"] = "AWSECommerceService";
	$params["AssociateTag"] = "affiliate-20"; // Put your Affiliate Code here
	$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;
	}
}

?>

Is it possible to combine this with a JReviews text field (or code) where I can put in the Amazon ASIN Number and this will give me the current price back?

 

Maybe some of you did some things like this.

 

Thank you

 

 


  • n00bster likes this

#2 Alejandro

Alejandro

    Advanced Member

  • Administrators
  • 32165 posts

Posted 04 April 2017 - 02:09 PM

You can put the functions in a PHP file on the root of your site, let's call it amazon.php for the purpose of this example. Then you can create a banner custom field and add this code:

require_once PATH_ROOT."amazon.php";
$asin = $CustomFields->fieldValue('jr_asin',$entry);
// Region code and Product ASIN
$response = getAmazonPrice("com", $asin);
if ($response) {
  return $response['price'];
}
return false;

Of course you probably need to replace these variables with real values:

    $public_key = "PUBLIC_KEY";
    $private_key = "PRIVATE_KEY";

And I wouldn't even attempt to test this inside JReviews, before you test it by itself, by creating that amazon.php file with the code you posted.


  • Daniel.de likes this

#3 Daniel.de

Daniel.de

    Advanced Member

  • Members
  • PipPipPip
  • 186 posts
  • LocationGermany

Posted 04 April 2017 - 04:17 PM

That's amazing. It's working exactly like you said :) I just had to enter my AWS credits, my Amazon Partner ID and the country code "de" for Germany. 

 

Great, thank you so much. And I learned again how to work with php.



#4 Alejandro

Alejandro

    Advanced Member

  • Administrators
  • 32165 posts

Posted 04 April 2017 - 04:21 PM

Cool! I am curious, have you had any success as an Amazon affiliate?



#5 Daniel.de

Daniel.de

    Advanced Member

  • Members
  • PipPipPip
  • 186 posts
  • LocationGermany

Posted 04 April 2017 - 04:38 PM

Yes, it is one of the biggest source of income on my page. 10 wonderfull women are writing book reviews unsalaried for me. As compensation they get the books for free from the publishing companies. And I will earn money by publishing the Amazon Link. So, everybody is happy :)

 

And everything is so easy to manage because of JReviews :)


  • n00bster likes this

#6 Alejandro

Alejandro

    Advanced Member

  • Administrators
  • 32165 posts

Posted 04 April 2017 - 04:47 PM

That sounds great! A win-win all around  :)



#7 Daniel.de

Daniel.de

    Advanced Member

  • Members
  • PipPipPip
  • 186 posts
  • LocationGermany

Posted 05 April 2017 - 12:57 PM

Now, I have one more question. I could change the php code to get also the currency back:

if ($response) {
  return array ($response['price'], $response['code']);
}

But now I get the information in ul-class back:

<ul class="jrFieldValueList"><li>9.99</li><li>EUR</li></ul>

What is to do to have the price and the currency in one line or in one li tag?



#8 Alejandro

Alejandro

    Advanced Member

  • Administrators
  • 32165 posts

Posted 05 April 2017 - 01:29 PM

Use:

return $response['price'] . $response['code'];

  • Daniel.de likes this

#9 Daniel.de

Daniel.de

    Advanced Member

  • Members
  • PipPipPip
  • 186 posts
  • LocationGermany

Posted 06 April 2017 - 12:08 PM

Thank you Alejandro.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users