Amazon Product Advertising API 5.0 Umstellung

Wie kann man die alte 4er Version der Amazon Product Advertising API auf 5.0 umstellen?

Dokumentation

Hier die Dokumentation über alle möglichen API Anfragen:

https://webservices.amazon.com/paapi5/documentation/

Beispiel: Offer Anfrage um Savings herauszufinden / Unverbindliche Preisempfehlung (UVP) berechnen oder aktuelle Produktpreise zu beziehen

https://webservices.amazon.com/paapi5/documentation/offers.html

Programmcode

Im folgenden Demo Code müsst ihr eure API Keys, sowie den Partner Tag eintragen. Eure API Keys könnt ihr im Associates Bereich erstellen und einsehen (Tools -> Product Advertising API). Die Tracking ID / Partner Tag findet ihr hier (Mein Konto -> Tracking-ID’s verwalten).

<?php

/* Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. */
/* Licensed under the Apache License, Version 2.0. */

// Put your Secret Key in place of **********
$serviceName="ProductAdvertisingAPI";
$region="us-east-1";
$accessKey="DEINACCESSKEY";
$secretKey="DEINSECRETKEY";
$payload="{"
        ." \"PartnerTag\": \"DEINPARTNERTAG\","
        ." \"PartnerType\": \"Associates\","
        ." \"Marketplace\": \"www.amazon.com\""
        ."}";
$host="webservices.amazon.com";
$uriPath="/paapi5/searchitems";
$awsv4 = new AwsV4 ($accessKey, $secretKey);
$awsv4--->setRegionName($region);
$awsv4->setServiceName($serviceName);
$awsv4->setPath ($uriPath);
$awsv4->setPayload ($payload);
$awsv4->setRequestMethod ("POST");
$awsv4->addHeader ('content-encoding', 'amz-1.0');
$awsv4->addHeader ('content-type', 'application/json; charset=utf-8');
$awsv4->addHeader ('host', $host);
$awsv4->addHeader ('x-amz-target', 'com.amazon.paapi5.v1.ProductAdvertisingAPIv1.SearchItems');
$headers = $awsv4->getHeaders ();
$headerString = "";
foreach ( $headers as $key => $value ) {
    $headerString .= $key . ': ' . $value . "\r\n";
}
$params = array (
        'http' => array (
            'header' => $headerString,
            'method' => 'POST',
            'content' => $payload
        )
    );
$stream = stream_context_create ( $params );

$fp = @fopen ( 'https://'.$host.$uriPath, 'rb', false, $stream );

if (! $fp) {
    throw new Exception ( "Exception Occured" );
}
$response = @stream_get_contents ( $fp );
if ($response === false) {
    throw new Exception ( "Exception Occured" );
}
echo $response;

class AwsV4 {

    private $accessKey = null;
    private $secretKey = null;
    private $path = null;
    private $regionName = null;
    private $serviceName = null;
    private $httpMethodName = null;
    private $queryParametes = array ();
    private $awsHeaders = array ();
    private $payload = "";

    private $HMACAlgorithm = "AWS4-HMAC-SHA256";
    private $aws4Request = "aws4_request";
    private $strSignedHeader = null;
    private $xAmzDate = null;
    private $currentDate = null;

    public function __construct($accessKey, $secretKey) {
        $this->accessKey = $accessKey;
        $this->secretKey = $secretKey;
        $this->xAmzDate = $this->getTimeStamp ();
        $this->currentDate = $this->getDate ();
    }

    function setPath($path) {
        $this->path = $path;
    }

    function setServiceName($serviceName) {
        $this->serviceName = $serviceName;
    }

    function setRegionName($regionName) {
        $this->regionName = $regionName;
    }

    function setPayload($payload) {
        $this->payload = $payload;
    }

    function setRequestMethod($method) {
        $this->httpMethodName = $method;
    }

    function addHeader($headerName, $headerValue) {
        $this->awsHeaders [$headerName] = $headerValue;
    }

    private function prepareCanonicalRequest() {
        $canonicalURL = "";
        $canonicalURL .= $this->httpMethodName . "\n";
        $canonicalURL .= $this->path . "\n" . "\n";
        $signedHeaders = '';
        foreach ( $this->awsHeaders as $key => $value ) {
            $signedHeaders .= $key . ";";
            $canonicalURL .= $key . ":" . $value . "\n";
        }
        $canonicalURL .= "\n";
        $this->strSignedHeader = substr ( $signedHeaders, 0, - 1 );
        $canonicalURL .= $this->strSignedHeader . "\n";
        $canonicalURL .= $this->generateHex ( $this->payload );
        return $canonicalURL;
    }

