我需要知道为什么这个调用ebay API GetCategoryInfo的代码不能正常工作,之前类似于检索信息,但对于我的生活我似乎无法让它工作,错误是foreach,说警告:无效的参数为foreach()提供的可能就是这条线吗?
“$ xml = new SimpleXMLElement($ resp);” appid是隐藏的,原因很明显,
$url = 'http://open.api.ebay.com/Shopping?callname=GetCategoryInfo';
$url .= '&appid=' . $appid . '&siteid=3&CategoryID=-1&version=729&IncludeSelector=ChildCategories';
$resp = file_get_contents($url);
$xml = new SimpleXMLElement($resp);
foreach($xml->searchResult->Item as $item)
{
$categoryName = (string) $item->primaryCategory->categoryName;
$catid = (string) $item->primaryCategory->CategoryID;
echo $categoryName . " " . $catid . " <br>\n";
}
更改以反映工作代码段
foreach($xml->CategoryArray->Category as $category)
{
$categoryName = (string) $category->CategoryName;
$catid = (string) $category->CategoryID;
echo $categoryName . " " . $catid . " <br>\n";
}
答案 0 :(得分:0)
如果您查看GetCategoryInfo的output,您会发现searchResult
不属于结果。
您需要将代码更改为:
foreach($ xml-&gt; CategoryArray-&gt;类别为$ category)