2016-07-31 3 views
0

Ich verwende die Amazon Product Advertising API PHP Class from Codediesel.Amazon Produkt Werbung API Lesen zuerst Angebot

Ich verwende die Funktion getItemByAsin und verändert die ResponseGroup zu Offers, weil ich die IsEligibleForPrime für das billigste Angebot erhalten möchten (siehe Amazon JSON response.

Da es mehr als 1 Angebot in der die Reaktion innerhalb des Tags offer sind, ich weiß nicht, wie die ersten lesen

Hier ist, was eine exzellente Spielbarkeit wie folgt aussieht:.

<Item> 
    <ASIN>047061529X</ASIN> 
<Offers> 
    <TotalOffers>2</TotalOffers> 
    <TotalOfferPages>1</TotalOfferPages> 
    <MoreOffersUrl>http://www.amazon.com/gp/offer-listing/047061529X/?SubscriptionId=AKIAI44QH8DHBEXAMPLE&ie=UTF8&tag=adrpik-20&creative=386001&camp=2025&linkCode=xm2 </MoreOffersUrl> 
    <Offer> 
     <OfferAttributes> 
     <Condition>New</Condition> 
     </OfferAttributes> 
     <OfferListing> 
     <OfferListingId>6vZH%2FR4dOoabV7sTSv3vC0Np5xK1c8MKOhAl5HYbCIJhxOLlJw1O2AM6mLYyVhjnI8s2gMkx7yq%2F%2BEC7yKPWkQTqhVhFBeUDm71EdpaYwrXpppfcAL1yPzyQFkOuK6MsK8aLPSNSWVs%3D </OfferListingId> 
     <Price> 
      <Amount>1025</Amount> 
      <CurrencyCode>USD</CurrencyCode> 
      <FormattedPrice>$10.25</FormattedPrice> 
     </Price> 
     <AmountSaved> 
      <Amount>974</Amount> 
      <CurrencyCode>USD</CurrencyCode> 
      <FormattedPrice>$9.74</FormattedPrice> 
     </AmountSaved> 
     <PercentageSaved>49</PercentageSaved> 
     <Availability>Usually ships in 1-2 business days</Availability> 
     <AvailabilityAttributes> 
      <AvailabilityType>now</AvailabilityType> 
      <MinimumHours>24</MinimumHours> 
      <MaximumHours>48</MaximumHours> 
     </AvailabilityAttributes> 
     <IsEligibleForSuperSaverShipping>0</IsEligibleForSuperSaverShipping> 
     <IsEligibleForPrime>1</IsEligibleForPrime> 
     </OfferListing> 
    </Offer> 
    <Offer> 
    <OfferAttributes> 
    <Condition>Used</Condition> 
    </OfferAttributes> 
    <OfferListing> 
    <OfferListingId>uXUlLeu7rH5t3ogkZJ%2Bd11tWCsdsj5kHhjoscRF1D1GuBuDwCyrz0XyR%2BTEOJO7PgpfwLjtX4ojhbXeHZgM0Br4DiWsPhNZTduzvYC8zLgG0z1e%2FgYiiuuR0wTyKqssY6ncHyVjZK1A%3D </OfferListingId> 
    <Price> 
     <Amount>1110</Amount> 
     <CurrencyCode>USD</CurrencyCode> 
     <FormattedPrice>$11.10</FormattedPrice> 
    </Price> 
    <AmountSaved> 
     <Amount>889</Amount> 
     <CurrencyCode>USD</CurrencyCode> 
     <FormattedPrice>$8.89</FormattedPrice> 
    </AmountSaved> 
    <PercentageSaved>44</PercentageSaved> 
    <Availability>Usually ships in 1-2 business days</Availability> 
    <AvailabilityAttributes> 
     <AvailabilityType>now</AvailabilityType> 
     <MinimumHours>24</MinimumHours> 
     <MaximumHours>48</MaximumHours> 
    </AvailabilityAttributes> 
    <IsEligibleForSuperSaverShipping>0</IsEligibleForSuperSaverShipping> 
    <IsEligibleForPrime>1</IsEligibleForPrime> 
    </OfferListing> 
</Offer> 
</Offers> 

Wie kann ich die IsEligibleForPrime von der ersten <Offer></Offer> lesen?

+0

Auf angeben, welches Element Sie bestellen möchten? Das billigste oder das erste Angebot? – Zl3n

+0

Ich möchte den Inhalt von IsEigibleForPrime des ersten (günstigsten) Angebots bekommen, wo die Bedingung "neu" ist. – Paul

+0

Ok, lass uns etwas versuchen – Zl3n

Antwort

1

Sagen wir, Sie in namepace Umgebung

Zuerst erklären Ihren Namensraum

namespace Amazon; 

Zweitens arbeiten, Ihre Variablen instanziiert. Dann rufen Sie das Konstrukt Funktion Ihrer Klasse:

/** 
* Check from Amazon 
* @param string The ASIN we are looking for 
*/ 
public function __construct($asin) 
{ 
    $this->amazonAPI = new AmazonProductAPI(); // Or call from it right place 
    $this->asin = $asin; 
    $this->getResults(); 
} 

Und da, rufen Sie die Funktion, die für das Ergebnis sucht Sie:

private function getResults() 
{ 
    // Call public function getItemByAsin($asin_code) from AmazonProductAPI class 
    $items = $this->amazonAPI->getItemByAsin($this->asin); 

    foreach ($items->Items->Item->Offers as $offer) { 

     if ($offer->Offer->OfferAttributes->Condition == 'New') { 
      $IsEligibleForPrime = $offer->Offer->OfferListing->IsEligibleForPrime; 
     } 
    } 
    $this->eligible = $IsEligibleForPrime == 1 ? 1 : 0; 
} 

ich diesen Code getestet und es funktioniert für die ASIN gibst du in deiner Frage. Hier ist das Ergebnis:

  1. ist berechtigt

    Amazon {#17 ▼ 
        +asin: "047061529X" 
        -amazonAPI: AmazonProductAPI {#16 ▼ 
        -public_key: "**********" 
        -private_key: "**********" 
        -associate_tag: "**********" 
        } 
        -eligible: 1 
    } 
    
  2. ist nicht berechtigt,

    Amazon {#17 ▼ 
        +asin: "B01E67UWX2" 
        -amazonAPI: AmazonProductAPI {#16 ▶} 
        -eligible: 0 
    } 
    

Voll Quelle:

namespace devSO\Amazon; 

class Amazon 
{ 
    /** 
    * @var string 
    */ 
    public $asin; 

    /** 
    * @var object 
    */ 
    private $amazonAPI; 

    /** 
    * @var int 
    */ 
    private $eligible; 

    /** 
    * Check from Amazon 
    * @param string The ASIN we are looking for 
    */ 
    public function __construct($asin) 
    { 
     $this->amazonAPI = new AmazonProductAPI(); // Or call from it right place 
     $this->asin = $asin; 
     $this->getResults(); 
    } 

    private function getResults() 
    { 
     // Call public function getItemByAsin($asin_code) from AmazonProductAPI class 
     $items = $this->amazonAPI->getItemByAsin($this->asin); 

     foreach ($items->Items->Item->Offers as $offer) { 

      if ($offer->Offer->OfferAttributes->Condition == 'New') { 
       $IsEligibleForPrime = $offer->Offer->OfferListing->IsEligibleForPrime; 
      } 
     } 
     $this->eligible = $IsEligibleForPrime == 1 ? 1 : 0; 
    } 
} 

Und in der Tat haben Sie

"ResponseGroup" => "Medium,Offers" 

in getItemByAsin Funktion von AmazonProductAPI Klasse

+0

Thx für die schnelle Antwort, leider '$ IsElligistForPrime' bleibt leer. Ich habe versucht, '$ items-> Offers-> TotalOffers' anzuzeigen, aber auch das ist leer (aus welchem ​​Grund auch immer). Ich habe deshalb die 'responseGroup' in der Klasse überprüft, scheint aber recht zu sein, da ich' "ResponseGroup" => "Medium, Offers" 'verwende. Was mache ich falsch? – Paul

+1

Bitte sehen Sie meine Bearbeitung. Ich habe alles neu geschrieben – Zl3n

Verwandte Themen