17

I was wondering how ink361 was creating an Instagram RSS feed from a username.

Example feed: http://ink361.com/feed/user/snoopdogg

Blog post: http://blog.ink361.com/post/23664609916/new-rss-instagram-feed-feature-on-ink361-com

Any insight would be appreciated.

Thanks.

4
  • I see that the ink361 user's RSS feed is no longer working. Aug 28, 2014 at 15:55
  • According to the new TOS you/we won't be able to read from users feed and probably from keywords.
    – PJunior
    Nov 19, 2015 at 1:47
  • browse-tutorials.com/snippet/php-instagram-rss-feed this one leverages paging. Thus enables you to control the items count.
    – ram4nd
    Aug 8, 2017 at 7:30
  • It looks like now in 2018 they are doing away with the api and fetching data by rss feed is not doable? Always changing stuff, drives one mad.
    – drooh
    Apr 26, 2018 at 21:20

8 Answers 8

11

Instagram has a publicly accessible RSS API, it's hard to find any information about it, but it works for tags (we do use it).

For tags the syntax is the following:

http://instagr.am/tags/some-tag-you-want-to-follow/feed/recent.rss

I'm not sure whether they have something similar for users' feeds, as I've said it's really hard to find information about it and it may disappear from a day to another in favor of the official API, but right now it works for tags.

Here's an official blog post about it (although it covers only tags): http://blog.instagram.com/post/8755963247/introducing-hashtags-on-instagram

