Amazon API 从产品中获取所有图像
Posted
技术标签:
【中文标题】Amazon API 从产品中获取所有图像【英文标题】:Amazon API get all images from product 【发布时间】:2018-06-10 08:07:36 【问题描述】:我有一个简单的问题,因为找不到任何答案。实际上,我可以使用 Amazon 的 API 从 ASIN 代码中获取有关产品的信息,但在这张图片的情况下,我可以得到一张不同格式的图片,大拇指、中拇指等,对吧?
在我的情况下,我想知道是否可以从产品中获取所有图像,不仅显示 1 张不同格式的图片,如果我可以使用抓取来获取所有图像,但在这种情况下,我想使用 API,因为它是更干净,并且使用抓取来自 CURL 的许多查询没有问题
其实我用这个:
$gallery=htmlentities((string) $item->ImageSets->ImageSet->LargeImage->URL);
项目结构:
$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);
$amount=htmlentities((string) $item->OfferSummary->LowestNewPrice->Amount);
$item->OfferSummary->LowestUsedPrice->Amount);
$code = htmlentities((string) $item->OfferSummary->LowestNewPrice->CurrencyCode);
$qty = htmlentities((string) $item->OfferSummary->TotalNew);
$gallery=htmlentities((string) $item->ImageSets->ImageSet->LargeImage->URL);
函数API代码:
function getAmazonPrice($region, $asin)
global $precioamz;
global $amountamz;
global $imageamz;
global $galeriaamz;
$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);
$amount=htmlentities((string) $item->OfferSummary->LowestNewPrice->Amount);
$item->OfferSummary->LowestUsedPrice->Amount);
$code = htmlentities((string) $item->OfferSummary->LowestNewPrice->CurrencyCode);
$qty = htmlentities((string) $item->OfferSummary->TotalNew);
$gallery=htmlentities((string) $item->ImageSets->ImageSet->LargeImage->URL);
/// $gallery=Images,ItemAttributes,Variations,VariationImages
if ($qty !== "0")
$response = array(
"code" => $code,
"price" => number_format((float) ($price / 100), 2, '.', ''),
"image" => $image,
"url" => $url,
"title" => $title,
"amount" => $amount,
"galeria" => $gallery
);
$precioamz=$response['price'];
$amountamz=$response['amount'];
$imageamz=$response['image'];
$galeriaamz=$response['galeria'];
//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;
但只能获得 1 张图片,我想获得该产品的所有图片,谢谢您的帮助,问候
【问题讨论】:
【参考方案1】:获取您必须使用的所有图像,或添加
Images,ItemAttributes,Variations,VariationImages
当您调用 aws_signed_request()
时作为请求键 ResponseGroup
的值
我当然希望得到与您现在得到的不同的响应,因此您的响应解析代码可能需要修改。
检查响应并相应地编辑您的代码。
【讨论】:
【参考方案2】:如果我没有误解您的要求,我假设您的 Amazon API XML 响应类似于我从 Amazon API 文档页面 https://docs.aws.amazon.com/AWSECommerceService/latest/DG/EX_RetrievingImages.html 获取的内容。所以你可以尝试这种方式来获取所有的图片网址。
$xmlstr = <<<XML
<Item>
<ASIN>B004HO6I4M</ASIN>
<SmallImage>
<URL>
https://ecx.images-amazon.com/images/I/519SgX2wwDL._SL75_.jpg
</URL>
<Height Units="pixels">75</Height>
<Width Units="pixels">56</Width>
</SmallImage>
<MediumImage>
<URL>
https://ecx.images-amazon.com/images/I/519SgX2wwDL._SL160_.jpg
</URL>
<Height Units="pixels">160</Height>
<Width Units="pixels">120</Width>
</MediumImage>
<LargeImage>
<URL>
https://ecx.images-amazon.com/images/I/519SgX2wwDL._SL500_.jpg
</URL>
<Height Units="pixels">500</Height>
<Width Units="pixels">375</Width>
</LargeImage>
<ImageSets>
<ImageSet Category="primary">
<SwatchImage>
<URL>
https://ecx.images-amazon.com/images/I/519SgX2wwDL._SL30_.jpg
</URL>
<Height Units="pixels">30</Height>
<Width Units="pixels">22</Width>
</SwatchImage>
<SmallImage>
<URL>
https://ecx.images-amazon.com/images/I/519SgX2wwDL._SL75_.jpg
</URL>
<Height Units="pixels">75</Height>
<Width Units="pixels">56</Width>
</SmallImage>
<ThumbnailImage>
<URL>
https://ecx.images-amazon.com/images/I/519SgX2wwDL._SL75_.jpg
</URL>
<Height Units="pixels">75</Height>
<Width Units="pixels">56</Width>
</ThumbnailImage>
<TinyImage>
<URL>
https://ecx.images-amazon.com/images/I/519SgX2wwDL._SL110_.jpg
</URL>
<Height Units="pixels">110</Height>
<Width Units="pixels">82</Width>
</TinyImage>
<MediumImage>
<URL>
https://ecx.images-amazon.com/images/I/519SgX2wwDL._SL160_.jpg
</URL>
<Height Units="pixels">160</Height>
<Width Units="pixels">120</Width>
</MediumImage>
<LargeImage>
<URL>
https://ecx.images-amazon.com/images/I/519SgX2wwDL._SL500_.jpg
</URL>
<Height Units="pixels">500</Height>
<Width Units="pixels">375</Width>
</LargeImage>
</ImageSet>
</ImageSets>
</Item>
XML;
$images = new SimpleXMLElement($xmlstr);
$image_set = $images->ImageSets->ImageSet;
$all_images = [];
foreach($image_set->children() as $key=>$value)
$all_images[$key] =trim((string)$value->URL);
print '<pre>';
print_r($all_images);
print '</pre>';
输出:
Array
(
[SwatchImage] => https://ecx.images-amazon.com/images/I/519SgX2wwDL._SL30_.jpg
[SmallImage] => https://ecx.images-amazon.com/images/I/519SgX2wwDL._SL75_.jpg
[ThumbnailImage] => https://ecx.images-amazon.com/images/I/519SgX2wwDL._SL75_.jpg
[TinyImage] => https://ecx.images-amazon.com/images/I/519SgX2wwDL._SL110_.jpg
[MediumImage] => https://ecx.images-amazon.com/images/I/519SgX2wwDL._SL160_.jpg
[LargeImage] => https://ecx.images-amazon.com/images/I/519SgX2wwDL._SL500_.jpg
)
【讨论】:
不,在我的问题中,我说的是产品中的所有图像,如果你在亚马逊上购买产品,你可以看到不同的图像,从 API 只能得到 1 个不同尺寸或分辨率的图像,你可以放直到在这里,但是产品有图像库,从 API 我想显示产品的所有图像不是同一个图像,实际上我可以在加载网站时使用解析器加载 DOM 产品显示所有图像,并显示这些图像并存储在数据库中,但是有问题'就是这样,来自亚马逊的 API 只显示单个图像,我不知道其他图像是否可能仅使用亚马逊的 API 显示以上是关于Amazon API 从产品中获取所有图像的主要内容,如果未能解决你的问题,请参考以下文章
使用 Powershell 从 Amazon 获取图书元数据