Written by Manabu Bannai

Amazon API Library:apai-ioの使い方とサンプルコード

PHP PROGRAMMING

先日にAmazon APIを使って商品検索するサンプルコード【配布・コピペOK】を書いたのですが、apai-ioというライブラリを使ったほうがよっぽど簡単でした。
» Exeu/apai-io: Amazon Product Adverstising Library

※更新情報(2016/11/13)
最終的な結論としては、書籍検索APIならGoogleBooksAPIsがオススメです。
» 書籍検索APIはGoogleBooksAPIsがオススメ【導入も楽ちんです】

Composerでインストール

新規作成ファイル:composer.json

{
    "require": {
        "exeu/apai-io": "~2.0"
    }
}

ターミナルで下記を実行

$ composer install

apai-ioでキーワードを使った商品検索する際のサンプルコード

新規作成ファイル:kw-search.php
composerでインストールが完了したら下記のとおりで利用可能です。

<?php
namespace AcmeDemo;
require_once( 'vendor/autoload.php' );

use ApaiIOConfigurationGenericConfiguration;
use ApaiIOOperationsSearch;
use ApaiIOApaiIO;

$conf = new GenericConfiguration();
$client = new GuzzleHttpClient();
$request = new ApaiIORequestGuzzleRequest($client);

$conf
->setCountry('co.jp')
->setAccessKey("AKIAJUVVBL2L2ZYSA5MQ")
->setSecretKey("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")
->setAssociateTag("test")
->setRequest($request);

$apaiIO = new ApaiIO($conf);
$search = new Search();
$search->setCategory('Books');
$search->setKeywords('東野圭吾');

$formattedResponse = $apaiIO->runOperation($search);
$results = simplexml_load_string( $formattedResponse );

foreach($results->Items->Item as $item) {
	$item_id = $item->ASIN; //ASIN
	$item_title = $item->ItemAttributes->Title; // 商品名
	$item_author = $item->ItemAttributes->Author; // 著者
	$item_publicationdate = $item->ItemAttributes->PublicationDate; // 発売日
	$item_publisher = $item->ItemAttributes->Publisher; // 出版社
	$item_url = $item->DetailPageURL; // 商品のURL
	$item_image	 = $item->LargeImage->URL; // 商品の画像
}

echo $item_id; // ASINを表示
echo $item_title; // 商品名を表示

apai-ioでASINを使った商品検索する際のサンプルコード

新規作成ファイル:asin-search.php

<?php
namespace AcmeDemo;
require_once( 'vendor/autoload.php' );

use ApaiIOConfigurationGenericConfiguration;
use ApaiIOOperationsLookup;
use ApaiIOApaiIO;

$conf = new GenericConfiguration();
$client = new GuzzleHttpClient();
$request = new ApaiIORequestGuzzleRequest($client);

$conf
->setCountry('co.jp')
->setAccessKey("AKIAJUVVBL2L2ZYSA5MQ")
->setSecretKey("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")
->setAssociateTag("test")
->setRequest($request);

$apaiIO = new ApaiIO($conf);
$lookup = new Lookup();
$lookup->setItemId('4167110121');
$lookup->setResponseGroup(array('Large', 'Small'));

$formattedResponse = $apaiIO->runOperation($lookup);
$results = simplexml_load_string( $formattedResponse );

foreach($results->Items->Item as $item) {
	$item_title = $item->ItemAttributes->Title; // 商品名
	$item_author = $item->ItemAttributes->Author; // 著者
	$item_publicationdate = $item->ItemAttributes->PublicationDate; // 発売日
	$item_publisher = $item->ItemAttributes->Publisher; // 出版社
	$item_url = $item->DetailPageURL; // 商品のURL
	$item_image	 = $item->LargeImage->URL; // 商品の画像
}
echo $item_title;
echo $item_author;

これだけです。apai-io最高/(^o^)\

※P.S:無料メルマガで発信中:過去の僕は「ブログ発信で5億円」を稼ぎました。次は「30億円」を目指します。挑戦しつつ、裏側の思考を「メルマガ」から発信します。不満足なら1秒で解約できます。無料登録は「こちら」です。