3
  • Hi Viktor. Do you still use the RSS feed? I'd be curious/interested in your use case (is there a link?). Thanks!
    – Paulo
    Aug 8, 2014 at 16:20
  • @Paulo no, not anymore. We used it for an iOS app I was working on at the time. Aug 8, 2014 at 19:01
  • 12
    As of very recently this feed has stopped working :( I've used it on a couple of projects, I have a feeling they'd just forgotten about it! They must have noticed it… Sep 27, 2014 at 17:15
5

@user2543857 's answer was good. Unfortunately, the structure of the Instagram data has changed. As of the date of posting this, this will work. Copy/paste into a file on your PHP server and use like: yoursite.com/instarss.php?user=name_of_instagram_user This will return valid XML/RSS feed.

EDIT!! Naturally, the output of the page/JSON has changed with instagram's new look/feel. Here is updated code (June, 2015):

<?php

if (!isset($_GET['user'])) {
    exit('Not a valid RSS feed. You didn\'nt provide an Instagram user. Send one via a GET variable. Example .../instarss.php?user=snoopdogg');
}

header('Content-Type: text/xml; charset=utf-8');

$html = file_get_contents('http://instagram.com/'.$_GET['user'].'/');
$html = strstr($html, '{"static_root');
$html = strstr($html, '</script>', true);
//$html = substr($html,0,-6);
$html = substr($html, 0, -1);

$data = json_decode($html);

// print_r($data->entry_data->ProfilePage[0]->user->media->nodes);

$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>';
$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>';

foreach($data->entry_data->ProfilePage[0]->user->media->nodes as $node) {

    $rss_feed .= '<item><title>';

    if(isset($node->caption) && $node->caption != '') {
        $rss_feed .= htmlspecialchars($node->caption, ENT_QUOTES, ENT_HTML5);
    } 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><dc:creator><![CDATA['.$_GET['user'].']]></dc:creator><description><![CDATA[<img src="'.$node->display_src.'" />]]></description><guid>https://instagram.com/p/'.$node->code.'/</guid></item>';

} // foreach "node" (photo)

$rss_feed .= '</channel></rss>';

echo $rss_feed;
?>

Actually, don't use the above code. I'll try to maintain this Gist in the future.

EDIT December 2016: I'm tired of chasing the every-changing Instagram output only to screen scrape it and have it change a few months later. I'd say just use the API.. If you are still interested in making an RSS feed from a user's page, this Gist should give you an idea of how to do it.

3
  • 1
    Thanks for the code. Just a note, with PHP 5.5 I was getting an error caused by htmlspecialchars and had to change ENT_HTML5 to 'utf-8' then it started to work as charm.
    – MPękalski
    May 28, 2015 at 13:58
  • Awesome work, and great that you have it working with tags aswell. Thanks
    – marca
    Jun 8, 2016 at 19:56
  • @marca thanks! Just remember with tags, you'll have to query the RSS very often as some tags are used quite frequently and the data will update quickly. This way, the script won't miss posts! Jun 9, 2016 at 19:01
4

You can access the feed for any Instagram user using the /users/user-id/media/recent API endpoint. This endpoint requires an access_token which you can obtain by authorizing some user with Instagram (not necessarily the one you request the feed for). The process for receiving access_token is described here.

So what ink361 may be doing is get an access_token for themselves (their user on Instagram) and use that to make /users/user-id/media/recent requests for any other users' feeds. Simple as that.

2
  • 1
    This endpoint is basically the same, but requires only a client ID. Jan 22, 2014 at 0:09
  • To clarify: this will provide access to Instagram API resource which will be a JSON document, not to the RSS feed.
    – raindev
    Dec 5, 2016 at 16:59
4

Thanks to torvin for the tip.

Here is how you can get instagram images on your site without using the API.

Create json file from url and username (set this as a cron job, X times per day)

<?
$html = file_get_contents('http://instagram.com/username/');
$html = strstr($html, '["lib');
$html = strstr($html, '</script>', true);
$html = substr($html,0,-6);
file_put_contents("username.json",$html);
?>

Display a few images from json feed

<?
$json = file_get_contents('username.json');
$data = json_decode($json);

$img1 = $data[2][0]->props->userMedia[0]->images->standard_resolution->url;
$img2 = $data[2][0]->props->userMedia[1]->images->standard_resolution->url;
$img3 = $data[2][0]->props->userMedia[2]->images->standard_resolution->url;

print '<img src="'.$img1.'" />';
print '<img src="'.$img2.'" />';
print '<img src="'.$img3.'" />';
?>
3
  • 3
    Nice technique. I take it this has the chance of failing at some point in the future if Instagram change how the JSON is formatted or change how the page works.
    – Jeff S.
    Aug 5, 2013 at 0:49
  • Nice technique. The output has changed, but it's easy to figure out and apply this again to the screen scrape. :) CHEERS! Aug 28, 2014 at 16:42
  • any idea how to do this in 2018?
    – drooh
    Apr 26, 2018 at 21:08
2

If I were ink361, I would just crawl Instagram pages, parse HTML and turn it into RSS. No API, no authorization, no problems.

2
  • 1
    The problem is, Instagram is entirely generated by javascript. Which seems pretty crazy - especially for a site that's aimed at mobile users - but makes crawling it as you suggest much more difficult.
    – Nick F
    Jun 21, 2013 at 17:18
  • 4
    In case of Instagram that makes the the job actually easier! Look at the source code of instagram.com/snoopdogg, the line that starts with ["lib\/fullpage\/transitions"... is just pure JSON. So, you don't even need to parse HTML at all! Just use any JSON parser and boom! - data for your RSS feed is ready
    – torvin
    Jun 26, 2013 at 8:49
2

Unfortunately user2543857's solution above doesn't work anymore. Here's a version that works with the current profile page source though.

Create JSON file from URL and username (set this as a cron job, X times per day)

<?php
    $json = file_get_contents('http://instagram.com/username');
    $json = strstr($json, '{"entry_data"');
    $json = strstr($json, '</script>', true);
    $json = rtrim($json,';');

    file_put_contents("username.json",$json);
?>

Display a few images from JSON feed

<?php
    $json = file_get_contents('username.json');
    $data = json_decode($json,true);

    $img1 = $data['entry_data']['UserProfile'][0]['userMedia'][0]['images']['thumbnail']['url'];
    $img2 = $data['entry_data']['UserProfile'][0]['userMedia'][1]['images']['thumbnail']['url'];
    $img3 = $data['entry_data']['UserProfile'][0]['userMedia'][2]['images']['thumbnail']['url'];

    print '<img src="'.$img1.'" />';
    print '<img src="'.$img2.'" />';
    print '<img src="'.$img3.'" />';
?>
1
  • Replace "thumbnail" with "standard_resolution" for the full size 640 x 640 or "low_resolution" for a 306 x 306 image.
    – Trae
    Jan 23, 2014 at 2:59
-1

You can get access to your rss feed for instagram by using their API. Their API uses oAuth2 for authentication. I use this method on my personal blog to pull in instagram pics on the homepage. I suspect this is how the ink361 site works. Ink361 will connect to the instagram api and prompt the user with a login via instagram box which they use to allow ink361 access to their instagram account. The ink361 site will cache the token received by instagram once authentication is successfull so they then can repeatedly go back to the instagram api on a periodic basis using the same token for authentication. Bingo you have access to the users data and you can create an rss feed from it.

1
  • Hi Matthew, thanks for the answer. The thing with Ink361, is that they don't use oAuth. I have not approved the ink361 app (& I am not snoop dogg ;) but I am still able to get snoop dogg's RSS: ink361.com/feed/user/snoopdogg
    – johnnyknox
    Feb 15, 2013 at 0:18
-1

The answer is simple.
To access user data you just must have valid access token.
The ink361 has an app in social network http://vk.com/app3225087 which stores authenticated users access tokens in db.
Its only left to find in db a valid one and get whatever user data you want

Not the answer you're looking for? Browse other questions tagged or ask your own question.