Search code examples
ebay-api

calling GetCategories ebay API to list all


I need to know why this code calling ebay API GetCategoryInfo wont work, ive down similar before to retrieve information but for the life of me I cant seem to get it to work, the error is the foreach, saying Warning: Invalid argument supplied for foreach() could it be this line?

" $xml = new SimpleXMLElement($resp);" the appid is hidden for obvious reasons,

$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";  
}

changes to reflect working snippet

foreach($xml->CategoryArray->Category as $category) 
{
   $categoryName = (string) $category->CategoryName;
   $catid = (string) $category->CategoryID;
   echo $categoryName . " " . $catid . " <br>\n";  
}

Solution

  • If you look at the output for GetCategoryInfo you can see that searchResult is not part of the result.

    You need to change your code to:

    foreach($xml->CategoryArray->Category as $category)