Using the Amazon portal, generate a refresh token (Atzr|....). Note your client ID and client secret as well.
Pass it as a parameter to the following function:
Once you have the authorization token, use it to retrieve data from your desired endpoint, e.g.:
function http_request(string $url, string $token, array $params = array()) {
try {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json","User-Agent: YourSuperApp","x-amz-access-token: $token")); // Add the token to the header
if (!empty($params)) { // this is a POST, add body.
//Some endpoints expect POST instead of GET, there you can pass the array of values as the 3rd parameter of this function
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
}
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
if ($result === false) {
throw new Exception(curl_error($ch), curl_errno($ch));
}
return json_decode($result);
} catch(Exception $ex) {
writeLog($ex->getMessage(),'ERROR');
}
}
$items = http_request('https://sellingpartnerapi-eu.amazon.com/catalog/2022-04-01/items?marketplaceIds=["A1PA6795UKMFR9"]',$your_auth_token);
Then you can process the JSON which should be in this format. You might use var_dump($items); to see how it looks like.
Things to keep in mind:
API URLs are different per region, see here. I used the EU one.
Marketplace IDs can be found here. I used the german one.
If you’re going to be doing serious work with SP API, I highly recommend you use a pre-built library. I’m a big fan of selling-partner-api PHP library, which is Laravel compatible. I’ve been using it for years in multiple projects and it simplifies the process for getting the Amazon product catalog and many other SP API related functions. Especially, if you’re building a public app and need to deal with account authorizations, permissions, etc. I’ve been using it for creating and processing FBA shipments without any issues.
2
Answers
Using the Amazon portal, generate a refresh token (
Atzr|....
). Note your client ID and client secret as well.Pass it as a parameter to the following function:
Once you have the authorization token, use it to retrieve data from your desired endpoint, e.g.:
Then you can process the JSON which should be in this format. You might use
var_dump($items);
to see how it looks like.Things to keep in mind:
If you’re going to be doing serious work with SP API, I highly recommend you use a pre-built library. I’m a big fan of selling-partner-api PHP library, which is Laravel compatible. I’ve been using it for years in multiple projects and it simplifies the process for getting the Amazon product catalog and many other SP API related functions. Especially, if you’re building a public app and need to deal with account authorizations, permissions, etc. I’ve been using it for creating and processing FBA shipments without any issues.