我正在尝试从XML文件中获取数据的值。我能够很好地获得其他行,但我无法获取 [asin][/asin] 标签之间的数据,因为它是 PHP 中的保留字。我想知道什么是解决方法?
这是我的代码:
$xml = file_get_contents($request_url, false, $context);
$xml = simplexml_load_string($xml);
$item = $xml->Items->Item[0];
// ** this is my Problem **
$asin = htmlentities((string) $item->Asin);
// ** this is my Problem **
$title = htmlentities((string) $item->ItemAttributes->Title);
以下是 xml 文件的一部分:
<Item>
<ASIN>B00TSUGXKE</ASIN>
<ParentASIN>B010BWYDYA</ParentASIN>
其他元素工作正常。它只是这个标签"Asin",它是 php 中的一个保留字,所以不能使用。有没有其他方法可以只引用标签而不是 php 中的"asin"函数?
试试这个:
$asin = htmlentities((string) $item->{'ASIN'});
这应该为你做。
$asin = htmlentities((string) $item->{'ASIN'});
XML 区分大小写。我做了更多的测试,这工作得很好。
实际上问题是字母大小写。在 XML 中是大写的ASIN
但您在驼峰大小写中请求。
应该是:
$asin = htmlentities((string) $item->ASIN);