fundevogel/php-mastodon

一个用于与 Mastodon 的 REST API 交互的 PHP 库。

0.6.0 2021-09-19 08:01 UTC

This package is auto-updated.

Last update: 2024-09-19 14:37:07 UTC


README

Release License Issues Status

一个用于与 Mastodon 的 REST API 交互的 PHP 库。其文档可以在 这里 找到。

注意:在开始之前,请确保首先 创建一个应用程序(在您的个人资料设置中的“开发”部分)。

入门

使用 Composer 安装此包

composer require fundevogel/php-mastodon

要了解如何实现此功能,请查看以下示例

<?php

require_once('vendor/autoload.php');

use Fundevogel\Mastodon\Api;

# Initialize Api for given instance (defaults to 'mastodon.social')
$api = new Api('freiburg.social');

# Generate access token via ..
# (1) .. OAuth call or ..
$api->accessToken = $api->oauth()->token('cl13nt_1d', 'cl13nt_s3cr3t')['access_token'];

# (2) .. app creation (create an app, get one `access_token` for free!)
$api->accessToken = 'ur-t0t4lly-s3cr3t-p4ssw@rd';

# If you want to obtain an authorization code, and want to know where to get one ..
$url = $api->getAuthURL('cl13nt_1d');

# .. but you might want to provide what you have & use the login helper
$api->logIn();

# This helper takes the authorization code as parameter - however,
# if you provided client key / secret / access token,
# it will attempt to provide whatever access level is possible

# Fetch statuses of given account
foreach ($api->accounts()->statuses('106612343490709443') as $status) {
    echo $status->content();
}

# Note: In case you obtained login-level access, you may omit the ID parameter, which gives back your own account's statuses, like so:
foreach ($api->accounts()->statuses() as $status) {
    echo $status->content();
}

# Fetch followers
foreach ($api->accounts()->followers('106612343490709443') as $follower) {
    echo $follower->displayName();
}

# View your timelines
$timelines = $api->timelines();

# (1) Public timeline
var_dump($timelines->public());

# (2) Home timeline (= accounts you are following)
var_dump($timelines->home());

# Fetch information about your instance
var_dump($api->instance()->get());


# ... enjoy playing with Mastodon's API!

注意:在大多数情况下,使用 $api->logIn() 就足够了。然后,您可以通过提供上面的账户 ID 作为参数,查看其他人的状态、您的状态等。

..但我需要更多的文档!

首先,请查看 文档

现在,所有 API 方法都与它们的 URL 对应方匹配

  • mastodon.example/api/v1/accounts/:id 变为 $api->accounts()->get($id)
  • mastodon.example/api/v1/accounts/:id/statuses 变为 $api->accounts()->statuses($id)
  • mastodon.example/api/v1/timelines/home 变为 $api->timelines()->home()
  • mastodon.example/api/v1/statuses/:id 变为 $api->statuses()->get($id)

..同样也适用于返回的 API 实体。例如,Status 实体方法与文档中找到的数组对应方法匹配(仅大写字母开头)

  • id 可以通过 $status->id() 访问
  • created_at 可以通过 $status->createdAt() 访问
  • account 可以通过 $status->account() 访问(这会返回一个 Account 实体)

路线图

  • 添加测试
  • 添加身份验证助手
  • 以单独的类返回 API 实体
  • 添加缺少的 API 方法
    • timelines/streaming
    • notifications/push
    • search
    • admin
  • 添加缺少的 API 实体
    • admin/account
    • admin/report
  • 更新空的 API 实体
    • error
    • pushsubscription
    • report
    • scheduledstatus

编码愉快!