【问题标题】:using simplexml_load_file() to get attributes from xml file for Amazon Affiliate API使用 simplexml_load_file() 从 xml 文件中获取 Amazon Affiliate API 的属性
【发布时间】:2017-07-25 15:21:01
【问题描述】:

我在这里使用 Amazon Affiliate API。实际上,我对这一切都很陌生。

我有 PHP 代码来为产品生成一个有效的 URL,但是我在从该 url 生成的 xml 中提取数据时遇到问题。我希望从 xml 中返回 FormattedPrice。

以下是 xml 文件的示例: https://pastebin.com/HnttEVfv

这是我试图提取“FormattedPrice”的代码。以下示例均无效,仅返回空值。

旁注:$request_url 是 xml 文件的完整 http:// 有效 url。

$getxml = simplexml_load_file($request_url); 
$price = $getxml->ItemLookupResponse->Items->Item->ItemAttributes->ListPrice['FormattedPrice'];

也没有

$price = $getxml->ItemLookupResponse->Items->Item->ItemAttributes->ListPrice->FormatedPrice;

【问题讨论】:

  • file_get_contents($request_url) 有返回值吗?
  • 如果它确实正确地将 XML 的内容作为字符串返回,你可以只使用 simplexml_load_string(file_get_contents($request_url))。
  • var_dump($getxml);转储但是当我尝试转到“FormattedPrice”时,我似乎无法从 XML.file_get_contents($request_url) 中获取价格代码,返回整个 xml 的字符串是的;但是包裹在加载字符串中不起作用。

标签: php xml amazon-web-services simplexml


【解决方案1】:

每个 XML 文档都有一个“根元素”。在您的示例中,该顶级元素是<ItemLookupResponse>

当您在 SimpleXML 中加载文档时,它为您提供的第一个对象就是该根元素。然后,您可以使用 ->ElementName 表示法访问该元素的 chlidren

所以你应该写$getxml->Items而不是$getxml->ItemLookupResponse->Items

【讨论】:

  • 我仍然得到一个空的回报。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-19
  • 1970-01-01
  • 2020-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多