定制 colorfield/mastodon-api 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

colorfield/mastodon-api

Composer 安装命令:

composer require colorfield/mastodon-api

包简介

PHP wrapper for Mastodon API

README 文档

README

PHP wrapper for the Mastodon API, that includes OAuth helpers. Guzzle based.

This is a plain API wrapper, so it makes it more resilient to changes (new parameters, ...) from the API by letting the developer pass the desired endpoint and parameters.

Quick start

Install it via Composer. Latest version requires PHP 8.1. For previous versions of PHP, use v0.1.0.

composer require colorfield/mastodon-api
  1. Get an instance from the instance list.
  2. Get endpoints from the Mastodon API documentation: Getting started with the API and Guidelines and best practices

Some requests, like public timelines, do not require any authentication.

Get public data.

Initialize the API.

$name = 'MyMastodonApp';
$instance = 'mastodon.social';
$config = new Colorfield\Mastodon\ConfigurationVO($name, $instance);
$this->api = new MastodonAPI($config);

Request a public endpoint.

$timeline = $this->api->getPublicData('/timelines/public');

which is equivalent to

$timeline = $this->api->get('/timelines/public', [], false);

where the 3rd parameter indicates that we don't require any authentication.

Authenticate with OAuth

This is needed for endpoints that are requiring a token.

To get OAuth credentials, a lightweight client is also available in test_oauth, via a local PHP server. It provides the client id, client secret and bearer. See the Development section below.

Register your application

$name = 'MyMastodonApp';
$instance = 'mastodon.social';
$oAuth = new Colorfield\Mastodon\MastodonOAuth($name, $instance);

The default configuration is limited to the read and write scopes. You can modify it via

$oAuth->config->setScopes(['read', 'write', 'follow', 'push']);

or alternatively use enum

$oAuth->config->setScopes([
    OAuthScope::read->name, 
    OAuthScope::write->name, 
    OAuthScope::follow->name,
    OAuthScope::push->name
]);

Note that this must be done while obtaining the token, so you cannot override this after. More about scopes.

Get the authorization code

  1. Get the authorization URL $authorizationUrl = $oAuth->getAuthorizationUrl();
  2. Go to this URL, authorize and copy the authorization code.

Get the bearer

  1. Store the authorization code in the configuration value object. $oAuth->config->setAuthorizationCode(xxx);

  2. Then get the access token. As a side effect, it is store in the Configuration value object. $oAuth->getAccessToken();

Use the Mastodon API

Instantiate the Mastodon API with the configuration

The OAuth credentials should be stored in the Configuration value object for later retrieval.

$name = 'MyMastodonApp';
$instance = 'mastodon.social';
$oAuth = new Colorfield\Mastodon\MastodonOAuth($name, $instance);
$oAuth->config->setClientId('...');
$oAuth->config->setClientSecret('...');
$oAuth->config->setBearer('...');
$mastodonAPI = new Colorfield\Mastodon\MastodonAPI($oAuth->config);

User login

Login with Mastodon email and password.

$oAuth->authenticateUser($email, $password);

Get

Get credentials (assumes by default that authentication is provided).

$credentials = $mastodonAPI->get('/accounts/verify_credentials');

Get followers

$followers = $mastodonAPI->get('/accounts/USER_ID/followers');

Post

Clear notifications

$clearedNotifications = $mastodonAPI->post('/notifications/clear');

@todo complete with delete, put, patch and stream.

Development

Manual testing tools

OAuth

An interactive demo is available.

  1. Clone the GitHub repository.
  2. cd in the cloned directory
  3. Run composer install
  4. Run php -S localhost:8000
  5. In your browser, go to http://localhost:8000/test_oauth.php
  6. You will get the client_id and client_secret, click on the authorization URL link, then confirm the authorization under Mastodon and copy the authorization code.
  7. Get the bearer: click on the "Get access token" button.
  8. Authenticate with your Mastodon username (email) and password: click on "Login".

Authorize your application

Authorize your application

Mastodon API

  1. Make a copy of .env.local as .env
  2. Define the information obtained with OAuth and your Mastodon email and password.
  3. In your browser, go to http://localhost:8000/test_api.php

colorfield/mastodon-api 适用场景与选型建议

colorfield/mastodon-api 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.06k 次下载、GitHub Stars 达 29, 最近一次更新时间为 2017 年 10 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「rest」 「api」 「mastodon」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 colorfield/mastodon-api 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 colorfield/mastodon-api 我们能提供哪些服务?
定制开发 / 二次开发

基于 colorfield/mastodon-api 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 3.06k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 29
  • 点击次数: 14
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 29
  • Watchers: 5
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2017-10-29