Created
July 18, 2017 04:45
-
-
Save rizqyhi/69869fbf05f95af7859d8a47a694f06b to your computer and use it in GitHub Desktop.
Simple instagram media grabber. Result is cached. Output as JSON.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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