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>';
}

Get Facebook Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.