PEAR Services_Amazon SimilarityLookup 使ってみた

できたアプリはこんな感じ

PEAR 準備

  • C:\xampp\php> pear でエラー発生.
    • intl3_svn.dll がみつからないと言ってくるのでパスを通す.
    • C:\xampp\apache\bin\intl3_svn.dll
  • コマンド一覧

C:\xampp\php> pear

  • インストール・パッケージ一覧

C:\xampp\php> pear list

  • PEARチャネルの情報更新

C:\xampp\php> pear channel-update pear.php.net

PEAR Services_Amazon のインストール

C:\xampp\php> pear install XML_Util
C:\xampp\php> pear install Cache
C:\xampp\php> pear install XML_Parser
C:\xampp\php> pear install XML_Serializer-0.18.0
C:\xampp\php> pear install Services_Amazon-0.7.0

コードの修正

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Amazon Webサービス: SimilarityLookup</title>
<link rel="stylesheet" type="text/css" href="css/test.css">
<body>
<h1>Amazon Webサービス: SimilarityLookup</h1>
<?php
class Amazon_Info {
    const ACCECSS_KEY_ID = '********';
    const ASSOC_ID = '';
}

error_reporting(E_ALL);
set_time_limit(0);
ini_set('default_socket_timeout', 60*5);

$isbn = '479733245X';     // デフォルト
if (isset($_REQUEST['isbn'])) {
    $isbn = trim($_REQUEST['isbn']);
}
$isbn_for_form = htmlspecialchars($isbn, ENT_QUOTES);

// フォームの表示
echo <<<HTML_FORM_END
<form action="" method="get">
<label>ISBN: </label>
<input type="text" name="isbn" size="50"
 value="$isbn_for_form">
<br />
<input type="submit" value="検索">
</form><br>
<img src="http://images-jp.amazon.com/images/P/${isbn_for_form}.09.THUMBZZZ.jpg"><br>
HTML_FORM_END;

if (empty($isbn)) {
    exit();
}

// AmazonECS4
require_once 'Cache.php';
require_once 'Services/AmazonECS4.php';

// サービスオブジェクトの生成
$amazon = new Services_AmazonECS4(
    Amazon_Info::ACCECSS_KEY_ID,    // アクセスキー
    Amazon_Info::ASSOC_ID           // アソシエイトID
);
$result = $amazon->setLocale('JP');     // 日本を指定
if (PEAR::isError($result)) {
    echo "対応していない地域です";
    exit ();
}

// キャッシュ設定
$result = $amazon->setCache(
    'file', // キャッシュはファイルで行なう
    array(
        'cache_dir' => 'cached/' // キャッシュ保存先の指定
    )
);
if (PEAR::isError($result)) {
    echo htmlspecialchars($result->message, ENT_QUOTES);
    echo "キャッシュの設定に失敗しました";
    exit();
}
$amazon->setCacheExpire(24*60*60);

// 検索
$options['ResponseGroup'] = 'Medium';
$result = $amazon->SimilarityLookup($isbn, $options);
if (PEAR::isError($result)) {
    echo htmlspecialchars($result->message, ENT_QUOTES);
    echo "検索に失敗しました";
    exit();
}

//echo '<pre>';var_dump($result);echo '</pre>';
//exit();

echo "<h2>関連商品</h2>";
echo "<table>";
foreach ($result['Item'] as $item) {
    displayOne($item);
}
echo "</table>";


function displayOne($item) {
    $attributes = $item['ItemAttributes'];

    // HTMLで表示して問題の無い形式にエスケープ
    $title = htmlspecialchars(
        $attributes['Title'], ENT_QUOTES);
    if (isset($attributes['Author'])) {
        $author = htmlspecialchars(
            // 複数著者はカンマでまとめる
            implode(',', $attributes['Author']),
            ENT_QUOTES);
    } else {
        $author = '(著者無し)';
    }
    $detailUrl = htmlspecialchars(
        $item['DetailPageURL'], ENT_QUOTES);
    $imageUrl = htmlspecialchars(
        $item['SmallImage']['URL'], ENT_QUOTES);
    $formattedPrice = htmlspecialchars(
        $attributes['ListPrice']['FormattedPrice'], ENT_QUOTES);
    $asin = htmlspecialchars(
        $item['ASIN'], ENT_QUOTES);
    $salesRank = (integer)($item['SalesRank']);

    // 整形して表示
    echo "<tr><td>";
    echo "<a href=\"${detailUrl}\">";
    echo "<img src=\"${imageUrl}\" />";
    echo "</a></td><td>${title}<br>${author}<br>${asin}</td></tr>";
}

?>

</body></html>

$result にはこんな感じに格納されてる

array(2) {
  ["Request"]=>
  array(2) {
    ["IsValid"]=>
    string(4) "True"
    ["SimilarityLookupRequest"]=>
    array(2) {
      ["ItemId"]=>
      string(10) "4797336838"
      ["ResponseGroup"]=>
      array(1) {
        [0]=>
        string(6) "Medium"
      }
    }
  }
  ["Item"]=>
  array(10) {
    [0]=>
    array(9) {
      ["ASIN"]=>
      string(10) "4798015164"
      ["DetailPageURL"]=>
      string(175) "http://www.amazon.co.jp/gp/redirect.html%3FASIN=4798015164%26tag=ws%26lcode=xm2%26cID=2025%26ccmID=165953%26location=/o/ASIN/4798015164%253FSubscriptionId=1Q30RJF7SRHG6CREBBR2"
      ["SalesRank"]=>
      string(4) "6477"
      ["SmallImage"]=>
      array(3) {
        ["URL"]=>
        string(53) "http://ec1.images-amazon.com/images/I/11H8QYNCXNL.jpg"
        ["Height"]=>
        array(2) {
          ["Units"]=>
          string(6) "pixels"
          ["_content"]=>
          string(2) "75"
        }
        ["Width"]=>
        array(2) {
          ["Units"]=>
          string(6) "pixels"
          ["_content"]=>
          string(2) "58"
        }
      }
      ["MediumImage"]=>
      array(3) {
        ["URL"]=>
        string(53) "http://ec1.images-amazon.com/images/I/319270ZKJ6L.jpg"
        ["Height"]=>
        array(2) {
          ["Units"]=>
          string(6) "pixels"
          ["_content"]=>
          string(3) "160"
        }
        ["Width"]=>
        array(2) {
          ["Units"]=>
          string(6) "pixels"
          ["_content"]=>
          string(3) "124"
        }
      }
      ["LargeImage"]=>
      array(3) {
        ["URL"]=>
        string(53) "http://ec1.images-amazon.com/images/I/51BFWSV4WTL.jpg"
        ["Height"]=>
        array(2) {
          ["Units"]=>
          string(6) "pixels"
          ["_content"]=>
          string(3) "386"
        }
        ["Width"]=>
        array(2) {
          ["Units"]=>
          string(6) "pixels"
          ["_content"]=>
          string(3) "300"
        }
      }
      ["ImageSets"]=>
      array(1) {
        ["ImageSet"]=>
        array(5) {
          ["Category"]=>
          string(7) "primary"
          ["SwatchImage"]=>
          array(3) {
            ["URL"]=>
            string(53) "http://ec1.images-amazon.com/images/I/01DSMBSW18L.jpg"
            ["Height"]=>
            array(2) {
              ["Units"]=>
              string(6) "pixels"
              ["_content"]=>
              string(2) "30"
            }
            ["Width"]=>
            array(2) {
              ["Units"]=>
              string(6) "pixels"
              ["_content"]=>
              string(2) "23"
            }
          }
          ["SmallImage"]=>
          array(3) {
            ["URL"]=>
            string(53) "http://ec1.images-amazon.com/images/I/11H8QYNCXNL.jpg"
            ["Height"]=>
            array(2) {
              ["Units"]=>
              string(6) "pixels"
              ["_content"]=>
              string(2) "75"
            }
            ["Width"]=>
            array(2) {
              ["Units"]=>
              string(6) "pixels"
              ["_content"]=>
              string(2) "58"
            }
          }
          ["MediumImage"]=>
          array(3) {
            ["URL"]=>
            string(53) "http://ec1.images-amazon.com/images/I/319270ZKJ6L.jpg"
            ["Height"]=>
            array(2) {
              ["Units"]=>
              string(6) "pixels"
              ["_content"]=>
              string(3) "160"
            }
            ["Width"]=>
            array(2) {
              ["Units"]=>
              string(6) "pixels"
              ["_content"]=>
              string(3) "124"
            }
          }
          ["LargeImage"]=>
          array(3) {
            ["URL"]=>
            string(53) "http://ec1.images-amazon.com/images/I/51BFWSV4WTL.jpg"
            ["Height"]=>
            array(2) {
              ["Units"]=>
              string(6) "pixels"
              ["_content"]=>
              string(3) "386"
            }
            ["Width"]=>
            array(2) {
              ["Units"]=>
              string(6) "pixels"
              ["_content"]=>
              string(3) "300"
            }
          }
        }
      }
      ["ItemAttributes"]=>
      array(15) {
        ["Author"]=>
        array(3) {
          [0]=>
          string(13) "下岡 秀幸"
          [1]=>
          string(10) "道端 良"
          [2]=>
          string(10) "畑 勝也"
        }
        ["Binding"]=>
        string(9) "大型本"
        ["Creator"]=>
        array(3) {
          [0]=>
          array(2) {
            ["Role"]=>
            string(3) "著"
            ["_content"]=>
            string(13) "下岡 秀幸"
          }
          [1]=>
          array(2) {
            ["Role"]=>
            string(3) "著"
            ["_content"]=>
            string(10) "道端 良"
          }
          [2]=>
          array(2) {
            ["Role"]=>
            string(3) "著"
            ["_content"]=>
            string(10) "畑 勝也"
          }
        }
        ["EAN"]=>
        string(13) "9784798015163"
        ["ISBN"]=>
        string(10) "4798015164"
        ["Label"]=>
        string(18) "秀和システム"
        ["ListPrice"]=>
        array(3) {
          ["Amount"]=>
          string(4) "3150"
          ["CurrencyCode"]=>
          string(3) "JPY"
          ["FormattedPrice"]=>
          string(9) "¥ 3,150"
        }
        ["Manufacturer"]=>
        string(18) "秀和システム"
        ["NumberOfPages"]=>
        string(3) "313"
        ["PackageDimensions"]=>
        array(4) {
          ["Height"]=>
          array(2) {
            ["Units"]=>
            string(17) "hundredths-inches"
            ["_content"]=>
            string(3) "102"
          }
          ["Length"]=>
          array(2) {
            ["Units"]=>
            string(17) "hundredths-inches"
            ["_content"]=>
            string(3) "906"
          }
          ["Weight"]=>
          array(2) {
            ["Units"]=>
            string(17) "hundredths-pounds"
            ["_content"]=>
            string(3) "185"
          }
          ["Width"]=>
          array(2) {
            ["Units"]=>
            string(17) "hundredths-inches"
            ["_content"]=>
            string(3) "724"
          }
        }
        ["ProductGroup"]=>
        string(4) "Book"
        ["PublicationDate"]=>
        string(7) "2006-11"
        ["Publisher"]=>
        string(18) "秀和システム"
        ["Studio"]=>
        string(18) "秀和システム"
        ["Title"]=>
        string(42) "PHPによるデザインパターン入門"
      }
      ["OfferSummary"]=>
      array(6) {
        ["LowestNewPrice"]=>
        array(3) {
          ["Amount"]=>
          string(4) "3150"
          ["CurrencyCode"]=>
          string(3) "JPY"
          ["FormattedPrice"]=>
          string(9) "¥ 3,150"
        }
        ["LowestUsedPrice"]=>
        array(3) {
          ["Amount"]=>
          string(4) "4869"
          ["CurrencyCode"]=>
          string(3) "JPY"
          ["FormattedPrice"]=>
          string(9) "¥ 4,869"
        }
        ["TotalNew"]=>
        string(1) "1"
        ["TotalUsed"]=>
        string(1) "1"
        ["TotalCollectible"]=>
        string(1) "0"
        ["TotalRefurbished"]=>
        string(1) "0"
      }
    }

…