Skip to content

Instantly share code, notes, and snippets.

@jonathanbell
Last active March 18, 2023 00:03
Show Gist options
  • Star 26 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • 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
@mxwright
Copy link

I agree, this a problematic moving target, but I've got a client that requires me to keep it going. Here's a fork that should work for now:
https://gist.github.com/mxwright/54af2703f19ae6f83949969c7b0b752c

I don't use the Gist in the same way as written, but the changes (noted above) should keep it working.

@jonathanbell
Copy link
Author

@mxwright Thanks! I added your fork as an upstream branch and merged your changes into this Gist.

@mimi89999
Copy link

@jonathanbell Under what license is that php code? Is it public domain?

@BMRG14
Copy link

BMRG14 commented Feb 16, 2017

Dude, you are one great person!
I hate how Instagram changes everything and makes them worse day by day!
Thank you very much for this code

@jonathanbell
Copy link
Author

@mimi89999 Licence = WTFPL (http://www.wtfpl.net/)

@pAyDaAr
Copy link

pAyDaAr commented Mar 1, 2017

hi @jonathanbell
again it doesnt show image URL.

@jonathanbell
Copy link
Author

More than likely, Instagram has changed their output again. If you'd like to make edits to a fork, I will pull them into this gist.

@sandeepkgupta
Copy link

sandeepkgupta commented Mar 7, 2017

Yes, pattern match in line 72 ?ig_cache_key= no longer there in img src. So image path not matched and not picked up.

@mimi89999
Copy link

@jonathanbell

More than likely, Instagram has changed their output again. If you'd like to make edits to a fork, I will pull them into this gist.

You only need to revert the last commit.

@jonathanbell
Copy link
Author

@mimi89999 It's been almost 3 years since we started chasing the Instagram output. I appreciate your comment but I think I will stop updating this Gist. Hopefully, people can reason about the code and work it into their own project(s).

@jonathanbell
Copy link
Author

Lots of interest expressed over email so this gist is working again. See installation instructions if you'd like to install on Heroku.

@mxwright
Copy link

oh man they really jacked it up this time

@mxwright
Copy link

gotta use something like:
$data->entry_data->ProfilePage[0]->graphql->user->edge_owner_to_timeline_media->edges as $e

@jonathanbell
Copy link
Author

I'm so tired! 😢

@kissuidotnet
Copy link

i was trying to get this to work..
instagram now has instgram.com/{user}/?__a=1
but it looks like you need to send headers and allow to set cookies.. and i dont think you can do this with file_get_contents

@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