试图获取非对象SimpleXML的属性?

01j*_*yss 5 php simplexml

我目前正在使用以下代码从REST API中检索信息.

$url = "http://api.remix.bestbuy.com/v1/products%28upc=".$upc."%29?apiKey=(API KEY)";

$xmlfiledata = file_get_contents("$url");

$xmldata = new SimpleXMLElement($xmlfiledata);

$saleprice = $xmldata->products->product->salePrice;
echo $saleprice;
Run Code Online (Sandbox Code Playgroud)

但是,PHP正在返回此错误.

Notice: Trying to get property of non-object in FILE LOCATION on line 132
Run Code Online (Sandbox Code Playgroud)

第132行是:

$saleprice = $xmldata->products->product->salePrice;
Run Code Online (Sandbox Code Playgroud)

我已经验证了生成的URL是正确的.有问题的xml文档在这里(为简单起见,我简化了它).

<products currentPage="1" totalPages="1" from="1" to="1" total="1" queryTime="0.006" totalTime="0.014" canonicalUrl="/v1/products(upc="635753489873")?apiKey=xr2r8us3dcef7qdjnecbvh6g" partial="false">
<product>
<salePrice>529.99</salePrice>
</product>
</products>
Run Code Online (Sandbox Code Playgroud)

怎么修?

afu*_*ama 4

看看PHP.net 上的示例,我相信您需要像这样进行访问:

$saleprice = $xmldata->product[0]->salePrice;
Run Code Online (Sandbox Code Playgroud)

$xmldata实际上是你的“产品”级别,所以我认为你不需要...->products->...