Created
April 18, 2016 03:58
-
-
Save webhasan/324ab954184e97f8d7bc1d5366ecf7a3 to your computer and use it in GitHub Desktop.
Amazon Product Rating by ASIN
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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