2016-07-31 156 views
0

我使用Amazon Product Advertising API PHP Class from Codediesel亚马逊产品广告API阅读优先

我使用的功能getItemByAsin,改变了ResponseGroupOffers,因为我想获得IsEligibleForPrime最便宜的报价(见Amazon JSON response

由于有更多然后1个报价在offer标签内的响应,我不知道如何读的第一个

这里是一个效应初探样子:

<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> 

如何从第一个<Offer></Offer>中读取IsEligibleForPrime

+0

在要订购哪些元素?最便宜或第一次报价? – Zl3n

+0

我希望获得条件为“新”的第一个(最便宜)优惠的IsEligibleForPrime内容。 – Paul

+0

好吧,让我们尝试一下 – Zl3n

回答

1

比方说,你在namepace环境

工作首先声明命名空间

namespace Amazon; 

其次,实例化的变量。然后,调用类的构造函数:

/** 
* 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; 
} 

我已经测试此代码,它的工作原理为ASIN你给你的问题。下面是结果:

  1. 可享有

    Amazon {#17 ▼ 
        +asin: "047061529X" 
        -amazonAPI: AmazonProductAPI {#16 ▼ 
        -public_key: "**********" 
        -private_key: "**********" 
        -associate_tag: "**********" 
        } 
        -eligible: 1 
    } 
    
  2. 没有资格

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

完整的源

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; 
    } 
} 

而且,事实上,你必须在getItemByAsin功能指定

"ResponseGroup" => "Medium,Offers" 

AmazonProductAPI

+0

Thx作为快速回复,不幸的是'$ IsEligibleForPrime'保持为空。我试图显示'$ items-> Offers-> TotalOffers',但即使是空的(无论什么原因)。因此,我检查了类中的responseGroup,但自从使用ResponseGroup =>“Medium,Offers”后似乎正确。我在做什么错了? – Paul

+1

请参阅我的编辑。我已经改写了一切 – Zl3n

相关问题