I am currently using a version of the following PHP code to automate the posting of content from World War II Database to the Reddit social network. First, please note that you will need the following Reddit information that you can set up after logging on to your Reddit account. You will also need to establish an app at https://www.reddit.com/prefs/apps, and the User Agent name you will set up will also be part of the configuration. And of course, you will need to enter the name of your own subreddit to post to; if you are posting to someone else’s subreddit, be sure to obey that particular community’s rules.
- Client ID
- Client Secret
- Username
- Password
- User Agent Name
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 Reddit
Below is the code. Note that in Step 3 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; Steps 4 and 5 makes the posts in the format of a “link” type Reddit post, which you may wish to adjust based on your needs.
$clientId = 'client_id_here'; $clientSecret = 'client_secret_here'; $username = 'username_here'; $password = 'password_here'; $userAgent = 'user_agent_here'; $rssUrl = 'rss_feed_url_here'; $subreddit = 'r/subreddit_name'; $postedIdsFile = 'rss_to_reddit_posted_ids.txt'; $silentMode = "Y"; // Step 1: Establish a function to fetch an access token function getAccessToken($clientId, $clientSecret, $username, $password, $userAgent) { $url = 'https://www.reddit.com/api/v1/access_token'; $postFields = http_build_query([ 'grant_type' => 'password', 'username' => $username, 'password' => $password ]); $options = [ CURLOPT_URL => $url, CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_USERPWD => "$clientId:$clientSecret", CURLOPT_POSTFIELDS => $postFields, CURLOPT_HTTPHEADER => ["User-Agent: $userAgent"] ]; $ch = curl_init(); curl_setopt_array($ch, $options); $response = curl_exec($ch); curl_close($ch); $response_data = json_decode($response, true); return $response_data['access_token'] ?? null; } // Step 2: Establish a function to fetch content from the RSS feed function fetchRss($rssUrl) { return simplexml_load_file($rssUrl); } // Step 3: Establish a function to track and avoid duplicate posts 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); // Keep the file small } file_put_contents($file, implode("\n", $postedIds) . "\n"); return true; } // Step 4: Establish a function to post to Reddit function postToReddit($accessToken, $subreddit, $title, $url, $userAgent) { $apiUrl = "https://oauth.reddit.com/api/submit"; $postData = [ 'sr' => $subreddit, 'title' => $title, 'url' => $url, 'kind' => 'link' ]; $options = [ CURLOPT_URL => $apiUrl, CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_POSTFIELDS => http_build_query($postData), CURLOPT_HTTPHEADER => [ "Authorization: Bearer $accessToken", "User-Agent: $userAgent", "Content-Type: application/x-www-form-urlencoded" ] ]; $ch = curl_init(); curl_setopt_array($ch, $options); $response = curl_exec($ch); curl_close($ch); return json_decode($response, true); } // Step 5: Put it all together $accessToken = getAccessToken($clientId, $clientSecret, $username, $password, $userAgent); if (!$accessToken) { die("Error: Unable to obtain access token. Exiting...\n"); } $rss = fetchRss($rssUrl); if (!$rss) { die("Error: Unable to fetch RSS feed. Exiting...\n"); } foreach ($rss->channel->item as $item) { $postId = (string) $item->guid; $title = (string) $item->title; $url = (string) $item->link; if (checkAndUpdatePostIds($postId, $postedIdsFile)) { if ($silentMode == "N") { echo "Posting to Reddit: $title ($url)\n"; } $response = postToReddit($accessToken, $subreddit, $title, $url, $userAgent); if ($silentMode == "N") { echo "Reddit Response: " . json_encode($response) . "\n"; } } else { if ($silentMode == "N") { echo "Duplicate detected, skipping: $title ($url)\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 subreddit at the URL https://www.reddit.com/r/ww2database/.