Skip to content

Commit 1a0172f

Browse files
authored
Updated README.md
1 parent 9b7a89e commit 1a0172f

File tree

1 file changed

+180
-1
lines changed

1 file changed

+180
-1
lines changed

README.md

+180-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,181 @@
11
# amazon-paapi5-php-sdk
2-
Official PHP SDK to access the Amazon Product Advertising API 5.0 (paapi5)
2+
Official PHP SDK to access the Amazon [Product Advertising API](https://webservices.amazon.com/paapi5/documentation/index.html) 5.0 (paapi5)
3+
4+
## Installation
5+
6+
The Product Advertising API PHP SDK can be installed with [Composer](https://getcomposer.org/). Run this command:
7+
8+
```sh
9+
$ composer require sanjayojha/amazon-paapi5-php-sdk
10+
```
11+
## Usage
12+
13+
Simple example for searching items.
14+
15+
```php
16+
<?php
17+
18+
/**
19+
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
20+
*
21+
* Licensed under the Apache License, Version 2.0 (the "License").
22+
* You may not use this file except in compliance with the License.
23+
* A copy of the License is located at
24+
*
25+
* http://www.apache.org/licenses/LICENSE-2.0
26+
*
27+
* or in the "license" file accompanying this file. This file is distributed
28+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
29+
* express or implied. See the License for the specific language governing
30+
* permissions and limitations under the License.
31+
*/
32+
33+
/*
34+
* ProductAdvertisingAPI
35+
*
36+
* https://webservices.amazon.com/paapi5/documentation/index.html
37+
*/
38+
39+
/*
40+
* This sample code snippet is for ProductAdvertisingAPI 5.0's SearchItems API
41+
*
42+
* For more details, refer: https://webservices.amazon.com/paapi5/documentation/search-items.html
43+
*/
44+
45+
use Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\api\DefaultApi;
46+
use Amazon\ProductAdvertisingAPI\v1\ApiException;
47+
use Amazon\ProductAdvertisingAPI\v1\Configuration;
48+
use Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\SearchItemsRequest;
49+
use Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\SearchItemsResource;
50+
use Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\PartnerType;
51+
use Amazon\ProductAdvertisingAPI\v1\com\amazon\paapi5\v1\ProductAdvertisingAPIClientException;
52+
53+
require_once(__DIR__ . '/vendor/autoload.php'); // change path as needed
54+
55+
56+
$config = new Configuration();
57+
58+
/*
59+
* Add your credentials
60+
* Please add your access key here
61+
*/
62+
$config->setAccessKey('<YOUR ACCESS KEY>');
63+
# Please add your secret key here
64+
$config->setSecretKey('<YOUR SECRET KEY>');
65+
66+
# Please add your partner tag (store/tracking id) here
67+
$partnerTag = '<YOUR PARTNER TAG>';
68+
69+
/*
70+
* PAAPI host and region to which you want to send request
71+
* For more details refer: https://webservices.amazon.com/paapi5/documentation/common-request-parameters.html#host-and-region
72+
*/
73+
$config->setHost('webservices.amazon.com');
74+
$config->setRegion('us-east-1');
75+
76+
$apiInstance = new DefaultApi(
77+
/*
78+
* If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
79+
* This is optional, `GuzzleHttp\Client` will be used as default.
80+
*/
81+
new GuzzleHttp\Client(), $config);
82+
83+
# Request initialization
84+
85+
# Specify keywords
86+
$keyword = 'Harry Potter';
87+
88+
/*
89+
* Specify the category in which search request is to be made
90+
* For more details, refer: https://webservices.amazon.com/paapi5/documentation/use-cases/organization-of-items-on-amazon/search-index.html
91+
*/
92+
$searchIndex = "All";
93+
94+
# Specify item count to be returned in search result
95+
$itemCount = 1;
96+
97+
/*
98+
* Choose resources you want from SearchItemsResource enum
99+
* For more details, refer: https://webservices.amazon.com/paapi5/documentation/search-items.html#resources-parameter
100+
*/
101+
$resources = array(
102+
SearchItemsResource::ITEM_INFOTITLE,
103+
SearchItemsResource::OFFERSLISTINGSPRICE);
104+
105+
# Forming the request
106+
$searchItemsRequest = new SearchItemsRequest();
107+
$searchItemsRequest->setSearchIndex($searchIndex);
108+
$searchItemsRequest->setKeywords($keyword);
109+
$searchItemsRequest->setItemCount($itemCount);
110+
$searchItemsRequest->setPartnerTag($partnerTag);
111+
$searchItemsRequest->setPartnerType(PartnerType::ASSOCIATES);
112+
$searchItemsRequest->setResources($resources);
113+
114+
# Validating request
115+
$invalidPropertyList = $searchItemsRequest->listInvalidProperties();
116+
$length = count($invalidPropertyList);
117+
if ($length > 0) {
118+
echo "Error forming the request", PHP_EOL;
119+
foreach ($invalidPropertyList as $invalidProperty) {
120+
echo $invalidProperty, PHP_EOL;
121+
}
122+
return;
123+
}
124+
125+
# Sending the request
126+
try {
127+
$searchItemsResponse = $apiInstance->searchItems($searchItemsRequest);
128+
129+
echo 'API called successfully', PHP_EOL;
130+
echo 'Complete Response: ', $searchItemsResponse, PHP_EOL;
131+
132+
# Parsing the response
133+
if ($searchItemsResponse->getSearchResult() != null) {
134+
echo 'Printing first item information in SearchResult:', PHP_EOL;
135+
$item = $searchItemsResponse->getSearchResult()->getItems()[0];
136+
if ($item != null) {
137+
if ($item->getASIN() != null) {
138+
echo "ASIN: ", $item->getASIN(), PHP_EOL;
139+
}
140+
if ($item->getDetailPageURL() != null) {
141+
echo "DetailPageURL: ", $item->getDetailPageURL(), PHP_EOL;
142+
}
143+
if ($item->getItemInfo() != null
144+
and $item->getItemInfo()->getTitle() != null
145+
and $item->getItemInfo()->getTitle()->getDisplayValue() != null) {
146+
echo "Title: ", $item->getItemInfo()->getTitle()->getDisplayValue(), PHP_EOL;
147+
}
148+
if ($item->getOffers() != null
149+
and $item->getOffers() != null
150+
and $item->getOffers()->getListings() != null
151+
and $item->getOffers()->getListings()[0]->getPrice() != null
152+
and $item->getOffers()->getListings()[0]->getPrice()->getDisplayAmount() != null) {
153+
echo "Buying price: ", $item->getOffers()->getListings()[0]->getPrice()
154+
->getDisplayAmount(), PHP_EOL;
155+
}
156+
}
157+
}
158+
if ($searchItemsResponse->getErrors() != null) {
159+
echo PHP_EOL, 'Printing Errors:', PHP_EOL, 'Printing first error object from list of errors', PHP_EOL;
160+
echo 'Error code: ', $searchItemsResponse->getErrors()[0]->getCode(), PHP_EOL;
161+
echo 'Error message: ', $searchItemsResponse->getErrors()[0]->getMessage(), PHP_EOL;
162+
}
163+
} catch (ApiException $exception) {
164+
echo "Error calling PA-API 5.0!", PHP_EOL;
165+
echo "HTTP Status Code: ", $exception->getCode(), PHP_EOL;
166+
echo "Error Message: ", $exception->getMessage(), PHP_EOL;
167+
if ($exception->getResponseObject() instanceof ProductAdvertisingAPIClientException) {
168+
$errors = $exception->getResponseObject()->getErrors();
169+
foreach ($errors as $error) {
170+
echo "Error Type: ", $error->getCode(), PHP_EOL;
171+
echo "Error Message: ", $error->getMessage(), PHP_EOL;
172+
}
173+
} else {
174+
echo "Error response body: ", $exception->getResponseBody(), PHP_EOL;
175+
}
176+
} catch (Exception $exception) {
177+
echo "Error Message: ", $exception->getMessage(), PHP_EOL;
178+
}
179+
```
180+
181+
Read [Complete documentation](https://webservices.amazon.com/paapi5/documentation/with-sdk.html).

0 commit comments

Comments
 (0)