ビジネス > アフィリエイト > Amazonアソシエイト > Amazon API
Amazonアソシエイトの機能を外部から利用するためのAPIのことです。
正式には「Amazon Product Advertising API」と言います。
これらのデータを取得するにはどうしたら良いのだろうか?
(参考)
Examples
The following is the sample json payload for GetItems request.{ "ItemIds": ["B0199980K4","B000HZD168","B01180YUXS","B00BKQTA4A"], "ItemIdType": "ASIN", "LanguagesOfPreference": ["en_US"], "Marketplace": "www.amazon.com", "PartnerTag": "xyz-20", "PartnerType": "Associates", "Resources": ["Images.Primary.Small","ItemInfo.Title","ItemInfo.Features","Offers.Summaries.HighestPrice","ParentASIN"] }
この例では、欲しいデータの内容をJSON形式で指定する方法になっている。
PHPのSDKのサンプルコードでは、PHPの配列の形式で指定する方法になっている。
JSONではなく、PHPの配列で指定するにはどうすればいいのか?さらに追跡調査。
function getItems()の中で、
# Request initialization # Choose item id(s) $itemIds = ["059035342X", "B00X4WHP55", "1401263119"]; /* * Choose resources you want from GetItemsResource enum * For more details, refer: https://webservices.amazon.com/paapi5/documentation/get-items.html#resources-parameter */ $resources = [ GetItemsResource::ITEM_INFOTITLE, GetItemsResource::OFFERSLISTINGSPRICE]; # Forming the request $getItemsRequest = new GetItemsRequest(); $getItemsRequest->setItemIds($itemIds); $getItemsRequest->setPartnerTag($partnerTag); $getItemsRequest->setPartnerType(PartnerType::ASSOCIATES); $getItemsRequest->setResources($resources);
という部分がある。
この$resourcesを編集すればOKだ。
「ITEM_INFOTITLE」などの定数が定義されている箇所にジャンプして見てみる。
SDKの中にある「GetItemsResource.php」を見る。
(src\com\amazon\paapi5\v1\GetItemsResource.php)
この中で定数が定義されていた。
/** * Possible values of this enum */ const BROWSE_NODE_INFOBROWSE_NODES = 'BrowseNodeInfo.BrowseNodes'; const BROWSE_NODE_INFOBROWSE_NODESANCESTOR = 'BrowseNodeInfo.BrowseNodes.Ancestor'; const BROWSE_NODE_INFOBROWSE_NODESSALES_RANK = 'BrowseNodeInfo.BrowseNodes.SalesRank'; const BROWSE_NODE_INFOWEBSITE_SALES_RANK = 'BrowseNodeInfo.WebsiteSalesRank'; const CUSTOMER_REVIEWSCOUNT = 'CustomerReviews.Count'; const CUSTOMER_REVIEWSSTAR_RATING = 'CustomerReviews.StarRating'; const IMAGESPRIMARYSMALL = 'Images.Primary.Small'; const IMAGESPRIMARYMEDIUM = 'Images.Primary.Medium'; const IMAGESPRIMARYLARGE = 'Images.Primary.Large'; const IMAGESVARIANTSSMALL = 'Images.Variants.Small'; const IMAGESVARIANTSMEDIUM = 'Images.Variants.Medium'; const IMAGESVARIANTSLARGE = 'Images.Variants.Large'; const ITEM_INFOBY_LINE_INFO = 'ItemInfo.ByLineInfo'; const ITEM_INFOCONTENT_INFO = 'ItemInfo.ContentInfo'; const ITEM_INFOCONTENT_RATING = 'ItemInfo.ContentRating'; const ITEM_INFOCLASSIFICATIONS = 'ItemInfo.Classifications'; const ITEM_INFOEXTERNAL_IDS = 'ItemInfo.ExternalIds'; const ITEM_INFOFEATURES = 'ItemInfo.Features'; const ITEM_INFOMANUFACTURE_INFO = 'ItemInfo.ManufactureInfo'; const ITEM_INFOPRODUCT_INFO = 'ItemInfo.ProductInfo'; const ITEM_INFOTECHNICAL_INFO = 'ItemInfo.TechnicalInfo'; const ITEM_INFOTITLE = 'ItemInfo.Title'; const ITEM_INFOTRADE_IN_INFO = 'ItemInfo.TradeInInfo'; const OFFERSLISTINGSAVAILABILITYMAX_ORDER_QUANTITY = 'Offers.Listings.Availability.MaxOrderQuantity'; const OFFERSLISTINGSAVAILABILITYMESSAGE = 'Offers.Listings.Availability.Message'; const OFFERSLISTINGSAVAILABILITYMIN_ORDER_QUANTITY = 'Offers.Listings.Availability.MinOrderQuantity'; const OFFERSLISTINGSAVAILABILITYTYPE = 'Offers.Listings.Availability.Type'; const OFFERSLISTINGSCONDITION = 'Offers.Listings.Condition'; const OFFERSLISTINGSCONDITIONSUB_CONDITION = 'Offers.Listings.Condition.SubCondition'; const OFFERSLISTINGSDELIVERY_INFOIS_AMAZON_FULFILLED = 'Offers.Listings.DeliveryInfo.IsAmazonFulfilled'; const OFFERSLISTINGSDELIVERY_INFOIS_FREE_SHIPPING_ELIGIBLE = 'Offers.Listings.DeliveryInfo.IsFreeShippingEligible'; const OFFERSLISTINGSDELIVERY_INFOIS_PRIME_ELIGIBLE = 'Offers.Listings.DeliveryInfo.IsPrimeEligible'; const OFFERSLISTINGSDELIVERY_INFOSHIPPING_CHARGES = 'Offers.Listings.DeliveryInfo.ShippingCharges'; const OFFERSLISTINGSIS_BUY_BOX_WINNER = 'Offers.Listings.IsBuyBoxWinner'; const OFFERSLISTINGSLOYALTY_POINTSPOINTS = 'Offers.Listings.LoyaltyPoints.Points'; const OFFERSLISTINGSMERCHANT_INFO = 'Offers.Listings.MerchantInfo'; const OFFERSLISTINGSPRICE = 'Offers.Listings.Price'; const OFFERSLISTINGSPROGRAM_ELIGIBILITYIS_PRIME_EXCLUSIVE = 'Offers.Listings.ProgramEligibility.IsPrimeExclusive'; const OFFERSLISTINGSPROGRAM_ELIGIBILITYIS_PRIME_PANTRY = 'Offers.Listings.ProgramEligibility.IsPrimePantry'; const OFFERSLISTINGSPROMOTIONS = 'Offers.Listings.Promotions'; const OFFERSLISTINGSSAVING_BASIS = 'Offers.Listings.SavingBasis'; const OFFERSSUMMARIESHIGHEST_PRICE = 'Offers.Summaries.HighestPrice'; const OFFERSSUMMARIESLOWEST_PRICE = 'Offers.Summaries.LowestPrice'; const OFFERSSUMMARIESOFFER_COUNT = 'Offers.Summaries.OfferCount'; const PARENT_ASIN = 'ParentASIN'; const RENTAL_OFFERSLISTINGSAVAILABILITYMAX_ORDER_QUANTITY = 'RentalOffers.Listings.Availability.MaxOrderQuantity'; const RENTAL_OFFERSLISTINGSAVAILABILITYMESSAGE = 'RentalOffers.Listings.Availability.Message'; const RENTAL_OFFERSLISTINGSAVAILABILITYMIN_ORDER_QUANTITY = 'RentalOffers.Listings.Availability.MinOrderQuantity'; const RENTAL_OFFERSLISTINGSAVAILABILITYTYPE = 'RentalOffers.Listings.Availability.Type'; const RENTAL_OFFERSLISTINGSBASE_PRICE = 'RentalOffers.Listings.BasePrice'; const RENTAL_OFFERSLISTINGSCONDITION = 'RentalOffers.Listings.Condition'; const RENTAL_OFFERSLISTINGSCONDITIONSUB_CONDITION = 'RentalOffers.Listings.Condition.SubCondition'; const RENTAL_OFFERSLISTINGSDELIVERY_INFOIS_AMAZON_FULFILLED = 'RentalOffers.Listings.DeliveryInfo.IsAmazonFulfilled'; const RENTAL_OFFERSLISTINGSDELIVERY_INFOIS_FREE_SHIPPING_ELIGIBLE = 'RentalOffers.Listings.DeliveryInfo.IsFreeShippingEligible'; const RENTAL_OFFERSLISTINGSDELIVERY_INFOIS_PRIME_ELIGIBLE = 'RentalOffers.Listings.DeliveryInfo.IsPrimeEligible'; const RENTAL_OFFERSLISTINGSDELIVERY_INFOSHIPPING_CHARGES = 'RentalOffers.Listings.DeliveryInfo.ShippingCharges'; const RENTAL_OFFERSLISTINGSMERCHANT_INFO = 'RentalOffers.Listings.MerchantInfo';
この定義の右辺の部分(BrowseNodeInfo.BrowseNodesなど)が、ちょうどAPIリファレンスの「Resources Parameter」と一致しているようだ。
(参考)GetItems · Product Advertising API 5.0 https://webservices.amazon.com/paapi5/documentation/get-items.html#resources-parameter
以上から、
欲しいデータ | 対応するResources Parameter | SDKのPHPサンプルコード中の定数 |
商品名 | ItemInfo.Title | ITEM_INFOTITLE |
商品画像(小)"Height": 75 | Images.Primary.Small | IMAGESPRIMARYSMALL |
商品画像(中)"Height": 160 | Images.Primary.Medium | IMAGESPRIMARYMEDIUM |
商品画像(大)"Height": 500 | Images.Primary.Large | IMAGESPRIMARYLARGE |
本の著者名 | ItemInfo.ByLineInfo | ITEM_INFOBY_LINE_INFO |
本の出版社名 | ItemInfo.ByLineInfo | ITEM_INFOBY_LINE_INFO |
発売日 | ItemInfo.ContentInfo | ITEM_INFOCONTENT_INFO |
価格 | Offers.Listings.Price | OFFERSLISTINGSPRICE |
という項目をセットしてリスエストすれば、欲しいデータが得られます。
PHPの配列で指定すると、以下のようになりました。
$resources = [ GetItemsResource::ITEM_INFOTITLE, GetItemsResource::IMAGESPRIMARYSMALL , GetItemsResource::IMAGESPRIMARYMEDIUM , GetItemsResource::IMAGESPRIMARYLARGE , GetItemsResource::ITEM_INFOBY_LINE_INFO , GetItemsResource::ITEM_INFOCONTENT_INFO , GetItemsResource::OFFERSLISTINGSPRICE , ];
これで、試しにASIN「4863542224」の本のデータを取得してみます。
返ってきたレスポンスのJSONは、以下のようになっていました。
#code(json){{{
API called successfully Complete Response: {
"ItemsResult": { "Items": [ { "ASIN": "4863542224", "DetailPageURL": "https:\/\/www.amazon.co.jp\/dp\/4863542224?tag=vertex9-22&linkCode=ogi&th=1&psc=1", "Images": { "Primary": { "Small": { "URL": "https:\/\/m.media-amazon.com\/images\/I\/51G32HC7rSL._SL75_.jpg", "Height": 75, "Width": 53 }, "Medium": { "URL": "https:\/\/m.media-amazon.com\/images\/I\/51G32HC7rSL._SL160_.jpg", "Height": 160, "Width": 113 }, "Large": { "URL": "https:\/\/m.media-amazon.com\/images\/I\/51G32HC7rSL.jpg", "Height": 500, "Width": 352 } } }, "ItemInfo": { "ByLineInfo": { "Contributors": [ { "Locale": "ja_JP", "Name": "\u9ce5\u5c45 \u967d\u4ecb", "Role": "\u8457", "RoleType": "author" } ], "Manufacturer": { "DisplayValue": "\u30b7\u30fc\u30a2\u30f3\u30c9\u30a2\u30fc\u30eb\u7814\u7a76\u6240", "Label": "Manufacturer", "Locale": "ja_JP" } }, "ContentInfo": { "Languages": { "DisplayValues": [ { "DisplayValue": "\u65e5\u672c\u8a9e", "Type": "\u767a\u884c\u6e08\u307f" } ], "Label": "Language", "Locale": "ja_JP" }, "PagesCount": { "DisplayValue": 224, "Label": "NumberOfPages", "Locale": "en_US" }, "PublicationDate": { "DisplayValue": "2019-02-27T00:00:01Z", "Label": "PublicationDate", "Locale": "en_US" } }, "Title": { "DisplayValue": "\u57fa\u790e\u304b\u3089\u308f\u304b\u308b Elm", "Label": "Title", "Locale": "ja_JP" } }, "Offers": { "Listings": [ { "Id": "%259DZs7G3(中略)%F0I8f6Xmq", "Price": { "Amount": 2893, "Currency": "JPY", "DisplayAmount": "\uffe52,893" }, "ViolatesMAP": false } ] } } ] }
} Printing all item information in ItemsResult: Printing information about the itemId: 4863542224 ASIN: 4863542224 Title: 基礎からわかる Elm Detail Page URL: https: //www.amazon.co.jp/dp/4863542224?tag=vertex9-22&linkCode=ogi&th=1&psc=1 Buying price: ¥2,893
}}}
次の工程では、JSONをパースして必要な項目のデータを取り出せばOKですね!
#html{{
!--
td> </td>
hr>
- >
}}