Amazon製品APIを使用して製品の検索と検索を実行するPHPライブラリ。
このライブラリでは、SimplexMLおよびCurl拡張機能をインストールし、PHP 7+を使用する必要があります。コンポーザーを使用して、インストールは簡単です。
composer require marcl/amazonproductapiまた、Amazonの製品APIに関する基本的な知識があり、Amazon Associateアカウントを設定していることを想定しています。Amazon製品APIセットアップを参照してください。
AWSキー、シークレットキー、アソシエイトタグが必要です。これらを安全に保つことを確認してください!
examples.phpにいくつかの簡単な例を追加しました。それらを実行するには、 secretKeys.phpというファイルを作成します。
<?php
$ keyId = ' YOUR-AWS-KEY ' ;
$ secretKey = ' YOUR-AWS-SECRET-KEY ' ;
$ associateId = ' YOUR-AMAZON-ASSOCIATE-ID ' ;
?>そして、次の例を実行します。
php examples.phpComposer 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 ' );注: Amazonキーを安全に保ちます。環境変数を使用するか、GitHubにチェックインしないファイルから含めるかどうか。
このライブラリは、すべての製品広告APIロケールをサポートしており、キーを使用して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()メソッドはfoludによって検索されます。別のカテゴリでソートする場合は、並べ替えたいカテゴリの名前を3番目のパラメーターに渡します。これらはカテゴリタイプによって異なりますが、おそらく必要な2つはprice (価格が低くて高くなるかどうか)または-price (価格が高いから低くなる)です。詳細については、itearearchソート値を参照してください。
// 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 ();製品を使用して製品を検索するには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"
…
異なるデータ変換タイプは、次のように定義されています。データを新しいタイプに変換したい場合は、お気軽に問題を提起してください。
このライブラリは、David DrakeによるPHPのAWS API認証に基づいてコードを使用していますが、ほとんど書き直されています。
ライセンスを参照してください