diff --git a/.gitignore b/.gitignore index 916043f..d84cf58 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ -/config.php +/twitter-config.php +/mastodon-config.php /.idea /vendor/ diff --git a/Mastodon/Mastodon.php b/Mastodon/Mastodon.php new file mode 100644 index 0000000..37bcd2f --- /dev/null +++ b/Mastodon/Mastodon.php @@ -0,0 +1,47 @@ +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, + 'Content-Type: multipart/form-data', + ]; + + $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) { + return json_encode(['ok'=>false, 'curl_error_code' => curl_errno($ch_status), 'curl_error' => curl_error(ch_status)]); + } + curl_close($ch); + + return json_decode($reply); + } +} diff --git a/mastodon-config.php.template b/mastodon-config.php.template new file mode 100644 index 0000000..3d550bc --- /dev/null +++ b/mastodon-config.php.template @@ -0,0 +1,8 @@ + '', + 'app_id' => '', + 'secret' => '', + 'access_token' => '', +]; \ No newline at end of file diff --git a/mastodon.php b/mastodon.php new file mode 100644 index 0000000..99311cb --- /dev/null +++ b/mastodon.php @@ -0,0 +1,40 @@ +saveTo($image_path); + + // send picture to mastodon + $app = new MastodonAPI($mastodon_config['access_token'], $mastodon_config['domain']); + $curl_file = curl_file_create(realpath($image_path)); + $body = ['file' => $curl_file]; + $response = $app->uploadMedia($body); + $file_id = $response->id; + $statusText = 'Nouveau collage'; + $status_data = [ + 'status' => $statusText, + 'visibility' => 'public', + 'language' => 'fr', + 'media_ids[]' => $file_id, + ]; + + $app->postStatus($status_data); +} catch (Exception $exception) { + echo $exception->getMessage(); +} + +