亞馬遜的產品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個頁面。