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)
2017 年 7 月 10 日午前 9:10
2
「いいね!」 6
Thanks, is it implemented in your php client ?
michaeld
(Michael - Communiteq)
2017 年 7 月 10 日午前 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;
}
}
「いいね!」 2
riking
(Kane York)
2017 年 7 月 14 日午前 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.
「いいね!」 3
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)
2020 年 3 月 16 日午後 10:22
8
site.json は、すべてのカテゴリのリストを取得する方法のようです。docs.discourse.org のどこかに記載されていますか?サブカテゴリの名前を取得する方法を探してドキュメントをくまなく探しましたが、見つかりませんでした。あるいは、より良い方法があるでしょうか?
simon
2020 年 3 月 17 日午前 12:14
9
site.json への API リクエストが、サブカテゴリを含むカテゴリ一覧を取得する正しい方法です。また、プライベートカテゴリを site.json へのリクエストで返したい場合は、認証された API リクエストを行う必要があります。
ドキュメントに site.json への言及は見当たりません。categories.json はサブカテゴリを返さないため、ドキュメント内の categories ルートの説明は正しくありません。そこではこのルートが「すべての」カテゴリを返すと記載されています。これを修正します。
「いいね!」 2