ざっくりと整理したのでサンプルソースをアップ。
Amazon APIのRequestへの電子署名添付(PHP版) | Yama’s Memorandum
http://memorandum.yamasnet.com/archives/Post-320.html
のソースを改変して、画像を表示させるようにしたもの。
サンプルの動作は、http://moonmile.net/php/amazon.php で確認できる。
探したい本のキーワード(例. 増田智明)を入力して、送信ボタンを押すと、画像付きのリストが表示される。先頭の行は、戻されるXMLの確認用で、そのままURLで指定するとamazon apiの返信が見られる。
実際は、複数ページで返されるので、Items/TotalResults, Items/TotalPages をチェックしないといけないだが、今回は省略。
ひとまず、これで物欲webはできそうだ。
<?php
if ($_POST["submit"] == "送信")
{
// Access Key ID と Secret Access Key は必須です
$access_key_id = ''; // ここに Access Key ID を入力';
$secret_access_key = ''; // 'ここに Secret Access Key を入力';
// RFC3986 形式で URL エンコードする関数
function urlencode_rfc3986($str)
{
return str_replace('%7E', '~', rawurlencode($str));
}
// 基本的なリクエストを作成します
// - この部分は今まで通り
$baseurl = 'http://ecs.amazonaws.jp/onca/xml';
$params = array();
$params['Service'] = 'AWSECommerceService';
$params['AWSAccessKeyId'] = $access_key_id;
$params['Version'] = '2009-03-31';
$params['Operation'] = 'ItemSearch'; // ← ItemSearch オペレーションの例
$params['SearchIndex'] = 'Books';
$params['Keywords'] = $_POST['kw']; // ← 文字コードは UTF-8
$params['ResponseGroup'] ='Small,Images';
#$params['IdType'] = 'ASIN';
#$params['ItemId'] = '479801382X';
// Timestamp パラメータを追加します
// - 時間の表記は ISO8601 形式、タイムゾーンは UTC(GMT)
$params['Timestamp'] = gmdate('Y-m-d\TH:i:s\Z');
// パラメータの順序を昇順に並び替えます
ksort($params);
// canonical string を作成します
$canonical_string = '';
foreach ($params as $k => $v) {
$canonical_string .= '&'.urlencode_rfc3986($k).'='.urlencode_rfc3986($v);
}
$canonical_string = substr($canonical_string, 1);
// 署名を作成します
// - 規定の文字列フォーマットを作成
// - HMAC-SHA256 を計算
// - BASE64 エンコード
$parsed_url = parse_url($baseurl);
$string_to_sign = "GET\n{$parsed_url['host']}\n{$parsed_url['path']}\n{$canonical_string}";
$signature = base64_encode(hash_hmac('sha256', $string_to_sign, $secret_access_key, true));
// URL を作成します
// - リクエストの末尾に署名を追加
$url = $baseurl.'?'.$canonical_string.'&Signature='.urlencode_rfc3986($signature);
# echo $url; // ← この URL にアクセスすれば、API リクエストができます
$response = file_get_contents($url);
$root = new SimpleXMLElement($response);
foreach ( $root->children() as $Items ) {
if ( $Items->getName() == 'Items' ) {
break;
}
}
/*
$parsed_xml = simplexml_load_string($response);
$Image_URL = $parsed_xml->Items->Item->MediumImage->URL;
$Title = $parsed_xml->Items->Item->ItemAttributes->Title;
$Author = $parsed_xml->Items->Item->ItemAttributes->Author;
$Creator = $parsed_xml->Items->Item->ItemAttributes->Creator;
$Creator_Role = $parsed_xml->Items->Item->ItemAttributes->Creator['Role'];
$Publ_date = $parsed_xml->Items->Item->ItemAttributes->PublicationDate;
*/
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?= $url ?>
<hr />
<table>
<? foreach ( $Items->children() as $Item ) {
if ( $Item->getName() == 'Item' ) {
?>
<tr>
<td><a href="http://amazon.co.jp/dp/<?= $Item->ASIN ?>"><?= $Item->ASIN ?></a>
<td><img src="<?= $Item->SmallImage->URL ?>"
width="<?= $Item->SmallImage->Width ?>"
height="<?= $Item->SmallImage->Height ?>">
<td><?= $Item->ItemAttributes->Title ?>
</tr>
<? }} ?>
</table>
<?php
}
else
{
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>
</title>
<body>
<form method="post" action="amazon.php">
<table>
<tr>
<td>キーワード:</td>
<td><input type="text" name="kw" /></td>
</tr>
<td></td>
<td><input type="submit" name="submit" value="送信" /></td>
</tr>
</table>
<?php
}
?>

1234567890