Skip to content

Instantly share code, notes, and snippets.

@rizqyhi
Created Jul 18, 2017
Embed
What would you like to do?
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