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身份验证的代码,但大部分已重写。
请参阅许可证