marcl / amazonproductapi
PHP 库,用于使用 Amazon Product API 执行产品查找和搜索。
Requires (Dev)
- phpunit/phpunit: 6.1.*
This package is not auto-updated.
Last update: 2024-09-28 02:42:58 UTC
README
PHP 库,用于使用 Amazon Product API 执行产品查找和搜索。
安装
此库需要安装 SimpleXML 和 Curl 扩展,并使用 PHP 7+。使用 Composer 安装非常简单。
composer require marcl/amazonproductapi
Amazon Product API
它还假设您对 Amazon 的产品 API 有一些基本了解,并且已设置了一个 Amazon Associate 帐户,请参阅:Amazon Product 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');
注意:请妥善保管您的 Amazon 密钥。您可以使用环境变量或从您不提交到 GitHub 的文件中包含。
区域设置
此库支持所有 Product Advertising 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 排序值。
// 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 数组和对象形式返回数据。
- 简单 - 返回简化的数组数据,不包含所有API数据。如果您只需要价格、标题和图片,请使用此选项。
- JSON - 返回JSON格式的字符串数据。用于从服务器API端点返回数据。
待办
- 需要使简化的数据更少地使用硬编码!
谢谢
此库基于David Drake的AWS API认证For PHP代码,但已大部分重写。
许可证
查看许可证