so.cl

Rascals are always sociable, and the chief sign that a man has any nobility in his character is the little pleasure he takes in others company. Arthur Schopenhauer


Posting to Mastodon API using PHP and cURL

Here is a very simple way to post to the Mastodon API using PHP and cURL.

You will need an access token which you can get from your Mastodon instance settings (Settings -> Development -> Your applications -> New application). Replace <ACESS_TOKEN> with your application access token and <MASTODON_INSTANCE_URL> with … your Mastodon instance URL.

$headers = [
  'Authorization: Bearer <ACCESS_TOKEN>'
];
$data = array(
	"status" => "Hello world!",
	"language" => "eng",
	"visibility" => "public"
);

$ch_status = curl_init();
curl_setopt($ch_status, CURLOPT_URL, "<MASTODON_INSTANCE_URL>/api/v1/statuses");
curl_setopt($ch_status, CURLOPT_POST, 1);
curl_setopt($ch_status, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch_status, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_status, CURLOPT_HTTPHEADER, $headers);
$status = json_decode(curl_exec($ch_status));

curl_close ($ch_status);

This will return a JSON-formatted Mastodon Status object in the $status variable. If you need more info, check out the documentation.