sevaske/discourse
Composer 安装命令:
composer require sevaske/discourse
包简介
Discourse laravel package
README 文档
README
Discourse PHP SDK (Unofficial)
This is an unofficial SDK for interacting with the Discourse API & Discourse Connect (SSO) in PHP.
It provides a clean, PSR-compliant abstraction on top of the Discourse API, so you can interact with your forum programmatically in a structured way.
Requirements
- PHP ^7.4 || ^8.0
- PSR-18 HTTP Client (
psr/http-client) - PSR-17 HTTP Factories (
psr/http-factory,psr/http-message) - A PSR-compliant HTTP implementation such as:
Notes
- This library is not an official Discourse SDK.
- It’s intended to provide a strongly typed, PSR-compliant abstraction that can be used in Laravel, Symfony, or plain PHP projects.
Installation
Install via Composer:
composer require sevaske/discourse
Usage
API
use GuzzleHttp\Client; use GuzzleHttp\Psr7\HttpFactory; use Sevaske\Discourse\Services\Api; $discourseUrl = 'https://meta.discourse.com'; // your forum url $discourseApiKey = 'your-api-key'; $discourseApiUsername = 'your-username'; $httpFactory = new HttpFactory(); $client = new Client([ 'base_uri' => $discourseUrl, 'headers' => [ 'Api-Key' => $discourseApiKey, 'Api-Username' => $discourseApiUsername, ], ]); $api = new Api($client, $httpFactory, $httpFactory); // for examole, invites $response = $api->invites()->create('some@email.com'); $response->getHttpStatusCode(); // 200 $response->link || $response['link']; // meta.discourse.com/invites/qwerty // make custom request $response = $api->request('GET', '/categories.json', [ 'include_subcategories' => true, ]);
API References
Badges
$api->badges()->list(); $api->badges()->create(string $name, int $badgeTypeId); $api->badges()->update(int $id, string $name, int $badgeTypeId); $api->badges()->delete(int $id);
Categories
$api->categories()->list(?bool $includeSubcategories = null); $api->categories()->get(int $id); $api->categories()->create(string $name, array $extra = []); $api->categories()->update(int $id, string $name, array $extra = []);
Groups
$api->groups()->list(); $api->groups()->get(int|string $nameOrId, bool $byId = true); $api->groups()->create(string $name, array $extra = []); $api->groups()->update(int $id, string $name, array $extra = []); $api->groups()->delete(int $id); $api->groups()->getMembers(int $groupId); $api->groups()->addMembers(int $groupId, array $usernames); $api->groups()->removeMembers(int $groupId, array $usernames);
Invites
$api->invites()->create( string $email, bool $skipEmail = false, ?string $customMessage = null, ?int $maxRedemptionsAllowed = 1, ?int $topicId = null, ?string $groupIds = null, ?string $groupNames = null, ?string $expiresAt = null );
Notifications
$api->notifications()->list(); $api->notifications()->read(?int $id); // null to read all
Posts
$api->posts()->latest(?int $before = null); $api->posts()->get(int $id); $api->posts()->create(array $data); $api->posts()->update(int $id, string $raw, ?string $editReason = null); $api->posts()->delete(int $id); $api->posts()->lock(int $id); $api->posts()->unlock(int $id); $api->posts()->replies(int $id); $api->posts()->action(int $postId, int $postActionTypeId, ?bool $flagTopic = null);
Users
$api->users()->list( ?string $flag = null, // "active", "new", "staff", "suspended", "blocked", "suspect" ?string $order = null, ?bool $asc = null, ?int $page = null, ?bool $showEmails = null, ?bool $stats = null, ?string $email = null, ?string $ip = null ); $api->users()->getById(int $id); $api->users()->getByUsername(string $username); $api->users()->getByExternalId(string $externalId); $api->users()->create(string $name, string $email, string $password, string $username, array $extra = []); $api->users()->update(string $username, string $name, array $extra); $api->users()->delete(int $id, ?bool $deletePosts = null, ?bool $blockEmail = null, ?bool $blockUrls = null, ?bool $blockIp = null); $api->users()->activate(int $id); $api->users()->deactivate(int $id); $api->users()->logout(int $id); $api->users()->changePassword(string $token, string $username, string $password); $api->users()->sendPasswordResetEmail(string $login); $api->users()->badges(string $username);
Site
$api->site()->info(); $api->site()->basicInfo();
Extending with Macros
This SDK uses a Macroable trait (inspired by Laravel), which allows you to add new methods to the Api class at runtime.
Example: Simple Macro
use Sevaske\Discourse\Services\Api; Api::macro('ping', function () { return 'pong'; }); $api = new Api($client, $httpFactory, $httpFactory); $api->ping(); // "pong"
Connect (SSO)
Discourse sends a signed payload to your endpoint with sso and sig. Build and sign the response payload and redirect back to discourse.
Notice
You should always validate the signature before using the payload.
use Sevaske\Discourse\Exceptions\InvalidRequestSignature; use Sevaske\Discourse\Services\Signer; use Sevaske\Discourse\Services\Connect\RequestPayload; use Sevaske\Discourse\Services\Connect\ResponsePayload; $signer = new Signer('your-discourse-secret'); $sso = $_GET['sso'] ?? ''; $sig = $_GET['sig'] ?? ''; if (! $signer->validate($sig, $sso)) { throw new InvalidRequestSignature; } $request = new RequestPayload($sso); $response = (new ResponsePayload($signer))->build( $request->nonce(), 'my-user-id', 'myemail@mywebsite.com', [ // optional params 'name' => 'Naruto Uzumaki', ] ); $redirectUrl = $request->buildReturnUrl($response);
Running Tests
This library uses PHPUnit for testing.
You can run tests via Composer:
composer test
Also, you can run stan
composer stan
License
MIT License
sevaske/discourse 适用场景与选型建议
sevaske/discourse 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 24 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 09 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 sevaske/discourse 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sevaske/discourse 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 24
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 18
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-09-19