AmazonProductAPI
PHP库使用Amazon Product API执行产品查找和搜索。
安装
该库需要安装单纯式和卷曲扩展名并使用PHP 7+。使用作曲家安装很简单:
composer require marcl/ AmazonProductAPI
亚马逊产品API
它还假设您对亚马逊的产品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类时将其设置。
目前,这些是当前受支持的地区:
- 巴西(\’br\’)
- 加拿大(“ CA”)
- 中国(\’CN\’)
- 法国(\’fr\’)
- 德国(\’de\’)
- 印度(“在”)
- 意大利(\’it\’)
- 日本(\’JP\’)
- 墨西哥(\’MX\’)
- 西班牙(\’es\’)
- 英国(“英国”)
- 美国(“我们”)
项目搜索
要搜索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.*a*m*azon.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.im*ages*-a*mazon.com/images/I/51qa9nTUsEL.jpg\"
\'mediumImage\' =>
string(61) \"http://ecx.imag**es-ama*zon.com/images/I/51qa9nTUsEL._SL160_.jpg\"
\'smallImage\' =>
string(60) \"http://ecx.*image*s-am*azon.com/images/I/51qa9nTUsEL._SL75_.jpg\"
}
[1] =>
array(8) {
\'asin\' =>
string(10) \"0747558191\"
\'url\' =>
string(212) \"http://www.a**mazon*.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- (默认值)将数据返回为单纯式节点。
- 数组– 将数据返回为PHP数组和对象。
- 简单– 返回数据作为简化数组,不包含所有API数据。如果您只需要价格,标题和图像,请使用此功能。
- JSON-将数据返回为JSON字符串。使用此功能从服务器API端点返回。
托多
- 需要使简化的数据少进行硬编码!
谢谢
该库使用David Drake的AWS API身份验证基于AWS API身份验证的代码,但大部分已重写。
执照
请参阅许可证
