Last active
October 26, 2019 17:19
-
-
Save Nyholm/b72a3a5b799a8daf8544e1ab0b1bc8e9 to your computer and use it in GitHub Desktop.
Get instagram images 2018-10-16
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
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