Last active
February 2, 2021 09:34
-
-
Save jackmcdade/ff8bf61fb57db5e74edd8a7b9b5a2b26 to your computer and use it in GitHub Desktop.
Instagrizzle Scraper
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
// Quick and dirty Instagram scraper | |
private function scrape($username) | |
{ | |
$source = file_get_contents('http://instagram.com/' . $username); | |
$shards = explode('window._sharedData = ', $source); | |
$json_response = explode(';</script>', $shards[1]); | |
$response_array = json_decode($json_response[0], TRUE); | |
$nodes = array_get($response_array, 'entry_data:ProfilePage:0:user:media:nodes'); | |
$data = array(); | |
foreach ($nodes as $node) { | |
$url = 'https://instagram.com/p/' . $node['code']; | |
$image = $node['display_src']; | |
$data[] = $node + [ | |
'url' => $url, | |
'link' => $url, | |
'image' => $image, | |
'thumbnail' => $node['thumbnail_src'], | |
'images' => [ | |
'high_resolution' => ['url' => $image], | |
'low_resolution' => ['url' => $image] | |
] | |
]; | |
} | |
return $data; | |
} | |
function array_get($array, $key, $default = null) | |
{ | |
if (is_null($key)) return $array; | |
if (isset($array[$key])) return $array[$key]; | |
foreach (explode('.', $key) as $segment) | |
{ | |
if ( ! is_array($array) || ! array_key_exists($segment, $array)) | |
{ | |
return value($default); | |
} | |
$array = $array[$segment]; | |
} | |
return $array; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment