Puck's Random Website

I tried letting PHP grab my Instagram feed

A friend of mine is making art, and I'm trying to make a website for her. To make updating her website easier for her, I want to make that website automatically import photos from her Instagram feed.

To try out the PHP code, I'm first trying to let it grab photos from my own Instagram profile because although I don't really post much on it, I still post more than she does (she currently has nothing on it).

As all my fellow computer nerds know: coding is mostly just googling for StackOverflow questions and -answers. The first result seemed quite obvious and was what I wanted to try. Sadly, as already mentioned in one of the replies, it no longer works because Instagram detects that PHP file_get_contents is used instead of a browser. I had hoped that Instagram achieves this by looking at the user agent string, and decided to spoof it to a fairly common UA of the Google Chrome browser. So this is what my code looked like:

$options  = array('http' => array('user_agent' => 'Mozilla/5.0 (X11; Linux x86_64 13816.55.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.86 Safari/537.36'));
$context  = stream_context_create($options);
$instaResult = file_get_contents('https://www.instagram.com/puikepuck/?__a=1',false,$context);
$insta = json_decode($instaResult);

Sadly, Instagram isn't that easy to fool. While the URL https://www.instagram.com/puikepuck/?__a=1 gives a feed-like output in a web browser, it outputs zero when called in PHP. The cause appeared quite simple: when opening the aforementioned URL in a private browser window, it won't work either and I'm being redirected to a login page. There goes my plan B: letting curl download the feed and then load it into PHP. Not an option now.

Yet there are ready-made plugins that can do exactly what I want. They all have disadvantages: they have their own layout that I don't like, they require an account or they cost money. But apparently, what I want is possible: you can grab photos from Instagram.

After a bit of googling, I discovered that yes, it is, but it is very complicated. And I don't mean complicated as in it needs a lot of code, as that would basically mean it would simply be a lot of work. No, I mean complicated as in Meta (or whatever Facebook calls itself these days) has made it as difficult as they can. Apparently I would need an access token. Not only that, but I would first need a token that expires after an hour, which I can then exchange for a token that expires after 60 days. The token can be refreshed within those 60 days, but there is no way to obtain a token with a longer or permanent validity.

Of course I could automate the token refresh. In fact, Meta already gives a suggestion to use curl for that. However, that does draw some possible complications: either I could put it in the PHP code, making PHP call for curl each time the website is loaded, but this means my friend's website needs to be visited at least once every 60 days but no more often than once every 24 hours (a token cannot be refreshed if it is less than 24 hours old; I can of course add a timestamp to the last refresh date to make it not be refreshed sooner but it's mostly the 60-days part that worries me, after all if the website isn't visited for 61 days, it will break). A better way is to make a cronjob that automatically refreshes the token every 59 days, but this requires my non-tech-savvy friend to have shell-access to her webserver and know how to work with cron. Which, yes, I can explain to her (if she even has shell access), but that seems more work than finding a solution that doesn't require a token.

It turns out, there is a way indeed. After a long search, I found this. And this is only complicated in that it's a long way of setting up. And since of course I only started doing this in the evening instead of much earlier in the day when I had plenty of time, I will try this out tomorrow. It looks fairly straightforward though, so I have little doubt it will work. But I will set it up tomorrow. Perhaps I'll update this post to show you how it works, or in case it doesn't, then why not. But for now I'm going to bed. ;)

Last modified: 23 December 2021 23:27:59.
Links PhotosHome Instagram Facebook LinkedInAll blog posts