Skip to content

Instantly share code, notes, and snippets.

@iampaul83
Created June 13, 2018 14:26
Show Gist options
  • Save iampaul83/f0339c95704c6c053635f5e4f4a80bdf to your computer and use it in GitHub Desktop.
Save iampaul83/f0339c95704c6c053635f5e4f4a80bdf to your computer and use it in GitHub Desktop.
amazon-images.php
<?php
include_once('simplehtmldom_1_5/simple_html_dom.php');
// $curl = curl_init('https://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Dautomotive&field-keywords=76250ST7A24ZC');
$curl = curl_init('https://www.amazon.com/Mirrors-Smooth-Replacement-76250ST7A24ZC-76200ST7A24ZC/dp/B00OVT6UKU/ref=sr_1_1?s=automotive&ie=UTF8&qid=1528855755&sr=1-1&keywords=76250ST7A24ZC');
// $curl = curl_init('https://www.amazon.com/Wireless-Charger-Veidoo-Gravity-Standard/dp/B07CJPRNN9/ref=sr_1_2_sspa?s=wireless&ie=UTF8&qid=1528899106&sr=1-2-spons&keywords=s9+plus&psc=1');
curl_setopt($curl, CURLOPT_VERBOSE, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'accept-language: en',
'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36'
));
// 將curl_exec()獲取的訊息以文件流的形式返回,而不是直接輸出。
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$html = curl_exec($curl);
curl_close($curl);
// https://github.com/galatanovidiu/amazon-images/blob/master/src/GetProductImageAmazon.php
$var = 'data';
$data = substr($html, strpos($html, 'ImageBlockATF'));
$len = strlen($var) + 2;
$start = strpos($data, $var." = {") + $len;
$stop = strpos($data, "};", $start);
$val = substr($data, $start, ($stop - $start));
$val = trim($val);
$vars = preg_replace("/'([^']*)'/i", '"$1"', $val).'}';
$vars_array = json_decode($vars, true);
if (is_array($vars_array)) {
foreach ($vars_array['colorImages']['initial'] as $k => $response) {
if (isset($response['hiRes']) and $response['hiRes']) {
$images[] = $response['hiRes'];
}
}
}
// $dom = new simple_html_dom();
// $dom->load($html);
// echo $dom;
?>
<?php foreach ($images as $image) { ?>
<div>
<p><?php echo $image; ?></p>
<img src="<?php echo $image; ?>" style="max-width: 500px;"></img>
</div>
<?php } ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment