PHP庫使用Amazon Product API執行產品查找和搜索。
該庫需要安裝單純式和捲曲擴展名並使用PHP 7+。使用作曲家安裝很簡單:
composer require marcl/amazonproductapi它還假設您對亞馬遜產品API有一些基本知識,並且已經設置了一個亞馬遜關聯帳戶,請參見:Amazon Product API設置。
您需要一個AWS密鑰,秘密密鑰和關聯標籤。確保您確保這些安全!
我在examples.php中添加了一些簡單的示例。 php。運行它們創建一個名為secretKeys.php的文件,其中包含您的秘密密鑰:
<?php
$ keyId = ' YOUR-AWS-KEY ' ;
$ secretKey = ' YOUR-AWS-SECRET-KEY ' ;
$ associateId = ' YOUR-AMAZON-ASSOCIATE-ID ' ;
?>然後用以下示例運行以下示例
php examples.php使用Composer AutoLoader在您的代碼中包含庫,並使用您的憑據創建AmazonurlBuilder
require ( ' vendor/autoload.php ' );
use MarcL AmazonAPI ;
use MarcL AmazonUrlBuilder ;
// Keep these safe
$ keyId = ' YOUR-AWS-KEY ' ;
$ secretKey = ' YOUR-AWS-SECRET-KEY ' ;
$ associateId = ' YOUR-AMAZON-ASSOCIATE-ID ' ;
// Setup a new instance of the AmazonUrlBuilder with your keys
$ urlBuilder = new AmazonUrlBuilder (
$ keyId ,
$ secretKey ,
$ associateId ,
' uk '
);
// Setup a new instance of the AmazonAPI and define the type of response
$ amazonAPI = new AmazonAPI ( $ urlBuilder , ' simple ' );
$ items = $ amazonAPI -> ItemSearch ( ' harry potter ' , ' Books ' , ' price ' );注意:保持亞馬遜鑰匙安全。要么使用環境變量,要么從您不入圍GitHub的文件中包括。
該庫支持所有產品廣告API Locales,您可以在使用鑰匙構造AmazonurlBuilder類時將其設置。
目前,這些是當前受支持的地區:
要搜索ItemSearch()
// Search for harry potter items in all categories
$ items = $ amazonAPI -> ItemSearch ( ' harry potter ' );
// Search for harry potter items in Books category only
$ items = $ amazonAPI -> ItemSearch ( ' harry potter ' , ' Books ' );默認情況下, ItemSearch()方法將通過特色搜索。如果要按另一個類別進行排序,請傳遞第三個參數,並以您希望排序的類別的名稱。這些因素類型有所不同,但您可能需要的兩個是price (按價格低到高)或-price (按價格高至低)。有關更多詳細信息,請參見項目搜索排序值。
// Search for harry potter items in Books category, sort by low to high
$ items = $ amazonAPI -> ItemSearch ( ' harry potter ' , ' Books ' , ' price ' );
// Search for harry potter items in Books category, sort by high to low
$ items = $ amazonAPI -> ItemSearch ( ' harry potter ' , ' Books ' , ' -price ' );確定搜索呼叫的有效類別GetValidSearchNames() :
// Get an array of valid search categories we can use
$ searchCategories = $ amazonAPI -> GetValidSearchNames ();要使用產品ASIN數字查找產品,請使用ItemLookup() :
// Retrieve specific item by id
$ items = $ amazonAPI -> ItemLookUp ( ' B003U6I396 ' );
// Retrieve a list of items by ids
$ asinIds = array ( ' B003U6I396 ' , ' B003U6I397 ' , ' B003U6I398 ' );
$ items = $ amazonAPI -> ItemLookUp ( $ asinIds );默認情況下,數據將返回為單純詞節點。但是,您可以根據API的用例要求對數據進行不同的轉換。在實例化Amazonapi類時通過類型,如下所示:
// Default return type is XML
$ amazonAPI = new AmazonAPI ( $ amazonUrlBuilder );
$ items = $ amazonAPI -> ItemSearch ( ' harry potter ' );
var_dump ( $ items );這將輸出:
class SimpleXMLElement#2 (2) {
public $OperationRequest = >
class SimpleXMLElement#3 (3) {
public $RequestId = >
string(36) " de58449e-0c1a-47ac-9823-00fd049c52df "
public $Arguments = >
class SimpleXMLElement#5 (1) {
public $Argument = >
array(11) {
... // Return simplified data
$ amazonAPI = new AmazonAPI ( $ amazonUrlBuilder , ' simple ' );
$ items = $ amazonAPI -> ItemSearch ( ' harry potter ' );
var_dump ( $ items );這將返回每個項目的簡化版本,其中具有最小的數據,但對於簡單的用例來說,這足以返回。
array(10) {
[0] =>
array(8) {
'asin' =>
string(10) "B00543R3WG"
'url' =>
string(212) "http://www.amazon.co.uk/Harry-Potter-Complete-8-Film-Collection/dp/B00543R3WG%3FSubscriptionId%3D1BM0B8TXM1YSZ1M0XDR2%26tag%3Ddjcr-21%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3DB00543R3WG"
'rrp' =>
double(44.99)
'title' =>
string(58) "Harry Potter - The Complete 8-Film Collection [DVD] [2011]"
'lowestPrice' =>
double(23.4)
'largeImage' =>
string(53) "http://ecx.images-amazon.com/images/I/51qa9nTUsEL.jpg"
'mediumImage' =>
string(61) "http://ecx.images-amazon.com/images/I/51qa9nTUsEL._SL160_.jpg"
'smallImage' =>
string(60) "http://ecx.images-amazon.com/images/I/51qa9nTUsEL._SL75_.jpg"
}
[1] =>
array(8) {
'asin' =>
string(10) "0747558191"
'url' =>
string(212) "http://www.amazon.co.uk/Harry-Potter-Philosophers-Stone-Rowling/dp/0747558191%3FSubscriptionId%3D1BM0B8TXM1YSZ1M0XDR2%26tag%3Ddjcr-21%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D0747558191"
'rrp' =>
double(6.99)
'title' =>
string(40) "Harry Potter and the Philosopher's Stone"
…
不同的數據轉換類型定義如下。如果您希望將數據轉換為新類型,請隨時提出問題。
該庫使用David Drake的AWS API身份驗證基於AWS API身份驗證的代碼,但大部分已重寫。
請參閱許可證