I am currently using a version of the following PHP code to automate the posting of content from World War II Database to the Mastodon social network. First, please note that you will need the following https://mastodon.social/ information that you can set up after logging on to your Mastodon account.
- Client ID
- Client Secret
- Access Token
There are two more things left to configure.
- URL to the RSS Feed
- File Path/Name for a Log File – This is used to avoid posting duplicate content on Mastodon
Below is the code. Note that in Step 2 there is a hard-coded value of “100” to note that the log file (to keep track of which RSS content has already been posted) will keep track of the most recent 100 records; Step 4 posts in the format of title-space-link, which you may wish to adjust based on your RSS feed structure.
$clientId = 'client_id_here'; $clientSecret = 'client_secret_here'; $accessToken = 'access_token_here'; $redirectUri = 'urn:ietf:wg:oauth:2.0:oob'; $rssUrl = 'rss_feed_url_here'; $postedIdsFile = 'rss_to_mastodon_posted_ids.txt'; // Change the following to "N" to output some debugging information $silentMode = "Y"; // Step 1: Establish a function to fetch RSS feed function fetchRss($rssUrl) { $rss = simplexml_load_file($rssUrl); return $rss; } // Step 2: Establish a function to check the log file to avoid duplicates function checkAndUpdatePostIds($postId, $file) { $postedIds = file_exists($file) ? file($file, FILE_IGNORE_NEW_LINES) : []; if (in_array($postId, $postedIds)) { return false; } array_push($postedIds, $postId); if (count($postedIds) > 100) { array_shift($postedIds); } file_put_contents($file, implode("\n", $postedIds) . "\n"); return true; } // Step 3: Establish a function to post to Mastodon function postToMastodon($accessToken, $content) { $url = "https://mastodon.social/api/v1/statuses"; $data = [ 'status' => $content, ]; $options = [ CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true, CURLOPT_POSTFIELDS => http_build_query($data), CURLOPT_HTTPHEADER => [ "Authorization: Bearer $accessToken", "Content-Type: application/x-www-form-urlencoded", ], ]; $ch = curl_init(); curl_setopt_array($ch, $options); $response = curl_exec($ch); curl_close($ch); return $response; } // Step 4: Put eveything together if (!$accessToken) { die("Error: Unable to obtain access token. Exiting..."); } $rss = fetchRss($rssUrl); foreach ($rss->channel->item as $item) { $postId = (string) $item->guid; $content = (string) $item->title . " " . (string) $item->link; if (checkAndUpdatePostIds($postId, $postedIdsFile)) { if ($silentMode == "N") { echo "Posting: $content\n"; } $response = postToMastodon($accessToken, $content); if ($silentMode == "N") { echo $response . "\n"; } } else { if ($silentMode == "N") { echo "Duplicate detected, skipping: $content\n"; } } }
As far as usage goes, you can refactor the various pieces to fit into your existing PHP-based management tool. As a shortcut, you can also take the above code as-is and run it via cron or other similar job schedulers.
My implementation of this code posts contents to the WW2DB Mastodon page at the URL https://mastodon.social/@ww2db.