⑥MWS実践-レポート(接続練習)-PHP-lib
- 2015年10月13日
- Amazon_MWS徹底解説
AmazonMWSにおけるPHPを用いた接続方法(題名にlibと記載しているのはAmazonのライブラリを利用するためです。)について以下のとおり解説します。
記載しているサンプルはAmazonで出品されている一覧を取得するためのものですが、本来は出品リストの取得はこちらで処理するような一連の流れになります。
今回は接続の練習ですのでレポートの要求をかけるところのみ(本来は結果を取得しないと出品リストは取得できません)を説明しています。リストを取得するための処理ですので間違っても問題にはなりませんので、気軽に実施してください。(出品などは実際に販売されてしまうため価格0で出品したりすると大変です。)
Amazonのサンプルを付けていますので、事前に以下の2点を実施しておく必要があります。
①MWSのアカウント登録(”⑤MWS実践-MWS登録方法“を参照)
②PHPのインストール(”PHP環境構築(XAMPPインストール)について“参照)
(1)サンプルプログラムのダウンロード
AmazonのMWSサイトからPHPのサンプルコードをダウンロード(右上の”DownLoad”を押下)します。(今回は商品APIを選択していますが、左のウィジットに各APIがありますので必要に応じて選択し”PHPクライアントライブラリ”を選択してください。)
(2)展開したサンプルプログラムを配置
今回は”C:\AmazonMWS\MWS_reportAPI”というフォルダを作成し、その下に配置しました。【実際はsrc配下の”MarketplaceWebService”フォルダ(このフォルダは名前変更不可)をコピーすれば動作します】
(3)配置したフォルダの”src\MarketplaceWebServiceProducts\Samples”の中の”.config.inc.php”ファイルの以下の行を、<xxx>内をMWS登録時に取得したID等に変更する。
define(‘AWS_ACCESS_KEY_ID’, ‘<Your Access Key ID>’);
define(‘AWS_SECRET_ACCESS_KEY’, ‘<Your Secret Key>’);
define (‘MERCHANT_ID’, ‘<Your Merchant Id>’);
以下の2つの<xxx>内も任意のIDに書き換える。
define(‘APPLICATION_NAME’, ‘<Your Application Name>’);
define(‘APPLICATION_VERSION’, ‘<Your Application Version or Build Number>’);
(4)(3)の”Samples”フォルダ内にある処理したいサンプルフィルを選び、ファイルを書き換える。今回は”RequestReport”を送信したいので、”RequestReportSample.php”を書き換える。以下のところをコメントを外します(“//”の削除)。また、変数も値を入れればOKです。
以下のところのコメントを外す。
$serviceUrl = “https://mws.amazonservices.com”;
$request = new MarketplaceWebService_Model_RequestReportRequest();
$request->setMarketplaceIdList($marketplaceIdArray);
$request->setMerchant(MERCHANT_ID);
$request->setReportType(‘_GET_MERCHANT_LISTINGS_DATA_’);
また、以下のところの<xxx>内をMWS登録時に取得したIDに変更する。
$marketplaceIdArray = array(“Id” => array(‘<Marketplace_Id_1>’,'<Marketplace_Id_2>’));
(5)PHPの認証エラーになるので証明書を配置します。
配置方法は”⑦MWS実践-認証エラー対処“を参照してください。
(6)DOSプロンプト(“ファイル名を指定して実行”で”cmd”)を開き、ディレクトリを移動し実行すればOKです。
【コマンド例】
①cd C:\AmazonMWS\MWS_reportAPI\src\MarketplaceWebService\Samples
②c:\xampp\php\php RequestReportSample.php
★書き換えた”RequestReportSample.php”のソースコードを以下に添付します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
<?php /** * PHP Version 5 * * @category Amazon * @package MarketplaceWebService * @copyright Copyright 2009 Amazon Technologies, Inc. * @link http://aws.amazon.com * @license http://aws.amazon.com/apache2.0 Apache License, Version 2.0 * @version 2009-01-01 */ /******************************************************************************* * Marketplace Web Service PHP5 Library * Generated: Thu May 07 13:07:36 PDT 2009 * */ /** * Report Sample */ include_once ('.config.inc.php'); /************************************************************************ * Uncomment to configure the client instance. Configuration settings * are: * * - MWS endpoint URL * - Proxy host and port. * - MaxErrorRetry. ***********************************************************************/ // IMPORTANT: Uncomment the approiate line for the country you wish to // sell in: // United States: $serviceUrl = "https://mws.amazonservices.com"; // United Kingdom //$serviceUrl = "https://mws.amazonservices.co.uk"; // Germany //$serviceUrl = "https://mws.amazonservices.de"; // France //$serviceUrl = "https://mws.amazonservices.fr"; // Italy //$serviceUrl = "https://mws.amazonservices.it"; // Japan //$serviceUrl = "https://mws.amazonservices.jp"; // China //$serviceUrl = "https://mws.amazonservices.com.cn"; // Canada //$serviceUrl = "https://mws.amazonservices.ca"; // India //$serviceUrl = "https://mws.amazonservices.in"; $config = array ( 'ServiceURL' => $serviceUrl, 'ProxyHost' => null, 'ProxyPort' => -1, 'MaxErrorRetry' => 3, ); /************************************************************************ * Instantiate Implementation of MarketplaceWebService * * AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY constants * are defined in the .config.inc.php located in the same * directory as this sample ***********************************************************************/ $service = new MarketplaceWebService_Client( AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, $config, APPLICATION_NAME, APPLICATION_VERSION); /************************************************************************ * Uncomment to try out Mock Service that simulates MarketplaceWebService * responses without calling MarketplaceWebService service. * * Responses are loaded from local XML files. You can tweak XML files to * experiment with various outputs during development * * XML files available under MarketplaceWebService/Mock tree * ***********************************************************************/ // $service = new MarketplaceWebService_Mock(); /************************************************************************ * Setup request parameters and uncomment invoke to try out * sample for Report Action ***********************************************************************/ // Constructing the MarketplaceId array which will be passed in as the the MarketplaceIdList // parameter to the RequestReportRequest object. $marketplaceIdArray = array("Id" => array('ATVPDKIKX0DER')); // @TODO: set request. Action can be passed as MarketplaceWebService_Model_ReportRequest // object or array of parameters // $parameters = array ( // 'Merchant' => MERCHANT_ID, // 'MarketplaceIdList' => $marketplaceIdArray, // 'ReportType' => '_GET_MERCHANT_LISTINGS_DATA_', // 'ReportOptions' => 'ShowSalesChannel=true', // ); // $request = new MarketplaceWebService_Model_RequestReportRequest($parameters); $request = new MarketplaceWebService_Model_RequestReportRequest(); $request->setMarketplaceIdList($marketplaceIdArray); $request->setMerchant(MERCHANT_ID); $request->setReportType('_GET_MERCHANT_LISTINGS_DATA_'); // Using ReportOptions // $request->setReportOptions('ShowSalesChannel=true'); invokeRequestReport($service, $request); /** * Get Report List Action Sample * returns a list of reports; by default the most recent ten reports, * regardless of their acknowledgement status * * @param MarketplaceWebService_Interface $service instance of MarketplaceWebService_Interface * @param mixed $request MarketplaceWebService_Model_GetReportList or array of parameters */ function invokeRequestReport(MarketplaceWebService_Interface $service, $request) { try { $response = $service->requestReport($request); echo ("Service Response\n"); echo ("=============================================================================\n"); echo(" RequestReportResponse\n"); if ($response->isSetRequestReportResult()) { echo(" RequestReportResult\n"); $requestReportResult = $response->getRequestReportResult(); if ($requestReportResult->isSetReportRequestInfo()) { $reportRequestInfo = $requestReportResult->getReportRequestInfo(); echo(" ReportRequestInfo\n"); if ($reportRequestInfo->isSetReportRequestId()) { echo(" ReportRequestId\n"); echo(" " . $reportRequestInfo->getReportRequestId() . "\n"); } if ($reportRequestInfo->isSetReportType()) { echo(" ReportType\n"); echo(" " . $reportRequestInfo->getReportType() . "\n"); } if ($reportRequestInfo->isSetStartDate()) { echo(" StartDate\n"); echo(" " . $reportRequestInfo->getStartDate()->format(DATE_FORMAT) . "\n"); } if ($reportRequestInfo->isSetEndDate()) { echo(" EndDate\n"); echo(" " . $reportRequestInfo->getEndDate()->format(DATE_FORMAT) . "\n"); } if ($reportRequestInfo->isSetSubmittedDate()) { echo(" SubmittedDate\n"); echo(" " . $reportRequestInfo->getSubmittedDate()->format(DATE_FORMAT) . "\n"); } if ($reportRequestInfo->isSetReportProcessingStatus()) { echo(" ReportProcessingStatus\n"); echo(" " . $reportRequestInfo->getReportProcessingStatus() . "\n"); } } } if ($response->isSetResponseMetadata()) { echo(" ResponseMetadata\n"); $responseMetadata = $response->getResponseMetadata(); if ($responseMetadata->isSetRequestId()) { echo(" RequestId\n"); echo(" " . $responseMetadata->getRequestId() . "\n"); } } echo(" ResponseHeaderMetadata: " . $response->getResponseHeaderMetadata() . "\n"); } catch (MarketplaceWebService_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"); } } ?> |
★DOSプロンプトでのコマンド例と成功した場合のログを表示します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\Users\rdc-58001970000>cd C:\AmazonMWS\MWS_reportAPI\src\MarketplaceWebService\Samples C:\AmazonMWS\MWS_reportAPI\src\MarketplaceWebService\Samples>c:\xampp\php\php RequestReportSample.php * Adding handle: conn: 0x24e0650 * Adding handle: send: 0 * Adding handle: recv: 0 * Curl_addHandleToPipeline: length: 1 * - Conn 0 (0x24e0650) send_pipe: 1, recv_pipe: 0 * About to connect() to proxy proxy.nex.west.ntt.co.jp port 8080 (#0) * Trying 10.74.64.138... * Connected to proxy.nex.west.ntt.co.jp (10.74.64.138) port 8080 (#0) * Establish HTTP proxy tunnel to mws.amazonservices.com:443 > CONNECT mws.amazonservices.com:443 HTTP/1.1 Host: mws.amazonservices.com:443 User-Agent: YnetGetList/1.0 (Language=PHP/5.4.16; Platform=Windows NT/i586/6.1; MWSClientVersion=2011-08-01) Proxy-Connection: Keep-Alive < HTTP/1.0 200 Connection established < * Proxy replied OK to CONNECT request * successfully set certificate verify locations: * CAfile: C:\xampp\php\cacert.pem CApath: none * SSL connection using RC4-MD5 * Server certificate: * subject: C=US; ST=Washington; L=Seattle; O=Amazon.com Inc.; CN=mws.amazonservices.com * start date: 2013-09-20 00:00:00 GMT * expire date: 2014-09-21 23:59:59 GMT * subjectAltName: mws.amazonservices.com matched * issuer: C=US; O=VeriSign, Inc.; OU=VeriSign Trust Network; OU=Terms of use at https://www.verisign.com/rpa (c)10; CN=Veri Sign Class 3 Secure Server CA - G3 * SSL certificate verify ok. > POST / HTTP/1.1 User-Agent: YnetGetList/1.0 (Language=PHP/5.4.16; Platform=Windows NT/i586/6.1; MWSClientVersion=2011-08-01) Host: mws.amazonservices.com Accept: */* Content-Length: 319 Content-Type: application/x-www-form-urlencoded * upload completely sent off: 319 out of 319 bytes < HTTP/1.1 200 OK < Date: Thu, 06 Feb 2014 03:27:37 GMT * Server AmazonMWS is not blacklisted < Server: AmazonMWS < x-mws-quota-max: 60.0 < x-mws-quota-remaining: 60.0 < x-mws-quota-resetsOn: Thu,06 Feb 2014 03:44:00 UTC < x-mws-response-context: uTjB0JsCRHGk2h48cYIMllqSR7t+BqwlRCykChcFLUbujLa7hOpbzDZ+r2ltU8 eO4omQhmLpa8wNoG/uSq < x-mws-response-context: M5sf0KiXSVFZ/dY6fwERUBQQXiQU41w/KC31Qtlm9dhAkTMv76JqcXR52zPk chkNmZskvA== < x-amz-request-id: b34d2b9f-0b0d-4610-8dfa75a11bdf < x-mws-request-id: b34d2b9f-0b0d-4610-8dfa75a11bdf < x-mws-timestamp: 2014-02-06T03:27:38.155Z < Content-Type: text/xml < Vary: User-Agent < Transfer-Encoding: chunked < * Connection #0 to host proxy.nex.west.ntt.co.jp left intact Service Response ============================================================================= RequestReportResponse RequestReportResult ReportRequestInfo ReportRequestId 9057334096 ReportType _GET_MERCHANT_LISTINGS_DATA_ StartDate 2014-02-06T03:27:38Z EndDate 2014-02-06T03:27:38Z SubmittedDate 2014-02-06T03:27:38Z ReportProcessingStatus _SUBMITTED_ ResponseMetadata RequestId b34d2b9f-0b0d-4610-8b2e-8dfa75a11bdf ResponseHeaderMetadata: RequestId: b34d2b9f-0b0d-4610-8b2e75a11bdf, ResponseContext: uTjB0JsCRHGk2h48cYIMllqSR7t+ Bqwh6tmRdnpf8grlRCykChcFLSkUbujLa7hOpbzDZKiXSVFZ/dY6fwERUBQQXiQU41w/KC31QqnI2lv1jCbm0PStlm9dh1xA kTMv76JqcXR52zPk chkNmZskvA==, Timestamp: 2014-02-06T03:27:38.155Z |
コメントを残す