Skip to content

Instantly share code, notes, and snippets.

@jonathanbell
Last active March 18, 2023 00:03
Show Gist options
  • Save jonathanbell/48c46d9fc2913fba3292 to your computer and use it in GitHub Desktop.
Save jonathanbell/48c46d9fc2913fba3292 to your computer and use it in GitHub Desktop.
August 28 2014 - InstaRss
{
"require": {
"php": "^7.0.0"
}
}
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"content-hash": "ba93918fe5641d588ab528e4bba94046",
"packages": [],
"packages-dev": [],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": {
"php": "^7.0.0"
},
"platform-dev": []
}

Instarss

I was looking around for a way to get a user's public Instagram feed as an RSS feed without using the Instagram API when I came across this question on Stack Overflow.

Probably the best way to do this, would be to use the Instagram API. However, I had no desire to sign up for an Instagram account.

Initially, this answer on Stack Overflow suited my needs quite well. However, as fate would have it, Instagram changed its HTML output and the page's JSON data structure changed. Since the idea is quite simple (we're just screen scrapping here) I decided to write my own script. If this works well for you, consider upvoting my answer on Stack Overflow: http://stackoverflow.com/a/25559442/1171790


Changelog

EDIT August 2017: I no longer support this scipt. The Instagram HTML output of any given user page changes often and it's been a fun game of cat and mouse but I no longer have a need for an Instagram user RSS. If someone else wants to submit a pull request to this Gist that supports the current Instagram output, I'd be happy to merge your changes. Thanks!

EDIT Septemeber 2017: I decided to continue supporting this script, for now. I got a couple emailed requests for support. I added changes and instructions to help people set it up on Heroku.

EDIT March 2018: This script will not be supported anymore but you get the idea - we are scraping the JSON object off of the page with PHP.

EDIT March 2023: Pull requests that work (tests or proof or some sort) will be accepted and merged. Cheers! 🥂


Example usage

Upload the script to your own server with PHP installed and then call the script via curl or in your web browser.

Getting a user's Instagram feed as an RSS feed: instarss.php?user=username

Getting an RSS feed of Instagram tags (aka hashtags): instarss.php?hashtag=carshow

Gotchas

Since your RSS reader will be checking for new content on some sort of interval and since Instagram provides the latest 20 photos/videos on a user's homepage, there is a potential for some content to be overlooked. In the event that more than 20 posts have been posted since the script last checked for new content, some content could be missed. Setting your RSS reader to check the instarss.php script often, will work around this. Alternatively, you can checkout this script which probably handles pagination.

Deploy Instarss on Heroku

If you'd like to use this script, it's probably easiest to set it up on a Heroku instance.

  1. First, clone this repository: git clone https://gist.github.com/48c46d9fc2913fba3292.git and then cd into the project directory.

  2. Login to Heroku and create a new app.

  3. And then login to Heroku via the command line: heroku login

  4. Then add Heroku as a remote: heroku git:remote -a your-projects-name

  5. git push heroku master

You can then access Instarss at a URL like: https://your-projects-name.herokuapp.com/instarss.php?user=snoopdogg

<?php
if (!isset($_GET['user'])) {
if (!isset($_GET['hashtag'])) {
exit('Not a valid RSS feed. You didn\'nt provide an Instagram user or hashtag. Send one via a GET variable. Example: mysite.com/instarss.php?user=snoopdogg');
}
}
if (isset($_GET['user']) && isset($_GET['hashtag'])) {
exit('Don\'t request both user and hashtag. Request one or the other.');
}
if (isset($_GET['user'])) {
$html = file_get_contents('http://instagram.com/'.$_GET['user'].'/');
}
if (isset($_GET['hashtag'])) {
$html = file_get_contents('http://instagram.com/explore/tags/'.$_GET['hashtag'].'/');
}
$html = strstr($html, '"entry_data');
$html = strstr($html, '</script>', true);
$html = substr($html, 0, -1);
$html = '{'.$html;
// For debugging... (like when Instagram changes its HTML page output).
// echo $html;
$data = json_decode($html);
// More debugging...
// print_r($data->entry_data->ProfilePage[0]->user->media->nodes);
// exit();
if (isset($_GET['user'])) {
if ($data->entry_data->ProfilePage[0]->user->media->nodes) {
$nodes = $data->entry_data->ProfilePage[0]->user->media->nodes;
} else {
exit('Looks like this Instagram account is set to private or doesn\'t exist. We can\'t do much about that now, can we?');
}
}
if (isset($_GET['hashtag'])) {
$nodes = $data->entry_data->TagPage[0]->tag->media->nodes;
}
header('Content-Type: text/xml; charset=utf-8');
$rss_feed = '<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel>';
if (isset($_GET['user'])) {
$rss_feed .= '<title>'.$_GET['user'].'\'s Instagram Feed</title><atom:link href="http://'.$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"].'" rel="self" type="application/rss+xml" /><link>http://instagram.com/'.$_GET['user'].'</link><description>'.$_GET['user'].'\'s Instagram Feed</description>';
}
if (isset($_GET['hashtag'])) {
$rss_feed .= '<title>Photos tagged with: '.$_GET['hashtag'].' on Instagram</title><atom:link href="http://'.$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"].'" rel="self" type="application/rss+xml" /><link>http://instagram.com/explore/tags/'.$_GET['hashtag'].'</link><description>Photos tagged with: '.$_GET['hashtag'].' on Instagram</description>';
}
foreach($nodes as $node) {
$rss_feed .= '<item><title>';
if (isset($node->caption) && $node->caption != '') {
$rss_feed .= htmlspecialchars($node->caption, ENT_QUOTES);
} else {
$rss_feed .= 'photo';
}
// <pubDate> format could also be: "D, d M Y H:i:s T"
$rss_feed .= '</title><link>https://instagram.com/p/'.$node->code.'/</link><pubDate>'.date("r", $node->date).'</pubDate>';
if (isset($_GET['user'])) {
$rss_feed .= '<dc:creator><![CDATA['.$_GET['user'].']]></dc:creator>';
}
$rss_feed .= '<description><![CDATA[<img src="'.$node->display_src.'" />]]></description><guid>https://instagram.com/p/'.$node->code.'/</guid></item>';
} // foreach "node" (aka photo)
$rss_feed .= '</channel></rss>';
echo $rss_feed;
web: vendor/bin/heroku-php-apache2
@jonathanbell
Copy link
Author

@kissuidonet If instagram now has ?__a=1 and that serves up json it's trivial to get that and convert it to a PHP array which we can then use to create the XML RSS feed like we were doing before.

@kamaleao
Copy link

kamaleao commented Dec 3, 2019

Can anyone get this code works with /? __ a =? https://browse-tutorials.com/snippet/php-instagram-rss-feed

@ralphbourji
Copy link

hello jonathan,

is there any updated version that works with the latest instagram changes?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment