亚马逊的产品API限制我们每页只能获得10个商品,而在某个查询中只能获得10个商品页面。获取亚马逊产品API上的所有商品
我开发了一个几乎可以获得所有项目的代码; 第一,我提供了一个PARAMS,看起来像这样:
$item_params = [
"Service" => "AWSECommerceService",
"Operation" => "ItemSearch",
"AWSAccessKeyId" => env('AWS_ACCESS_KEY_ID'),
"AssociateTag" => env('AWS_ASSOCIATE_TAG_ID'),
"SearchIndex" => "HomeGarden",
"ResponseGroup" => "ItemAttributes,SalesRank,Offers",
"Sort" => "-price",
"BrowseNode" => $item_params['BrowseNode'],
"MaximumPrice" => $max_price,
"MinimumPrice" => "0"
];
然后,该代码将获得浏览节点下的所有项目(类别),排序方式PRICE(DESC)还通过指定MAX和MIN价格的项目来限制搜索。
伪码(原代码太长)
function getProducts($item_params, $max_price = null){
$products = //request to amazon
foreach ($product as $key=>$value){
//add product to db
}
// if the total number of results on the query is not equal to zero, continue looping
if (!$products->totalResults() == 0){
$product = //get the first lowest priced item on the db
$this->getProducts($item_params, $product->price);
}
}
但是我遇到这样的情景:
样品请求输出(假设从亚马逊的所有项目):
ASIN(unique id) | Price
1 | 201
2 | 194
3 | 195
.
.
n | 33
n+1 | 33
n+2 | 33
.
n+120 | 33
n+121 | 34
n+122 | 35
其中从n到n + 120的产品是相等的。这将创建一个无限循环到我的getProducts
函数。我怎样才能避免这种情况?知道每个请求只返回10个项目,只有10个页面。