Skip to content
Advertisement

How to Fetch Amazon Product Data By Php Without Api

I want to fetch data from the amazon search result and product details. I Found some source to get data by api . But i am searching for get product information without api . As example : https://www.amazon.com/Apple-MWP22AM-A-AirPods-Pro/dp/B07ZPC9QD4 this is an amazon product links. I want fetch this product data. I also try by cUrl/file get contents but failed. Please someone can help ?

Here Code Example i try, But Its Required Capcha

$curl = curl_init('https://www.amazon.com/gp/product/B00M0QVG3W');
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 10.10; labnol;) ctrlq.org");
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$html = curl_exec($curl);
curl_close($curl);

echo $html;

Advertisement

Answer

curl_setopt($curl, CURLOPT_USERAGENT, “Mozilla/5.0 (Windows; U; Windows NT 10.10; labnol;) ctrlq.org”);

why are you lying to them? you’re not a web browser, and Amazon doesn’t seem to block people who aren’t lying about it..

this:

<?php
declare(strict_types = 1);
$ch = curl_init();
curl_setopt_array($ch, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_ENCODING => '', // Warning: if we don't say "Accept-Encoding: gzip", the SOB's at Amazon will send it gzip-compressed anyway.
    CURLOPT_URL => 'https://www.amazon.com/Apple-MWP22AM-A-AirPods-Pro/dp/B07ZPC9QD4'
));
$html = curl_exec($ch);
@($domd = new DOMDocument())->loadHTML($html);
$xp=new DOMXPath($domd);
$product=[];
$product["productName"]=trim($domd->getElementById("productTitle")->textContent);
$product["stock"]=trim($domd->getElementById("availability")->textContent);
$prodInfo=$xp->query("//*[@id='productOverview_feature_div']//tr[contains(@class,'a-spacing-small')]");
foreach($prodInfo as $info){
    $product[trim($info->getElementsByTagName("td")->item(0)->textContent)]=trim($info->getElementsByTagName("td")->item(1)->textContent);
}
var_export($product);

prints:

array (
  'productName' => 'Apple AirPods Pro',
  'stock' => 'Temporarily out of stock.
(bunch of newlines)
We are working hard to be back in stock as soon as possible.',
  'Brand' => 'Apple',
  'Connections' => 'Wireless',
  'Model Name' => 'Apple AirPods Pro',
  'Color' => 'White',
  'Headphones Form Factor' => 'In Ear',
)
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement