リクエストURLの作成

URLに認証用のコードを含める必要があるため、少し手間がかかります。

処理手順についてはAuthenticating REST Requestsに詳しい解説があります。また、Signed Requests Helperで、この処理を模擬できます。

サンプルコード

define( "AWS_ENDPOINT",         "http://ecs.amazonaws.jp/onca/xml" );
define( "AWS_SERVICE",          "AWSECommerceService" );
define( "AWS_ACCESS_KEY",       "DUMMY" );
define( "AWS_SECRET_ACCESS_KEY","dummy" );
define( "AWS_ASSOCIATE_TAG",    "name-10" );
define( "AWS_VERSION",          "2009-07-01" );

// リクエストURLの取得
function GetRequestUrl( $operation, $searchIndex, $keywords, $responseGroup, $sort )
{
    $request = array(
        "Service"        =>AWS_SERVICE,
        "AWSAccessKeyId" =>AWS_ACCESS_KEY,
        "AssociateTag"   =>AWS_ASSOCIATE_TAG,
        "Version"        =>AWS_VERSION,
        "Operation"      =>$operation,
        "SearchIndex"    =>$searchIndex,
        "Keywords"       =>$keywords,
        "ResponseGroup"  =>$responseGroup,
        "Timestamp"      =>gmdate( "Y-m-d\TH:i:s\Z" ),
        );

    if( $searchIndex != "All" && $searchIndex != "Blended" )
    {
        $request[ "Sort" ] = $sort;
    }

    // アルファベット順にソートする
    ksort( $request );

    $parameter = "";
    foreach( $request as $key=>$value )
    {
        // 値をURLエンコードして キーと連結する
        $parameter .= $key."=".rawurlencode( $value )."&";
    }
    $parameter = rtrim( $parameter, "&" );

    $url = parse_url( AWS_ENDPOINT );
    $sign = "GET\n"
        ."{$url[ 'host' ]}\n"
        ."{$url[ 'path' ]}\n"
        .$parameter;

    // Secret Access Keyを使用し署名を作成する
    $signature = base64_encode( hash_hmac( "sha256", $sign, AWS_SECRET_ACCESS_KEY, TRUE ) );

    return AWS_ENDPOINT."?{$parameter}&Signature=".rawurlencode( $signature );
}

レスポンスの取得

結果はXML形式ですので、たとえばsimplexml_load_file()を用いることで簡単に処理できます。上記で作成したURLにアクセスすると、次のような結果が返されます。

