How to get a category name with the ID via API

AI-generated summary

In a discussion titled ‘How to get a category name with the ID via API’ under the Dev category, users explore how to retrieve category names and slugs using the Discourse API. anon89120937 seeks a way to get subcategory details (name/slug) from IDs using the PHP Discourse API client. michaeld references an old thread suggesting /site.json as the solution, citing riking. anon89120937 asks if this is implemented in the PHP client; michaeld confirms it isn’t but offers to add it. anon89120937 shares a custom getCategoryById function using /site.json, which riking advises caching for efficiency. Later, anon89120937 updates with a more efficient approach: fetching /site.json once and parsing all categories and subcategories into a structured array, referencing their GitHub implementation. nedbat notes the lack of documentation for site.json and asks if there’s a better method. simon confirms /site.json is the correct approach, especially for subcategories, and clarifies that authenticated requests are needed to include private categories. He also acknowledges the docs are outdated — categories.json doesn’t return all categories as claimed — and commits to fixing the documentation.

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 هو الطريقة للحصول على قائمة بجميع التصنيفات. هل تم ذكر ذلك في مكان ما في docs.discourse.org؟ كنت أبحث في الوثائق عن طريقة للحصول على أسماء التصنيفات الفرعية ولم أتمكن من العثور عليها. أو هل هناك طريقة أفضل؟

إن إجراء طلب API إلى site.json هو الطريقة الصحيحة للحصول على قائمة بالتصنيفات تتضمن التصنيفات الفرعية. أيضًا، إذا كنت تريد أن تُرجع الطلبات الموجهة إلى site.json تصنيفات خاصة، فيجب أن يكون طلب الـ API موثَّقًا.

لا أرى أي إشارة إلى site.json في الوثائق. وبما أن categories.json لا يُرجع التصنيفات الفرعية، فإن وصف مسار categories في الوثائق غير صحيح، حيث يذكر أن المسار يُرجع جميع التصنيفات. سأقوم بتصحيح ذلك.