13260815_1733242290220838_107033783_n.jpg

As of this month (June 2016), the vast majority of third-party apps and websites that use the Instagram API will no longer function, thanks to much stricter access rules. Thanks for breaking everything Instagram, much appreciated! Anyway, if you’d like to continue to use Instagram content on your site, here’s some simple PHP code that will scrape photos from the Instagram website for you:

To display content for a specified hashtag (in this example, the hashtag #biscuits:

<?php

// Change this URL to pull in top posts for a specific tag, in this case #biscuits

$url = “https://www.instagram.com/explore/tags/biscuits/“;

$instagram_content = file_get_contents($url);
preg_match_all(‘/window._sharedData = (.*)\;\<\/script\>/’, $instagram_content, $matches);
$txt = implode(”,$matches[1]);
$json = json_decode($txt);

foreach ($json->entry_data->TagPage{0}->tag->media->nodes AS $item) {
echo “<img src='”.$item->display_src.”‘>”;
}

?>

To display content for a specified user (in this example, the user ’soul grenades’:

<?php

// Change this URL to pull in top posts for a specific user, in this case the user ‘soulgrenades’

$url = “https://www.instagram.com/soulgrenades”;

$instagram_content = file_get_contents($url);
preg_match_all(‘/window._sharedData = (.*)\;\<\/script\>/’, $instagram_content, $matches);
$txt = implode(”,$matches[1]);
$json = json_decode($txt);

foreach ($json->entry_data->TagPage{0}->tag->media->nodes AS $item) {
echo “<img src='”.$item->display_src.”‘>”;
}

?>

This code has no worthwhile error checking going on at this point, it’s just a quick-and-dirty solution, but it’s very easily expanded upon (something we’ll be doing in due course). It doesn’t support video yet, but we’ll add that over the coming days.

UPDATE 06.06.2016

We’ve had some feedback suggesting this is quite useful, so here an updated version, with clickthroughs to the Instagram post and some rudimentary styling:

Hashtag demo: http://host.include-digital.com/apps/instagram/hashtag

User demo: http://host.include-digital.com/apps/instagram/user

The source code is the same as above with the exception of the “echo” line, which should change to:

echo “<a class=’instagramImage’ href=’http://instagram.com/p/”.$item->code.”‘ target=’_blank’ style=’background-image: url(\””.$item->display_src.”\”);’><div class=’instagramInfo’><p>”.$item->caption.”</p></div></a>”;

The simple CSS used to style this is can be found at:

http://host.include-digital.com/apps/instagram/instagram.css

Please let us know if you find this even vaguely useful, get in touch at @includedigital.

Related Posts