diff --git a/Mastodon.php b/Mastodon.php index 44a8c40..09b442e 100644 --- a/Mastodon.php +++ b/Mastodon.php @@ -1,41 +1,46 @@ 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, true); - } - } -?> \ No newline at end of file + 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, + '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, true); + } + } diff --git a/examples/media_bot.php b/examples/media_bot.php index b3bf82c..b77ed06 100644 --- a/examples/media_bot.php +++ b/examples/media_bot.php @@ -1,32 +1,30 @@ $curl_file - ]; - - $response = $mastodon -> uploadMedia($body); - - $file_id = $response->id; - - $statusText = "This is a status"; - - $status_data = array( - "status" => $statusText, - "visibility" => $visibility, - "language" => $language, - "media_ids[]" => $file_id - ); - - $mastodon -> postStatus($status_data); - -?> \ No newline at end of file + + include_once 'Mastodon.php'; + + $token = 'XXXXX'; // Token of your Mastodon welcome bot account + $base_url = 'https://botsin.space'; // URL of your instance (Do not include '/' at the end.) + $visibility = 'private'; // "Direct" means sending welcome message as a private message. The four tiers of visibility for toots are Public , Unlisted, Private, and Direct (default) + $language = 'en'; // en for English, zh for Chinese, etc. + + $mastodon = new MastodonAPI($token, $base_url); + + $curl_file = curl_file_create('./imageOnServer.jpg', 'image/jpg', 'imagename.jpg'); + $body = [ + 'file' => $curl_file, + ]; + + $response = $mastodon->uploadMedia($body); + + $file_id = $response->id; + + $statusText = 'This is a status'; + + $status_data = [ + 'status' => $statusText, + 'visibility' => $visibility, + 'language' => $language, + 'media_ids[]' => $file_id, + ]; + + $mastodon->postStatus($status_data); diff --git a/examples/reddit_bot.php b/examples/reddit_bot.php index 97aa3d6..2d1e889 100644 --- a/examples/reddit_bot.php +++ b/examples/reddit_bot.php @@ -1,77 +1,77 @@ $lastDate) && !$itemData["is_self"]) { - $post = new BlogPost(); - $post->link = $itemData["url"]; - $post->title = $itemData["title"]; - $post->category = $itemData["subreddit"]; - $post->nsfw = $itemData["over_18"]; - - array_push($statuses, $post); - if (!$first_article) { - $myfile = fopen($file, "w") or die("Unable to open file!"); - fwrite($myfile, $ts); - fclose($myfile); - $first_article = true; - } - } - } - - $statuses = array_reverse($statuses); - $mastodon = new MastodonAPI($token, $base_url); - - foreach ($statuses as $s) { - $status_data = array( - "visibility" => $visibility, - "language" => $language - ); - - $statusText = $s->title . $tagline . $s->link . " "; - - if ($s->nsfw) { - $status_data["status"] = "NSFW"; - $status_data["spoiler_text"] = $statusText; - } else { - $status_data["status"] = $statusText; - } - - $mastodon -> postStatus($status_data); - } - - - class BlogPost { - var $ts; - var $link; - var $category; - var $title; - var $nsfw; - } - -?> \ No newline at end of file + if ($f = fopen($file, 'r')) { + $lastDate = fgets($f); + fclose($f); + } + + $statuses = []; + + foreach ($json['data']['children'] as $item) { + $itemData = $item['data']; + $ts = $itemData['created_utc']; + if (($lastDate == '' || $ts > $lastDate) && !$itemData['is_self']) { + $post = new BlogPost(); + $post->link = $itemData['url']; + $post->title = $itemData['title']; + $post->category = $itemData['subreddit']; + $post->nsfw = $itemData['over_18']; + + array_push($statuses, $post); + if (!$first_article) { + $myfile = fopen($file, 'w') or die('Unable to open file!'); + fwrite($myfile, $ts); + fclose($myfile); + $first_article = true; + } + } + } + + $statuses = array_reverse($statuses); + $mastodon = new MastodonAPI($token, $base_url); + + foreach ($statuses as $s) { + $status_data = [ + 'visibility' => $visibility, + 'language' => $language, + ]; + + $statusText = $s->title.$tagline.$s->link.' '; + + if ($s->nsfw) { + $status_data['status'] = 'NSFW'; + $status_data['spoiler_text'] = $statusText; + } else { + $status_data['status'] = $statusText; + } + + $mastodon->postStatus($status_data); + } + + class BlogPost + { + public $ts; + public $link; + public $category; + public $title; + public $nsfw; + } diff --git a/examples/rss_bot.php b/examples/rss_bot.php index 2625fd2..6165583 100644 --- a/examples/rss_bot.php +++ b/examples/rss_bot.php @@ -1,78 +1,79 @@ channel->item as $item) { - $ts = strtotime($item->pubDate); - if ($lastDate == "" || $ts > $lastDate) { - $post = new BlogPost(); - $post->link = (string) $item->link; - $post->title = (string) $item->title; - - $post->category = $item->category; + if ($f = fopen($file, 'r')) { + $lastDate = fgets($f); + fclose($f); + } - array_push($statuses, $post); - if (!$first_article) { - $myfile = fopen($file, "w") or die("Unable to open file!"); - fwrite($myfile, $ts); - fclose($myfile); - $first_article = true; - } - } - } - - $statuses = array_reverse($statuses); - $mastodon = new MastodonAPI($token, $base_url); - - foreach ($statuses as $s) { - $statusText = $s->title . " leggi su " . $s->link . " "; - - foreach ($s->category as $c) { - $statusText = $statusText . "#" . formatHashTag($c) . " "; - } - - $status_data = array( - "status" => $statusText, - "visibility" => $visibility, - "language" => $language - ); - - $mastodon -> postStatus($status_data); - } - - - function formatHashTag($category) { - $filtered = str_replace('/', ' ', $category); - $upper = ucwords($filtered); - return str_replace(' ', '', $upper); - } - - - class BlogPost { - var $ts; - var $link; - var $category; - var $title; - } - -?> \ No newline at end of file + $statuses = []; + + foreach ($rss->channel->item as $item) { + $ts = strtotime($item->pubDate); + if ($lastDate == '' || $ts > $lastDate) { + $post = new BlogPost(); + $post->link = (string) $item->link; + $post->title = (string) $item->title; + + $post->category = $item->category; + + array_push($statuses, $post); + if (!$first_article) { + $myfile = fopen($file, 'w') or die('Unable to open file!'); + fwrite($myfile, $ts); + fclose($myfile); + $first_article = true; + } + } + } + + $statuses = array_reverse($statuses); + $mastodon = new MastodonAPI($token, $base_url); + + foreach ($statuses as $s) { + $statusText = $s->title.' leggi su '.$s->link.' '; + + foreach ($s->category as $c) { + $statusText = $statusText.'#'.formatHashTag($c).' '; + } + + $status_data = [ + 'status' => $statusText, + 'visibility' => $visibility, + 'language' => $language, + ]; + + $mastodon->postStatus($status_data); + } + + function formatHashTag($category) + { + $filtered = str_replace('/', ' ', $category); + $upper = ucwords($filtered); + + return str_replace(' ', '', $upper); + } + + class BlogPost + { + public $ts; + public $link; + public $category; + public $title; + }