Created
November 6, 2019 11:02
-
-
Save pandanote-info/d8338da77d7df57dbc5985be7d9523bc to your computer and use it in GitHub Desktop.
PA-APIv5のAPIを使った商品データ取得用関数の実装例。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* 第1引数にはPA-APIv5のアクセスキーを、第2引数にはASINの配列を指定する。 | |
* SDKへのパスは環境に合わせて変更すること。 | |
* エラーメッセージ表示部はコメントアウトしているので、必要に応じてコメントを外して利用すること。 | |
*/ | |
use Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\api\DefaultApi; | |
use Amazon\ProductAdvertisingAPI\v1\ApiException; | |
use Amazon\ProductAdvertisingAPI\v1\Configuration; | |
use Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\GetItemsRequest; | |
use Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\GetItemsResource; | |
use Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\PartnerType; | |
use Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\ProductAdvertisingAPIClientException; | |
use Amazon\ProductAdvertisingAPI\v1\ObjectSerializer; | |
use Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\GetItemsResponse; | |
require_once('/usr/share/php/paapi5-php-sdk/vendor/autoload.php'); | |
function getItems($data,$item_list) | |
{ | |
$config = new Configuration(); | |
/* | |
* Add your credentials | |
* Please add your access key here | |
*/ | |
$config->setAccessKey($data['access_key_id']); | |
# Please add your secret key here | |
$config->setSecretKey($data['secret_key']); | |
# Please add your partner tag (store/tracking id) here | |
$partnerTag = $data['amazon_associate_id']; | |
/* | |
* PAAPI host and region to which you want to send request | |
* For more details refer: https://webservices.amazon.com/paapi5/documentation/common-request-parameters.html#host-and-region | |
*/ | |
$config->setHost('webservices.amazon.co.jp'); | |
$config->setRegion('us-west-2'); | |
$apiInstance = new DefaultApi( | |
/* | |
* If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`. | |
* This is optional, `GuzzleHttp\Client` will be used as default. | |
*/ | |
new GuzzleHttp\Client(), $config); | |
# Request initialization | |
# Choose item id(s) | |
$itemIds = $item_list; | |
/* | |
* Choose resources you want from GetItemsResource enum | |
* For more details, refer: https://webservices.amazon.com/paapi5/documentation/get-items.html#resources-parameter | |
*/ | |
$resources = array( | |
GetItemsResource::ITEM_INFOTITLE, | |
GetItemsResource::OFFERSLISTINGSPRICE, | |
GetItemsResource::OFFERSLISTINGSDELIVERY_INFOIS_PRIME_ELIGIBLE, | |
GetItemsResource::OFFERSLISTINGSPROGRAM_ELIGIBILITYIS_PRIME_EXCLUSIVE, | |
GetItemsResource::OFFERSLISTINGSPROGRAM_ELIGIBILITYIS_PRIME_PANTRY, | |
GetItemsResource::OFFERSLISTINGSAVAILABILITYMESSAGE, | |
GetItemsResource::OFFERSLISTINGSAVAILABILITYMAX_ORDER_QUANTITY, | |
GetItemsResource::OFFERSLISTINGSAVAILABILITYMIN_ORDER_QUANTITY, | |
GetItemsResource::OFFERSLISTINGSAVAILABILITYTYPE, | |
GetItemsResource::IMAGESPRIMARYMEDIUM); | |
# Forming the request | |
$getItemsRequest = new GetItemsRequest(); | |
$getItemsRequest->setItemIds($itemIds); | |
$getItemsRequest->setPartnerTag($partnerTag); | |
$getItemsRequest->setPartnerType(PartnerType::ASSOCIATES); | |
$getItemsRequest->setResources($resources); | |
# Validating request | |
$invalidPropertyList = $getItemsRequest->listInvalidProperties(); | |
$length = count($invalidPropertyList); | |
if ($length > 0) { | |
echo "Error forming the request", PHP_EOL; | |
foreach ($invalidPropertyList as $invalidProperty) { | |
echo $invalidProperty, PHP_EOL; | |
} | |
return null; | |
} | |
# Sending the request | |
try { | |
$getItemsResponse = $apiInstance->getItems($getItemsRequest); | |
# Parsing the response | |
if ($getItemsResponse->getItemsResult() != null) { | |
return $getItemsResponse; | |
} | |
if ($getItemsResponse->getErrors() != null) { | |
return null; | |
} | |
} catch (ApiException $exception) { | |
/* | |
echo "Error calling PA-API 5.0!", PHP_EOL; | |
echo "HTTP Status Code: ", $exception->getCode(), PHP_EOL; | |
echo "Error Message: ", $exception->getMessage(), PHP_EOL; | |
if ($exception->getResponseObject() instanceof ProductAdvertisingAPIClientException) { | |
$errors = $exception->getResponseObject()->getErrors(); | |
foreach ($errors as $error) { | |
echo "Error Type: ", $error->getCode(), PHP_EOL; | |
echo "Error Message: ", $error->getMessage(), PHP_EOL; | |
} | |
} else { | |
echo "Error response body: ", $exception->getResponseBody(), PHP_EOL; | |
} | |
*/ | |
return null; | |
} catch (Exception $exception) { | |
echo "Error Message: ", $exception->getMessage(), PHP_EOL; | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment