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.
michaeld
(Michael - Communiteq)
7월 10, 2017, 9:10오전
2
Thanks, is it implemented in your php client ?
michaeld
(Michael - Communiteq)
7월 10, 2017, 10:17오전
4
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;
}
}
riking
(Kane York)
7월 14, 2017, 3:14오전
6
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)
nedbat
(Ned Batchelder)
3월 16, 2020, 10:22오후
8
site.json이 모든 카테고리의 목록을 가져오는 방법인 것 같습니다. docs.discourse.org 어딘가에 이에 대한 설명이 있나요? 하위 카테고리의 이름을 가져오는 방법을 찾으려고 문서를 꼼꼼히 살펴봤는데 찾을 수 없었습니다. 아니면 더 나은 방법이 있을까요?
simon
3월 17, 2020, 12:14오전
9
site.json에 대한 API 요청을 보내는 것이 하위 카테고리를 포함한 카테고리 목록을 가져오는 올바른 방법입니다. 또한 site.json 요청에서 비공개 카테고리가 반환되기를 원한다면 인증된 API 요청을 보내야 합니다.
문서에서 site.json에 대한 언급을 찾을 수 없습니다. categories.json은 하위 카테고리를 반환하지 않으므로, 문서의 categories 경로 설명은 정확하지 않습니다. 해당 경로가 모든 카테고리를 반환한다고 되어 있거든요. 이 부분을 수정하겠습니다.