Skip to content

Instantly share code, notes, and snippets.

@csilverman
Created April 25, 2019 15:33
Show Gist options
  • Save csilverman/fd87ea44e16bbbc851f96b97b904b1ba to your computer and use it in GitHub Desktop.
Save csilverman/fd87ea44e16bbbc851f96b97b904b1ba to your computer and use it in GitHub Desktop.
<?php
/* RSS2INSTA
=========
This scans a folder of images with specially formatted captions, and generates a generic RSS feed. You can then plug this feed into a Zapier workflow and have it send the images (with tags) to Buffer, which will post them to Instagram.
The caption format allows you to specify a plain-language caption as well as whatever hashtags you want to have. The different parts of the caption are delimited by %%, since that seems unlikely to occur in normal text.
Captions should be formatted as follows: [ID]%%[caption text]%%[image tags].jpg.
- ID: a unique identifier (could be a date or image number)
- Caption: the caption for the image.
- Tags: A space-delimited list of tags. Don't include the # - this will be added automatically.
Setup:
1. Create a folder for this script on a remote server (needs to be public so a Zapier workflow can find it)
2. Add this script. Name it whatever you want.
3. In the script, set $public_url to the URL for the folder you just created (the folder, not the script)
4. In the folder with the script, create a new folder, called "images".
Usage:
- Upload images to the images folder
- Call this script. It will generate a 'insta_images.xml' file in the main folder.
- That's your RSS feed. Share it with whoever!
*/
/* CONFIG AND TEMPLATES */
$public_url = "https://csilverman.com/YOUR_URL_HERE/";
$rss_header = <<<EOT
<?xml version="1.0" ?>
<rss version="2.0">
<channel>
<title>Instagram</title>
<link>https://www.csilverman.com/</link>
<description>Instagram posts</description>
EOT;
$rss_footer = <<<EOT
</channel>
</rss>
EOT;
/* FUNCTIONS */
function format_caption($caption) {
// First, remove the file extension.
$caption = str_ireplace('.jpg', '', $caption);
$caption = str_ireplace('.png', '', $caption);
// I'm using %% as a delimiter, since periods could be used in a caption
$caption = explode('%%', $caption);
// The first part of the caption is just the ID. We'll ignore that.
// The second part is the main caption. We need that.
$caption_text = $caption[1];
// The third part of the caption is the tags.
$caption_tags = $caption[2];
$caption_tags = explode(' ', $caption_tags);
foreach ($caption_tags as &$tag) {
$final_tags .= ' #'.$tag;
}
$final_caption = $caption_text.'\n\n'.$final_tags;
return $final_caption;
}
/* ACTION */
// Scan the images folder
$dir = getcwd()."/images";
$scanned_directory = array_diff(scandir($dir), array('..', '.'));
// For each file in the images directory, grab the filename
// and construct the info we need.
foreach ($scanned_directory as &$value) {
$guid = $value;
$img_url = $public_url.'images/'.$value;
$caption = format_caption($value);
$rss_item = <<<EOT
<item>
<title>$guid</title>
<link></link>
<description>$caption</description>
<guid>$guid</guid>
<image>
<url>$img_url</url>
</image>
</item>
EOT;
$all_items .= $rss_item;
}
$ig_final_rss = $rss_header.$all_items.$rss_footer;
file_put_contents(getcwd()."/insta_images.xml", $ig_final_rss);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment