LoginSignup
1
1

More than 3 years have passed since last update.

MWSでAmazonの最安値を取得する

Last updated at Posted at 2019-12-12

MWSのサンプルだと設定が不足していて動かなかったのでメモ。

1. MWSのClientLibraryをダウンロード

https://developer.amazonservices.com.au/phpclients
※PHPです
「Products」をクリック。

2. 「.config.inc.php」を編集

MWSProductsPHPClientLibrary-2011-10-01/src/MarketplaceWebServiceProducts/Samples/.config.inc.php
で、以下の項目を埋める。

  • AWS_ACCESS_KEY_ID
  • AWS_SECRET_ACCESS_KEY
  • MERCHANT_ID
  • MARKETPLACE_ID

以下も必要なので、「.config.inc.php」に追記。

define('MWS_AUTH_TOKEN', '***');

3. 「GetLowestOfferListingsForASINSample.php」を参考に実装

必要なファイルを読み込む。

require_once 'MWSProductsPHPClientLibrary-2011-10-01/src/MarketplaceWebServiceProducts/Client.php';
require_once 'MWSProductsPHPClientLibrary-2011-10-01/src/MarketplaceWebServiceProducts/Exception.php';
require_once 'MWSProductsPHPClientLibrary-2011-10-01/src/MarketplaceWebServiceProducts/Interface.php';
require_once 'MWSProductsPHPClientLibrary-2011-10-01/src/MarketplaceWebServiceProducts/Model/ASINListType.php';
require_once 'MWSProductsPHPClientLibrary-2011-10-01/src/MarketplaceWebServiceProducts/Model.php';
require_once 'MWSProductsPHPClientLibrary-2011-10-01/src/MarketplaceWebServiceProducts/Model/GetLowestOfferListingsForASINRequest.php';
require_once 'MWSProductsPHPClientLibrary-2011-10-01/src/MarketplaceWebServiceProducts/Model/GetLowestOfferListingsForASINResponse.php';
require_once 'MWSProductsPHPClientLibrary-2011-10-01/src/MarketplaceWebServiceProducts/Samples/.config.inc.php';

「invokeGetLowestOfferListingsForASIN」という関数をサンプルからコピペし、xmlを返すように改造する。

  function invokeGetLowestOfferListingsForASIN(MarketplaceWebServiceProducts_Interface $service, $request)
  {
      try {
        $response = $service->GetLowestOfferListingsForASIN($request);

        echo ("Service Response\n");
        echo ("=============================================================================\n");

        $dom = new DOMDocument();
        $dom->loadXML($response->toXML());
        $dom->preserveWhiteSpace = false;
        $dom->formatOutput = true;
        //echo $dom->saveXML();
        //echo("ResponseHeaderMetadata: " . $response->getResponseHeaderMetadata() . "\n");

        $xml = $dom->saveXML();
        $xmlObject = simplexml_load_string($xml);
        return $xmlObject;

     } catch (MarketplaceWebServiceProducts_Exception $ex) {
        echo("Caught Exception: " . $ex->getMessage() . "\n");
        echo("Response Status Code: " . $ex->getStatusCode() . "\n");
        echo("Error Code: " . $ex->getErrorCode() . "\n");
        echo("Error Type: " . $ex->getErrorType() . "\n");
        echo("Request ID: " . $ex->getRequestId() . "\n");
        echo("XML: " . $ex->getXML() . "\n");
        echo("ResponseHeaderMetadata: " . $ex->getResponseHeaderMetadata() . "\n");
     }
 }

requestを作成する。

    $serviceUrl = "https://mws.amazonservices.jp/Products/2011-10-01";

    $config = array (
        'ServiceURL' => $serviceUrl,
        'ProxyHost' => null,
        'ProxyPort' => -1,
        'ProxyUsername' => null,
        'ProxyPassword' => null,
        'MaxErrorRetry' => 3,
    );

    $service = new MarketplaceWebServiceProducts_Client(
        AWS_ACCESS_KEY_ID,
        AWS_SECRET_ACCESS_KEY,
        APPLICATION_NAME,
        APPLICATION_VERSION,
        $config
    );

    $request = new MarketplaceWebServiceProducts_Model_GetLowestOfferListingsForASINRequest();
    $request->setSellerId(MERCHANT_ID);
    $request->setMWSAuthToken(MWS_AUTH_TOKEN);
    $asinList = new MarketplaceWebServiceProducts_Model_ASINListType();
    $asinList->setASIN('XXXXXXX');// ここでASINを指定する
    $request->setASINList($asinList);
    $request->setMarketplaceId(MARKETPLACE_ID);
    $xml = invokeGetLowestOfferListingsForASIN($service, $request);

4. xmlから最安値を取得する

    $amazon_price = null;
    $amazon_used_price = null;

    if (isset($xml->GetLowestOfferListingsForASINResult->Product->LowestOfferListings->LowestOfferListing)) {
        foreach ($xml->GetLowestOfferListingsForASINResult->Product->LowestOfferListings->LowestOfferListing as $listing) {
            if ($listing->Qualifiers->ItemCondition == 'New') {
                $amazon_price = intval($listing->Price->LandedPrice->Amount);
                break;
            }
        }
        foreach ($xml->GetLowestOfferListingsForASINResult->Product->LowestOfferListings->LowestOfferListing as $listing) {
            if ($listing->Qualifiers->ItemCondition == 'Used') {
                $amazon_used_price = intval($listing->Price->LandedPrice->Amount);
                break;
            }
        }
    }

感想

$serviceUrl = "https://mws.amazonservices.jp/Products/2011-10-01";

「2011-10-01」これって日付で絞り込むために指定してるのかと思いましたが、バージョンの指定らしいです。
2011年からバージョンアップしてないって、完成度たけーなオイwww

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1