How to get a category name with the ID via API

I use Discourse API client for PHP (by @michaeld I guess) and I can’t find how to get a category name/slug with only with an ID.

I need it to display (slug and id) of all our categories and their sub-categories.
But when I get the categories list, the only information about sub-categories is their id.

I asked the same question a long time ago,
https://meta.discourse.org/t/how-to-get-subcategory-names-using-api/29795

Thanks, is it implemented in your php client ?

No, we never implemented that. Should be fairly easy to do, I can take a look later today.

function getCategoryById($categoryId) {
       $datas = $this->_getRequest("/site.json");
       $categories = $datas->apiresult->categories;
       foreach($categories as $categorie) {
         if(($categorie->id) == $categoryId) {
           return $categorie;
         }
       }

You should probably save the data in a global, so you can call the function multiple times in the same PHP script.

I’d rather use /site.json once (to limit the API’s calls). We’ve added the following function to DiscourseApi.php :

function getSite()
    {
        return $this->_getRequest("/site.json");
    }

and then use it to get wanted informations :

  $categories = $discourseApi->getSite()->apiresult->categories;
  foreach ($categories as $category) {
    if (isset($category->parent_category_id)){
      $categories_all[$category->parent_category_id]['subcategory'][$category->id]['id'] = $category->id;
      $categories_all[$category->parent_category_id]['subcategory'][$category->id]['slug'] = $category->slug;
      $categories_all[$category->parent_category_id]['subcategory'][$category->id]['name'] = $category->name;
    } else {
      $categories_all[$category->id]['id'] = $category->id;
      $categories_all[$category->id]['slug'] = $category->slug;
      $categories_all[$category->id]['name'] = $category->name;
    }
  }

(code from congressus/application/config/discourse.structure.php at master · PartiPirate/congressus · GitHub)

(We don’t use the getCategoryById() I’ve suggested before)

site.json scheint der Weg zu sein, um eine Liste aller Kategorien zu erhalten. Wird dies irgendwo auf docs.discourse.org erwähnt? Ich habe die Dokumentation durchsucht, um eine Möglichkeit zu finden, die Namen der Unterkategorien zu erhalten, konnte sie aber nicht finden. Oder gibt es einen besseren Weg?

Eine API-Anfrage an site.json ist der richtige Weg, um eine Liste von Kategorien zu erhalten, die Unterkategorien einschließt. Wenn Sie zudem möchten, dass private Kategorien in der Antwort auf die Anfrage an site.json enthalten sind, müssen Sie eine authentifizierte API-Anfrage stellen.

Ich sehe in der Dokumentation keinen Hinweis auf site.json. Da categories.json keine Unterkategorien zurückgibt, ist die Beschreibung der categories-Route in der Dokumentation nicht korrekt; sie besagt, dass die Route alle Kategorien zurückgibt. Ich werde das beheben.