Skip to content

Instantly share code, notes, and snippets.

@webhasan
Created April 18, 2016 03:58
Show Gist options
  • Save webhasan/324ab954184e97f8d7bc1d5366ecf7a3 to your computer and use it in GitHub Desktop.
Save webhasan/324ab954184e97f8d7bc1d5366ecf7a3 to your computer and use it in GitHub Desktop.
Amazon Product Rating by ASIN
<?php
/**
* Amazon Rating Stars
*/
function amazon_stars($asin) {
$content = file_get_contents('http://www.amazon.com/gp/customer-reviews/widgets/average-customer-review/popover/ref=dpx_acr_pop_?contextId=dpx&asin='.trim($asin));
$result = preg_match('/([1,2,3,4,5]\.\d+) out of [1,2,3,4,5] stars/', $content, $matches);
$rating = $matches[1];
$rating = round($rating * 2) / 2;
$rating_img;
if( $rating == '5') {
$rating_img = IMAGES.'/stars-5.png';
}elseif($rating == '4.5') {
$rating_img = IMAGES.'/stars-4.5.png';
}elseif($rating == '4') {
$rating_img = IMAGES.'/stars-4.png';
}elseif($rating == '3.5') {
$rating_img = IMAGES.'/stars-3.5.png';
}elseif($rating == '3') {
$rating_img = IMAGES.'/stars-3.png';
}elseif($rating == '2.5') {
$rating_img = IMAGES.'/stars-2.5.png';
}elseif($rating == '2') {
$rating_img = IMAGES.'/stars-2.png';
}
if($result) {
return $rating_img;
}
}
/**
* Amazon Number Of Review
*/
function amazon_review_count($asin) {
$content = file_get_contents('http://www.amazon.com/gp/customer-reviews/widgets/average-customer-review/popover/ref=dpx_acr_pop_?contextId=dpx&asin='.trim($asin));
$result = preg_match('/See all ([0,1,2,3,4,5,6,7,8,9,\,]+) reviews/',$content, $matches);
if($result) {
return $reviews = $matches[1];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment