revolution/laravel-mastodon-api

Laravel 的 Mastodon API

3.1.4 2024-08-25 02:30 UTC

README

要求

  • PHP >= 8.1
  • Laravel >= 10.0

安装

Composer

composer require revolution/laravel-mastodon-api

用法

注册应用程序

通过 Web UI

  1. 前往你的 Mastodon 用户偏好设置页面。
  2. 前往开发页面。

通过 API

use Revolution\Mastodon\Facades\Mastodon;

class MastodonController
{
    public function app()
    {
        $client_name = 'my-app';
        $redirect_uris = 'https://my-instance/callback';
        $scopes = 'read write follow';
        
        $app_info = Mastodon::domain('https://example.com')
                            ->createApp($client_name, $redirect_uris, $scopes);

        dd($app_info);
        //[
        //    'id' => '',
        //    'client_id' => '',
        //    'client_secret' => '',
        //]
     }
}

OAuth 认证

使用 https://github.com/kawax/socialite-mastodon

保存账户信息。(id、token、username、acct 等。)

获取状态

use Revolution\Mastodon\Facades\Mastodon;

$statuses = Mastodon::domain('https://example.com')
                    ->token('token')
                    ->statuses($account_id);

dd($statuses);

获取一个状态

use Revolution\Mastodon\Facades\Mastodon;

$status = Mastodon::domain('https://example.com')
                  ->token('token')
                  ->status($status_id);

dd($status);

发布状态

use Revolution\Mastodon\Facades\Mastodon;

Mastodon::domain('https://example.com')->token('token');
$response = Mastodon::createStatus('test1');
$response = Mastodon::createStatus('test2', ['visibility' => 'unlisted']);

dd($response);

通过 get 或 post 方法调用任何 API

use Revolution\Mastodon\Facades\Mastodon;

$response = Mastodon::domain('https://example.com')
                    ->token('token')
                    ->get('/timelines/public', ['local' => true]);
use Revolution\Mastodon\Facades\Mastodon;

$response = Mastodon::domain('https://example.com')
                    ->token('token')
                    ->post('/follows', ['uri' => '']);

通过 call 方法调用任何 API

use Revolution\Mastodon\Facades\Mastodon;

$response = Mastodon::domain('https://example.com')
                    ->token('token')
                    ->call('DELETE', '/statuses/1');

通过 Guzzle 发送任何请求

自行设置所有数据

use Revolution\Mastodon\Facades\Mastodon;

$url = 'https://example.com/api/v1/instance';

$options = [
    'headers' => [
        'Authorization' => 'Bearer ' . $token,
    ]
]

$response = Mastodon::request('GET', $url, $options);

dd($response);

无需 Laravel 或 Facade

use GuzzleHttp\Client;
use Revolution\Mastodon\MastodonClient;

$mastodon = new MastodonClient(new Client);

$statuses = $mastodon->domain('https://example.com')
                     ->token('token')
                     ->statuses($account_id);

其他方法

Contracts/Factory.php 中检查公开方法

流式 API

编辑 streaming_example.php 中的 $token$url

php ./streaming_example.php

Ctrl+C 退出。

许可协议

MIT