    private function prepareStringToSign($canonicalURL) {
        $stringToSign = '';
        $stringToSign .= $this->HMACAlgorithm . "\n";
        $stringToSign .= $this->xAmzDate . "\n";
        $stringToSign .= $this->currentDate . "/" . $this->regionName . "/" . $this->serviceName . "/" . $this->aws4Request . "\n";
        $stringToSign .= $this->generateHex ( $canonicalURL );
        return $stringToSign;
    }

    private function calculateSignature($stringToSign) {
        $signatureKey = $this->getSignatureKey ( $this->secretKey, $this->currentDate, $this->regionName, $this->serviceName );
        $signature = hash_hmac ( "sha256", $stringToSign, $signatureKey, true );
        $strHexSignature = strtolower ( bin2hex ( $signature ) );
        return $strHexSignature;
    }

    public function getHeaders() {
        $this->awsHeaders ['x-amz-date'] = $this->xAmzDate;
        ksort ( $this->awsHeaders );

        // Step 1: CREATE A CANONICAL REQUEST
        $canonicalURL = $this->prepareCanonicalRequest ();

        // Step 2: CREATE THE STRING TO SIGN
        $stringToSign = $this->prepareStringToSign ( $canonicalURL );

        // Step 3: CALCULATE THE SIGNATURE
        $signature = $this->calculateSignature ( $stringToSign );

        // Step 4: CALCULATE AUTHORIZATION HEADER
        if ($signature) {
            $this->awsHeaders ['Authorization'] = $this->buildAuthorizationString ( $signature );
            return $this->awsHeaders;
        }
    }

    private function buildAuthorizationString($strSignature) {
        return $this->HMACAlgorithm . " " . "Credential=" . $this->accessKey . "/" . $this->getDate () . "/" . $this->regionName . "/" . $this->serviceName . "/" . $this->aws4Request . "," . "SignedHeaders=" . $this->strSignedHeader . "," . "Signature=" . $strSignature;
    }

    private function generateHex($data) {
        return strtolower ( bin2hex ( hash ( "sha256", $data, true ) ) );
    }

    private function getSignatureKey($key, $date, $regionName, $serviceName) {
        $kSecret = "AWS4" . $key;
        $kDate = hash_hmac ( "sha256", $date, $kSecret, true );
        $kRegion = hash_hmac ( "sha256", $regionName, $kDate, true );
        $kService = hash_hmac ( "sha256", $serviceName, $kRegion, true );
        $kSigning = hash_hmac ( "sha256", $this->aws4Request, $kService, true );

        return $kSigning;
    }

    private function getTimeStamp() {
        return gmdate ( "Ymd\THis\Z" );
    }

    private function getDate() {
        return gmdate ( "Ymd" );
    }
}

?>

Verarbeitung

Um die Daten zu verarbeiten müsst ihr zuerst in PHP die JSON Antwort der Amazon API decodieren:

$response = json_decode($response);

Alternativ könnt ihr auch durch einen Online JSON Formatter, die echo Ausgabe formatieren:

http://jsonviewer.stack.hu/

Amazon Product Advertising API 5.0 Umstellung

Der Zugriff auf die einzelnen Produktdaten müsst ihr nach der Dokumentation anpassen. In der Payload Variable im Demo Code werden die einzelnen Daten angefragt.

$payload="{"
         ." \"Keywords\": \"$Keyword\","
         ." \"PartnerTag\": \"domainparking1337-21\","
         ." \"PartnerType\": \"Associates\","
         ." \"Marketplace\": \"www.amazon.de\","
         ." \"Resources\": ["
         ." \"Offers.Listings.Price\","
         ." \"Images.Primary.Medium\","
         ." \"ItemInfo.Title\","
         ." \"ItemInfo.ByLineInfo\","
         ." \"ItemInfo.Features\""
         ." ]"
         ."}";

Beispiel: Ausgabe der Produktbeschreibung als HTML Liste

echo "<ul>";
foreach ($product->ItemInfo->Features->DisplayValues as $key => $value) {       
  echo "$value";   
}   
echo "</ul>";

Beispiel: Ausgabe des Produktpreises

if($product->Offers->Listings[0]->Price->Amount) {
  echo ("<span class='preis'>Preis: ".$product->Offers->Listings[0]->Price->DisplayAmount."</span><br>"); 
}

Bewerte diesen Artikel

0 Bewertung(en), Durchschnitt: 0 von 5

Dieser Artikel wurde noch nicht bewertet.

Es tut uns leid, dass dir der Beitrag nicht gefallen hat.

Lass uns genau diesen Artikel überarbeiten.

Erzähle uns, was wir besser machen können.

Beitrag teilen

Hinterlasse einen Kommentar

  Abonnieren  
Benachrichtige mich bei
Scroll to Top