xcapatemx / amazonapi
PHP 库,用于使用亚马逊产品API进行产品查找和搜索。
Requires (Dev)
- phpunit/phpunit: 6.1.*
This package is not auto-updated.
Last update: 2024-09-22 03:22:29 UTC
README
PHP 库,用于使用亚马逊产品API进行产品查找和搜索。
安装
此库需要安装 SimpleXML 和 Curl 扩展,并使用 PHP 7+。安装简单,使用 Composer 进行。
composer require marcl/amazonproductapi
亚马逊产品API
它还假设您对亚马逊的产品API有基本的了解,并且已设置亚马逊联盟账户,请参阅: 亚马逊产品API设置。
您需要AWS密钥、秘密密钥和联盟标记。确保您将这些安全地保存!
示例
我在 examples.php
中添加了一些简单的示例。要运行它们,创建一个名为 secretKeys.php
的文件,其中包含您的密钥
<?php $keyId = 'YOUR-AWS-KEY'; $secretKey = 'YOUR-AWS-SECRET-KEY'; $associateId = 'YOUR-AMAZON-ASSOCIATE-ID'; ?>
然后使用以下命令运行示例
php examples.php
快速入门
使用Composer自动加载器将库包含在您的代码中,并使用您的凭据创建一个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区域设置,您可以在构建AmazonUrlBuilder类时设置它,使用您的密钥。
目前,这些是当前支持的区域设置
- 巴西 ('br')
- 加拿大 ('ca')
- 中国 ('cn')
- 法国 ('fr')
- 德国 ('de')
- 印度 ('in')
- 意大利 ('it')
- 日本 ('jp')
- 墨西哥 ('mx')
- 西班牙 ('es')
- 英国 ('uk')
- 美国 ('us')
项目搜索
要搜索项目,请使用 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
(按价格从高到低排序)。有关更多详细信息,请参阅 ItemSearch Sort Values。
// 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);
数据转换
默认情况下,数据将作为SimpleXML节点返回。但是,您可以根据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"
…
以下定义了不同的数据转换类型。如果您想将数据转换为新的类型,请随时提出问题。
- xml - (默认)以SimpleXML节点返回数据。
- array - 以PHP数组返回数据。
- simple - 以简化的数组返回数据,不包含所有API数据。如果您只需要价格、标题和图片,请使用此选项。
- json - 以JSON字符串返回数据。如果您需要从服务器API端点返回数据,请使用此选项。
待办事项
- 需要使简化数据更少地硬编码!
谢谢
这个库使用了基于AWS API认证 For PHP的代码,作者是David Drake,但大部分已经重写。
许可证
请参阅许可证