Easily Publish a Post on Mastodon with PHP

13 January 2025 - 508 mots - php

Mastodon is a decentralized, open-source, federated social network that provides an ethical alternative to traditional social media platforms. Unlike Twitter or Facebook, Mastodon is not controlled by a single central entity.

Mastodon’s decentralization is based on the concept of interconnected instances (or servers). Each instance is independently managed, with its own rules and community, while allowing users to interact with members of other instances. This architecture, built on the ActivityPub protocol, forms what is known as the “Fediverse” – a truly distributed social network.

The benefits of this decentralized approach include:

  • No ads or user tracking
  • Greater control over personal data
  • More effective and localized moderation adapted to each community
  • Resistance to censorship due to network distribution
Mastodon Logo

Now that we’ve introduced the Mastodon network, let’s learn how to publish a post on Mastodon using PHP.

Prerequisites

First, you need an account on a Mastodon instance. In my case, I use the instance piaille.fr.

You also need to obtain an access token.

Here’s how:

  • Log in to your Mastodon instance.
  • Go to the “Preferences” page.
  • Navigate to the “Development” menu.
  • Click on the “New application” button.
  • Set the application name: in our case, we’ll call it “MastoBot.”
  • Check the write:media and write:statuses scopes.
  • Save your settings; you’ll arrive at the list of applications.
  • Click on your application.
  • Retrieve your access token.

Theory

We will use the Mastodon API, which is documented online.

If you wish to use images, you first need to upload the media via the POST /api/v2/media endpoint. The response will return media identifiers (id) that you can use when creating the status (media_ids[]).

Implementation

The implementation will be done in PHP 8.4 with the Curl extension installed and enabled.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php

const MASTODON_ACCESS_TOKEN = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
const MASTODON_INSTANCE = 'https://piaille.fr';

enum MastodonStatusVisibility: string
{
  case Direct = 'direct';
  case Private = 'private';
  case Public = 'public';
  case Unlisted = 'unlisted';
}

function fnPostMastodon(
  string $status,
  string $language = 'en',
  bool $isSensitive = false,
  string $spoilerText = '',
  MastodonStatusVisibility $visibility = MastodonStatusVisibility::Public
  
) {
  $hCurl = curl_init();
  curl_setopt_array($hCurl, [
    CURLOPT_URL => MASTODON_INSTANCE . '/api/v1/statuses',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_HTTPHEADER => [
      'Authorization: Bearer '.MASTODON_ACCESS_TOKEN,
      'Content-Type: application/json',
      'Accept: application/json',
      'Accept-Charset: utf-8'
    ],
    CURLOPT_POSTFIELDS => json_encode([
      'status' => $status,
      'sensitive' => $isSensitive,
      'spoiler_text' => $spoilerText,
      'visibility' => $visibility->value,
    ]),
    CURLOPT_TIMEOUT => 20
  ]);
  $response = curl_exec($hCurl);
  curl_close($hCurl);

  return json_decode($response, true);
}

$response = fnPostMastodon(
  '🚧 Ceci est un test 🚧',
  'fr'
);

var_dump($response);

As you can see in the code, no library is used, and we work directly with Curl, which keeps the code simple.

The returned response is also documented in the Mastodon documentation.

After running the code, you will notice:

  • The application name is displayed in the bottom right corner.
  • You can also use emojis.
Result

Laisser un commentaire

Merci. Votre message a bien été enregistré.