SimpleXMLElement Object
(
  [OperationRequest] => SimpleXMLElement Object
    (
      [RequestId] => dummy
      [Arguments] => SimpleXMLElement Object
        (
          [Argument] => Array
            (
              [0] => SimpleXMLElement Object
                (
                  [@attributes] => Array
                    (
                      [Name] => Operation
                      [Value] => ItemSearch
                    )
                )
            )
        )
      [RequestProcessingTime] => 0.5050370000000000
    )
  [Items] => SimpleXMLElement Object
    (
      [Request] => SimpleXMLElement Object
        (
          [IsValid] => True
          [ItemSearchRequest] => SimpleXMLElement Object
            (
              [Condition] => New
              [DeliveryMethod] => Ship
              [Keywords] => パソコン
              [MerchantId] => Amazon
              [ResponseGroup] => Medium
              [ReviewSort] => -SubmissionDate
              [SearchIndex] => Electronics
              [Sort] => price
            )
        )
      [TotalResults] => 24793
      [TotalPages] => 2480
      [Item] => Array
        (
          [0] => SimpleXMLElement Object
            (
              [ASIN] => B00008B0TJ
              [DetailPageURL] => http://www.amazon.co.jp/%E3%82%A8%E3%83%97%E3%82%BD%E3%83%B3-EPSON
              [ItemLinks] => SimpleXMLElement Object
                (
                  [ItemLink] => Array
                    (
                      [0] => SimpleXMLElement Object
                        (
                          [Description] => Add To Wishlist
                          [URL] => http://www.amazon.co.jp/gp/registry/wishlist/add-item.html
                        )
                      [1] => SimpleXMLElement Object
                        (
                          [Description] => Tell A Friend
                          [URL] => http://www.amazon.co.jp/gp/pdp/taf/
                        )
                      [2] => SimpleXMLElement Object
                        (
                          [Description] => All Customer Reviews
                          [URL] => http://www.amazon.co.jp/review/product/
                        )
                      [3] => SimpleXMLElement Object
                        (
                          [Description] => All Offers
                          [URL] => http://www.amazon.co.jp/gp/offer-listing/
                        )
                    )
                )
              [SalesRank] => 1327
              [SmallImage] => SimpleXMLElement Object
                (
                  [URL] => http://ecx.images-amazon.com/images/I/21EFEPRAMAL._SL75_.jpg
                  [Height] => 75
                  [Width] => 75
                )
              [MediumImage] => SimpleXMLElement Object
                (
                  [URL] => http://ecx.images-amazon.com/images/I/21EFEPRAMAL._SL160_.jpg
                  [Height] => 150
                  [Width] => 150
                )
              [LargeImage] => SimpleXMLElement Object
                (
                  [URL] => http://ecx.images-amazon.com/images/I/21EFEPRAMAL.jpg
                  [Height] => 150
                  [Width] => 150
                )
              [ImageSets] => SimpleXMLElement Object
                (
                  [ImageSet] => SimpleXMLElement Object
                    (
                      [@attributes] => Array
                        (
                          [Category] => primary
                        )
                      [SwatchImage] => SimpleXMLElement Object
                        (
                          [URL] => http://ecx.images-amazon.com/images/I/21EFEPRAMAL._SL30_.jpg
                          [Height] => 30
                          [Width] => 30
                        )
                      [SmallImage] => SimpleXMLElement Object
                        (
                          [URL] => http://ecx.images-amazon.com/images/I/21EFEPRAMAL._SL75_.jpg
                          [Height] => 75
                          [Width] => 75
                        )
                      [ThumbnailImage] => SimpleXMLElement Object
                        (
                          [URL] => http://ecx.images-amazon.com/images/I/21EFEPRAMAL._SL75_.jpg
                          [Height] => 75
                          [Width] => 75
                        )
                      [TinyImage] => SimpleXMLElement Object
                        (
                          [URL] => http://ecx.images-amazon.com/images/I/21EFEPRAMAL._SL110_.jpg
                          [Height] => 110
                          [Width] => 110
                        )
                      [MediumImage] => SimpleXMLElement Object
                        (
                          [URL] => http://ecx.images-amazon.com/images/I/21EFEPRAMAL._SL160_.jpg
                          [Height] => 150
                          [Width] => 150
                        )
                      [LargeImage] => SimpleXMLElement Object
                        (
                          [URL] => http://ecx.images-amazon.com/images/I/21EFEPRAMAL.jpg
                          [Height] => 150
                          [Width] => 150
                        )
                    )
                )
              [ItemAttributes] => SimpleXMLElement Object
                (
                  [Binding] => エレクトロニクス
                  [Brand] => エプソン
                  [EAN] => 4965957830369
                  [Feature] => Array
                    (
                      [0] => インクジェットカートリッジ
                      [1] => 1個
                    )
                  [Label] => エプソン
                  [ListPrice] => SimpleXMLElement Object
                    (
                      [Amount] => 1260
                      [CurrencyCode] => JPY
                      [FormattedPrice] => ¥ 1,260
                    )
                  [Manufacturer] => エプソン
                  [MPN] => ICBK21
                  [PackageDimensions] => SimpleXMLElement Object
                    (
                      [Height] => 87
                      [Length] => 472
                      [Weight] => 13
                      [Width] => 362
                    )
                  [PackageQuantity] => 1
                  [ProductGroup] => CE
                  [ProductTypeName] => INK_OR_TONER
                  [Publisher] => エプソン
                  [ReleaseDate] => 2001-10-05
                  [Studio] => エプソン
                  [Title] => EPSON ICBK21(モノクロインクカートリッジ)
                )
              [OfferSummary] => SimpleXMLElement Object
                (
                  [LowestNewPrice] => SimpleXMLElement Object
                    (
                      [Amount] => 250
                      [CurrencyCode] => JPY
                      [FormattedPrice] => ¥ 250
                    )
                  [LowestUsedPrice] => SimpleXMLElement Object
                    (
                      [Amount] => 1
                      [CurrencyCode] => JPY
                      [FormattedPrice] => ¥ 1
                    )
                  [LowestRefurbishedPrice] => SimpleXMLElement Object
                    (
                      [Amount] => 630
                      [CurrencyCode] => JPY
                      [FormattedPrice] => ¥ 630
                    )
                  [TotalNew] => 26
                  [TotalUsed] => 3
                  [TotalCollectible] => 0
                  [TotalRefurbished] => 6
                )
              [EditorialReviews] => SimpleXMLElement Object
                (
                  [EditorialReview] => Array
                    (
                      [0] => SimpleXMLElement Object
                        (
                          [Source] => Product Description
                          [Content] => 黒/染料インクハイライトからシャドーまで、
                          [IsLinkSuppressed] => 0
                        )
                      [1] => SimpleXMLElement Object
                        (
                          [Source] => Amazon.co.jp 商品紹介
                          [Content] =>  対応機種:PM-930C、PM-940C、PM-950C、PM-970C、PM-980C
                          [IsLinkSuppressed] => 0
                        )
                    )
                )
            )
        )
    )
)