Amazon MWS: Access to an array of eligible products
This is my first time working with MWS and am hoping to create a program that uses a ListMatchingProducts query to average the prices of each product that matches the query.
It should be a very simple program, but I am having a hard time getting the data.
First I make a call and get an amazon xml sheet, then I convert the xml to an array.
Print_R shows that the array looks something like this:
Array ( [ListMatchingProductsResult] => Array ( [Products] => Array ( [Product] => Array ( [0] => Array ( [Identifiers] => Array ( [MarketplaceASIN] => Array ( [MarketplaceId] => ATVPDKIKX0DER [ASIN] => 0786866020 ) ) [AttributeSets] => Array ( [ItemAttributes] => Array ( [Author] => Array ( [0] => Stephen C. Lundin [1] => Harry Paul [2] => John Christensen ) [Binding] => Hardcover [Brand] => Hyperion [Color] => White [Creator] => Ken Blanchard [Edition] => 1 [Feature] => Great product! [ItemDimensions] => Array ( [Height] => 8.25 [Length] => 5.50 [Width] => 0.00 [Weight] => 0.54 ) [IsAdultProduct] => false [Label] => Hyperion [Languages] => Array ( [Language] => Array ( [0] => Array ( [Name] => english [Type] => Published ) [1] => Array ( [Name] => english [Type] => Original Language ) [2] => Array ( [Name] => english [Type] => Unknown ) ) ) [ListPrice] => Array ( **[Amount] => 21.00** [CurrencyCode] => USD ) [Manufacturer] => Hyperion [ManufacturerMaximumAge] => 1188.0 [ManufacturerMinimumAge] => 156.0 [NumberOfItems] => 1 [NumberOfPages] => 110 [PackageDimensions] => Array ( [Height] => 0.65 [Length] => 8.60 [Width] => 5.65 [Weight] => 0.58 ) [PackageQuantity] => 1 [PartNumber] => 9780786866021 [ProductGroup] => Book [ProductTypeName] => ABIS_BOOK [PublicationDate] => 2000-03-08 [Publisher] => Hyperion [ReleaseDate] => 2000-03-08 [SmallImage] => Array ( [URL] => http://ecx.images-amazon.com/images/I/51cHo55tbOL._SL75_.jpg [Height] => 75 [Width] => 47 ) [Studio] => Hyperion [Title] => Fish: A Proven Way to Boost Morale and Improve Results ) ) [Relationships] => Array ( ) [SalesRankings] => Array ( [SalesRank] => Array ( [0] => Array ( [ProductCategoryId] => book_display_on_website [Rank] => 4629 ) [1] => Array ( [ProductCategoryId] => 1043856 [Rank] => 2 ) [2] => Array ( [ProductCategoryId] => 2635 [Rank] => 7 ) [3] => Array ( [ProductCategoryId] => 2637 [Rank] => 18 ) ) ) ) [1] ...
I am trying to access a portion of the sum of an array since that is the price of an object. Eventually, I will need to access the quantity of each product, and thus the cycle is likely to come into play, but right now I cannot even access the sales volume of a single product.
Here is the code I tried
$value = $array->ListMatchingProductsResult->Products->Product[0]->ListPrice->Amount;
print_r($value);
And it doesn't work. Even calling print_r on $ array-> ListMatchingProductsResult doesn't print the array.
Any help is greatly appreciated!
Thank,
Matt
source to share
You're almost there. Whatever method you use to convert XML to PHP array, it does exactly that: it creates an associative array of things, not an object. This is why you cannot access it via an operator ->element
, but you need to use array indices['element']
$value = $array['ListMatchingProductsResult']['Products']['Product'][0]['ListPrice']['Amount'];
If you've ever wondered why you're not getting something back, you can successfully reduce the expression until you do so. So if the expression print_r(...)
in the above expression returns nothing, just remove one square bracket at a time from that print_r until you get something back. Then you know that the last livestock you removed was the culprit.
Array (
[ListMatchingProductsResult] => Array (
[Products] => Array (
[Product] => Array (
[0] => Array (
[Identifiers] => Array (
[MarketplaceASIN] => Array (
[MarketplaceId] => ATVPDKIKX0DER
[ASIN] => 0786866020
)
)
[AttributeSets] => Array (
[ItemAttributes] => Array (
[Author] => Array (
[0] => Stephen C. Lundin
[1] => Harry Paul
[2] => John Christensen
)
[Binding] => Hardcover
[Brand] => Hyperion
[Color] => White
[Creator] => Ken Blanchard
[Edition] => 1
[Feature] => Great product!
[ItemDimensions] => Array (
[Height] => 8.25
[Length] => 5.50
[Width] => 0.00
[Weight] => 0.54
)
[IsAdultProduct] => false
[Label] => Hyperion
[Languages] => Array (
[Language] => Array (
[0] => Array (
[Name] => english
[Type] => Published
)
[1] => Array (
[Name] => english
[Type] => Original Language
)
[2] => Array (
[Name] => english
[Type] => Unknown
)
)
)
[ListPrice] => Array (
[Amount] => 21.00
[CurrencyCode] => USD
)
[Manufacturer] => Hyperion
...
BTW, using <pre>...</pre>
is helpful when trying to understand print_r()
or var_dump()
.
PS You owe me a new space key.
source to share
Here is the sdk library that uses the Amazon MWS ListMatchingProducts. You will be able to get the information you need to get the information you need.
To show that php errors have this set in your php.in
display_errors = On
You can also give this over PHP script
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 'On');
source to share
It turns out Hazzit was right. Here is my new code for array access.
$value = $array['ListMatchingProductsResult']['Products']['Product'][0]
['AttributeSets']['ItemAttributes']['ListPrice']['Amount'];
print_r($value);
Zapping also seems to have answered the question in a way that aligns with MWS API best practices. I'm sure that for better coders than me, the SDK is a simple guide for doing what I ask, and preferable to my array solution. I will try to learn more about sdk before going further into this project.
source to share