Skip to content

Instantly share code, notes, and snippets.

@mxwright
Forked from jonathanbell/Instarss.md
Last active May 19, 2021 15:45
Show Gist options
  • Save mxwright/54af2703f19ae6f83949969c7b0b752c to your computer and use it in GitHub Desktop.
Save mxwright/54af2703f19ae6f83949969c7b0b752c to your computer and use it in GitHub Desktop.
Make a RSS feed from an Instagram user's username.
<?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]->graphql->user->edge_owner_to_timeline_media->edges) {
$edges = $data->entry_data->ProfilePage[0]->graphql->user->edge_owner_to_timeline_media->edges;
} 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'])) {
$edges = $data->entry_data->TagPage[0]->graphql->hashtag->edge_hashtag_to_media->edges;
}
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($edges as $nodes) {
$rss_feed .= '<item><title>';
if(isset($nodes->node->edge_media_to_caption->edges[0]->node->text) && $nodes->node->edge_media_to_caption->edges[0]->node->text != '') {
$rss_feed .= htmlspecialchars($nodes->node->edge_media_to_caption->edges[0]->node->text, 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/'.$nodes->node->shortcode.'/</link><pubDate>'.date("r", $nodes->node->taken_at_timestamp).'</pubDate>';
if (isset($_GET['user'])) {
$rss_feed .= '<dc:creator><![CDATA['.$_GET['user'].']]></dc:creator>';
}
$rss_feed .= '<description><![CDATA[<img src="'.$nodes->node->display_url.'" />]]></description><guid>https://instagram.com/p/'.$nodes->node->shortcode.'/</guid></item>';
} // foreach "node" (photo)
$rss_feed .= '</channel></rss>';
echo $rss_feed;
?>
@eprowe
Copy link

eprowe commented Sep 11, 2019

@mxwright No worries I think I wasn't 100% clear.

I use a self-hosted instance of Tiny Tiny RSS (TTRSS) for my RSS reader. Since it requires Apache and PHP to run I thought using Instarss would be a great way to follow Instagram users without using Instagram. I copied instarss.php to my server and add a RSS feed to TTRSS using dummydomain.dur/instarss.php?user=user as the feed URL. This was worked beautifully for the around 80 Instagram users I follow. In the past month or so this has stopped working.

To troubleshoot I removed the comment from line 25 (// echo $html;) from my server's copy of instarss.php to view the raw JSON data. In each case the data begins with: {"entry_data":{"LoginAndSignupPage":

Continuing to troubleshoot I went to instagram.com/user/ and did a view source. I copied the source to a text editor and performed the same data transformations that lines 19-22 of instarss.php would perform. When I did this the result was data that begins with: {"entry_data":{"ProfilePage": This is completely different than what is being returned by my server.

After some more research, and after submitting this comment, I believe the issue may be Instagram ramping up its fight against scrappers by limiting the number of requests allowed. So when TTRSS attempts to pull an update on this stack of "Instagram RSS Feeds" every hour Instagram basically rate-limits my server and starts presenting a different page. I've modified TTRSS to slow down the refresh rate on my Instagram feeds but I worry it may not be enough.

Ultimately, after my additional research, I don't think this is so much a instarss.php problem as it is a Instagram limitation. So for anyone else wanting to use instarss.php in a similar fashion be ready to be rate limited in a similar fashion.

@chanakyaindian
Copy link

Hi currently it is not working..some changes happened in the instagram last week i guess. Can you please take a look? Thanks

@6arshid
Copy link

6arshid commented Sep 4, 2020

Hello
This file in localhost for me working but in cpanel on server no working give me this error
Looks like this Instagram account is set to private or doesn't exist. We can't do much about that now, can we?

@pat-richter
Copy link

pat-richter commented Dec 7, 2020

Hello
This file in localhost for me working but in cpanel on server no working give me this error
Looks like this Instagram account is set to private or doesn't exist. We can't do much about that now, can we?

same for me. any suggestion? the file_get_contents() returns false

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