Skip to content

Instantly share code, notes, and snippets.

@Nyholm
Last active October 26, 2019 17:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nyholm/b72a3a5b799a8daf8544e1ab0b1bc8e9 to your computer and use it in GitHub Desktop.
Save Nyholm/b72a3a5b799a8daf8544e1ab0b1bc8e9 to your computer and use it in GitHub Desktop.
Get instagram images 2018-10-16
function getInstagramImages($username, $limit = 5) {
$url = 'https://www.instagram.com/'.$username;
$response = file_get_contents($url);
if (!preg_match('|<script type="text\/javascript">window\._sharedData = (.*?);<\/script>|sim', $response, $matches)) {
return [];
}
$json = json_decode($matches[1], true);
$edges = $json['entry_data']['ProfilePage'][0]['graphql']['user']['edge_owner_to_timeline_media']['edges'];
$images = [];
for($i = 0; $i<$limit; $i++) {
if (isset($edges[$i])) {
$images['https://www.instagram.com/p/'.$edges[$i]['node']['shortcode']] = $edges[$i]['node']['thumbnail_resources'][0]['src'];
}
}
return $images;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment