Download the PHP package colorfield/mastodon-api without Composer
On this page you can find all versions of the php package colorfield/mastodon-api. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download colorfield/mastodon-api
More information about colorfield/mastodon-api
Files in colorfield/mastodon-api
Package mastodon-api
Short Description PHP wrapper for Mastodon API
License Apache-2.0
Homepage https://github.com/colorfield/mastodon-api-php
Informations about the package mastodon-api
Mastodon API PHP
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
.
- Get an instance from the instance list.
- 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.
Request a public endpoint.
which is equivalent to
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
The default configuration is limited to the read
and write
scopes.
You can modify it via
or alternatively use enum
Note that this must be done while obtaining the token, so you cannot override this after. More about scopes.
Get the authorization code
- Get the authorization URL
$authorizationUrl = $oAuth->getAuthorizationUrl();
- Go to this URL, authorize and copy the authorization code.
Get the bearer
-
Store the authorization code in the configuration value object.
$oAuth->config->setAuthorizationCode(xxx);
- 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.
User login
Login with Mastodon email and password.
Get
Get credentials (assumes by default that authentication is provided).
Get followers
Post
Clear notifications
@todo complete with delete, put, patch and stream.
Development
Manual testing tools
OAuth
An interactive demo is available.
- Clone the GitHub repository.
- cd in the cloned directory
- Run
composer install
- Run
php -S localhost:8000
- In your browser, go to http://localhost:8000/test_oauth.php
- 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.
- Get the bearer: click on the "Get access token" button.
- Authenticate with your Mastodon username (email) and password: click on "Login".
Mastodon API
- Make a copy of
.env.local
as.env
- Define the information obtained with OAuth and your Mastodon email and password.
- In your browser, go to http://localhost:8000/test_api.php