Mastodon-Bot-PHP/Mastodon.php

47 lines
1.3 KiB
PHP
Raw Permalink Normal View History

2020-04-10 21:48:35 +02:00
<?php
2020-04-10 21:54:59 +02:00
class MastodonAPI
{
private $token;
private $instance_url;
public function __construct($token, $instance_url)
{
$this->token = $token;
$this->instance_url = $instance_url;
}
public function postStatus($status)
{
return $this->callAPI('/api/v1/statuses', 'POST', $status);
}
public function uploadMedia($media)
{
return $this->callAPI('/api/v1/media', 'POST', $media);
}
public function callAPI($endpoint, $method, $data)
{
$headers = [
'Authorization: Bearer '.$this->token,
2022-12-17 18:51:20 +01:00
'Content-Type: multipart/form-data',
2020-04-10 21:54:59 +02:00
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->instance_url.$endpoint);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$reply = curl_exec($ch);
if (!$reply) {
2023-05-09 10:59:05 +02:00
return json_encode(['ok'=>false, 'curl_error_code' => curl_errno($ch), 'curl_error' => curl_error($ch)]);
2020-04-10 21:54:59 +02:00
}
curl_close($ch);
return json_decode($reply, true);
}
}