Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@rizqyhi
Created July 18, 2017 04:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rizqyhi/69869fbf05f95af7859d8a47a694f06b to your computer and use it in GitHub Desktop.
Save rizqyhi/69869fbf05f95af7859d8a47a694f06b to your computer and use it in GitHub Desktop.
Simple instagram media grabber. Result is cached. Output as JSON.
<?php
$username = 'sadarkawasan';
$cache_time = 60 * 60 * 2; // seconds
$media = "";
if (file_exists('igfeed.json') && (filemtime('igfeed.json') > (time() - $cache_time ))) {
$media = file_get_contents('igfeed.json');
} else {
$html = file_get_contents('https://www.instagram.com/'.$username);
preg_match('/"nodes": (.+), "count/', $html, $result);
$media = array_map(function ($node) {
return [
'id' => $node->id,
'code' => $node->code,
'date' => $node->date,
'thumbnail' => $node->thumbnail_src,
'url' => "https://www.instagram.com/p/{$node->code}/"
];
}, json_decode($result[1]));
$media = array_slice($media, 0, 6);
$media = json_encode($media);
file_put_contents('igfeed.json', $media);
}
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
echo $media;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment