我第一次使用MWS并希望创建一个程序,该程序使用ListMatchingProducts请求来平均每个匹配查询的产品的价格。
它应该是一个非常简单的程序,但我在检索数据时遇到了问题。
首先我打电话给亚马逊的xml表,然后我把xml转换成数组。
Print_R显示数组看起来像这样:
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] ...
我正在尝试访问数组的金额部分,因为这是对象的价格。最终,我需要访问每个产品的数量,因此循环可能会发挥作用,但是现在我甚至无法访问一个产品的销售额。
以下是我一直在尝试的代码
$value = $array->ListMatchingProductsResult->Products->Product[0]->ListPrice->Amount;
print_r($value);
它不起作用。甚至在$ array-> ListMatchingProductsResult上调用print_r也不会打印数组。
非常感谢任何帮助!
谢谢,
马特
答案 0 :(得分:2)
你快到了。无论您使用什么方法将XML转换为PHP数组,都可以:它创建一个关联的数组,而不是对象。这就是为什么你不能通过->element
运算符访问它,但需要使用数组索引['element']
$value = $array['ListMatchingProductsResult']['Products']['Product'][0]['ListPrice']['Amount'];
如果你想知道为什么你没有得到回复,你可以成功地缩短上述表达,直到你这样做。因此,如果上面的表达式上的print_r(...)
没有返回任何内容,只需从该print_r一次删除一个方括号,直到找到回复的内容。然后你知道你删除的最后一个括号是罪魁祸首。
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
...
顺便说一句,在尝试理解<pre>...</pre>
或print_r()
时,使用var_dump()
会很有帮助。
P.S。你欠我一个新的太空钥匙。
答案 1 :(得分:0)
这是一个使用亚马逊MWS的ListMatchingProducts的sdk库。您将能够获得访问所需信息的信息。
要显示php错误,请在php.in
中设置此错误display_errors = On
您也可以在php脚本
之上提供此功能ini_set('error_reporting', E_ALL);
ini_set('display_errors', 'On');
答案 2 :(得分:0)
原来哈兹特是对的。这是我访问数组的新代码。
$value = $array['ListMatchingProductsResult']['Products']['Product'][0]
['AttributeSets']['ItemAttributes']['ListPrice']['Amount'];
print_r($value);
Zapping似乎也以与MWS API的最佳实践相对应的方式回答了问题。我相信,对于比我更好的编码器,SDK是一个简单的指南,可以满足我的要求并且比我的阵列解决方案更可取。在进一步深入这个项目之前,我会尝试更多地了解sdk。