O'Reilly logo

Facebook Cookbook by Jay Goldman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Get Marketplace Categories and Subcategories

Problem

I need to pull in the list of Marketplace categories and subcategories.

Solution

Use the Marketplace.getCategories() method:

$categories = $facebook->api_client->marketplace_getCategories();

Once you have a category, use the Marketplace.getSubCategories() method to retrieve its subcategories:

$subcategories = $facebook->api_client->marketplace_getSubCategories('FORSALE');

Discussion

You can quite easily combine the two methods to generate a <select> representing the hierarchy of the Marketplace:

$categories = $facebook->api_client->marketplace_getCategories();
if($categories){
    echo '<select>';
    foreach($categories as $category){
        echo '<option value="' . $category . '">'. $category . '</option>';
        $subcategories = $facebook->api_client->marketplace_
getSubCategories($category);
        if($subcategories){
            foreach($subcategories as $subcategory){
                echo '<option value="' . $subcategory . '">&nbsp;'
 . $subcategory . '</option>';
            }
        }
    }
    echo '</select>';
}

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required