Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Make a RSS feed from an Instagram user's username. If this works well for you, please upvote the answer here: http://stackoverflow.com/a/25559442/1171790

instarss.php

Get a user's public Instagram feed as an RSS feed without using the Instagram API.

Example usage

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

Getting a RSS feed of tags: 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, 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 script often, will work around this.

<?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 .../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... sigh........
// echo $html;
$data = json_decode($html);
// more debugging...
// print_r($data->entry_data->ProfilePage[0]->user->media->nodes);
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="'.strstr($node->display_src, "?ig_cache_key=", true).'" />]]></description><guid>https://instagram.com/p/'.$node->code.'/</guid></item>';
} // foreach "node" (photo)
$rss_feed .= '</channel></rss>';
echo $rss_feed;
?>
Owner

Works now (August 2014) until Instagram changes up its json format (again).
Usage: http://yourserver.com/instarss.php?user=snoopdogg

Interesting. Will test and see if this works. I'll try if not looks like I'll have to sign up to Instagram and get their API.

Owner

Updated as the output from instagram was changed (naturally...).

adarwash commented Jul 9, 2015

This is really handy, thanks :)

Not working now :(

it works with any other users but not with emzstuff

@jonathanbell Your demo has stopped to working too.

Owner
jonathanbell commented Oct 22, 2015 edited

@kamaleao Updated. The Instagram JSON object changed a little. Should work now. Check gist revision history for changes. Cheers!

@jonathanbell Very good! Is there any way to make this code compatible with PHP Version 5.2.17

@kamaleao To make compatible with PHP 5.2, just replace line 28 with:

$rss_feed .= htmlspecialchars($node->caption, ENT_QUOTES);

Not working now :(

@kamaleao try replacing line 10

$html = strstr($html, '{"qs');

with this:

$html = strstr($html, '{"country_code');
Owner

@mxwright, thanks for the fix! This is starting to become a bit of a moving target with Instagram updating the output of their pages more often than we'd like. I'm starting to get tired of maintaining this. You guys get the idea though, we are basically screen scraping a JSON object on the page into PHP and making the RSS (XML) feed.

Kinda annoying how GitHub Gists don't notify me about comments. Anybody know about that?

I made the suggested changes to the code above. Thanks all.

If people don't stop hotlinking to the demo, I'm going to have to take it down. Please don't do that! Host the file on your own server, please.

xdmedia commented Feb 5, 2016

Is it outdated again? My file doesn't work anymore since yesterday, but your hosted demo file does.

Owner

@xdmedia Make sure your host didn't change your version of PHP?? And make sure that the Instagram user's account is not private.

Awesome work on this Jonathan, thanks so much! Might it possible to make this script work for #hashtags too? Like

https://www.instagram.com/explore/tags/carshow/

Owner
jonathanbell commented Mar 31, 2016 edited

@BCwhoLikesPHP See my recent edit. I think that'll work for ya.
http://yourserver.com/instarss/instarss.php?hashtag=carshow

mboveiri commented Jul 17, 2016 edited

Hi.
how to remove "?ig_cache_key=*" from the source pics.?
thanks.

Hi.
how to remove "?ig_cache_key=*" from the source pics.?
thanks.

thanks to blitterobject reddit user for resolve this problem.

Owner

@mboveiri just curious, why did you want to remove that param?

hobailey commented Sep 9, 2016

@jonathanbell - because it's untidy, and not actually necessary?
@mboveiri - I'm doing it with a simple strstr($i["display_src"], "?ig_cache_key=", true)

Owner
jonathanbell commented Sep 15, 2016 edited

@hobailey Interesting, when I first wrote the script, I couldn't view/call the images in browser without it. You're right though, it seems to work just fine now without the token.

pAyDaAr commented Nov 25, 2016 edited

@jonathanbell hi there
does it support videos?

Owner

@pAyDar currently it will send the thumbnail of the video but not the video itself.

Did Instagram change something? It doesn't work anymore

mxwright commented Dec 16, 2016 edited

I don't have a copy of the previous Instagram JSON, and I'm no JSON expert, but it seems like they've removed a parent element necessary for this script. I get around this now by restricting the array to just the more or less static elements and adding a parent open tag:

$html = strstr($html, '"entry_data');
$html = strstr($html, '</script>', true);

$html = substr($html, 0, -1);
$html = '{' . $html;

Owner

I'm tired of chasing Instagram. I won't be making changes to this Gist any longer. If your fork of this Gist works with Instagram's updated JSON output, copy/paste a link to your fork here.

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.

Owner

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

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

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

Owner

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

pAyDaAr commented Mar 1, 2017

hi @jonathanbell
again it doesnt show image URL.

Owner

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 commented Mar 7, 2017 edited

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

@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.

Owner

@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).

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