Amazon Product Advertising API Чтение с первого чтения

Я использую API рекламы продукта Amazon PHP класс от Codediesel.

Я использую функцию getItemByAsin и изменил ResponseGroup в Offers потому что я хочу получить IsEligibleForPrime для самого дешевого предложения (см. Amazon JSON ответ.

Поскольку в ответе более 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>

Как я могу прочитать IsEligibleForPrime от первой <Offer></Offer>?

0

Решение

Допустим, вы работаете в среде имен

Сначала объявите свое пространство имен

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

И, действительно, вы должны указать

"ResponseGroup" => "Medium,Offers"

в getItemByAsin функция от AmazonProductAPI учебный класс

1

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector