Last active
October 19, 2022 14:24
-
-
Save shrimp2t/cb1f1c123caccdff66d0d69cf86da9e7 to your computer and use it in GitHub Desktop.
Get instagram images without API
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 | |
/** | |
* @see http://stackoverflow.com/questions/40836144/php-how-to-get-images-from-instagram-without-api-access-token | |
* You can use function file_get_conents instead wp_remote_get | |
*/ | |
function get_instagram_images( $username, $limit = 100 ){ | |
$profile_url = "https://www.instagram.com/$username/?__a=1"; | |
$iteration_url = $profile_url; | |
$tryNext = true; | |
$found = 0; | |
$images = array(); | |
while ($tryNext) { | |
$tryNext = false; | |
$remote = wp_remote_get( $iteration_url ); | |
if ( is_wp_error( $remote ) ) { | |
return false; | |
} | |
if ( 200 != wp_remote_retrieve_response_code($remote) ) { | |
return false; | |
} | |
$response = wp_remote_retrieve_body( $remote ); | |
if ($response === false) { | |
return false; | |
} | |
$data = json_decode($response, true); | |
if ( $data === null) { | |
return false; | |
} | |
$media = $data['user']['media']; | |
foreach ( $media['nodes'] as $index => $node ) { | |
if ( $found + $index < $limit ) { | |
if (isset($node['is_video']) && $node['is_video'] == true) { | |
$type = 'video'; | |
} else { | |
$type = 'image'; | |
} | |
$image = array( | |
'title' => isset($node['caption']) ? $node['caption'] : '', | |
'link' => trailingslashit('//instagram.com/p/' . $node['code']), | |
'time' => $node['date'], | |
'comments' => $node['comments']['count'], | |
'likes' => $node['likes']['count'], | |
'thumbnail' => $node['thumbnail_src'], | |
'small' => $node['thumbnail_src'], | |
'full' => $node['display_src'], | |
'original' => $node['display_src'], | |
'type' => $type | |
); | |
array_push($images, $image); | |
} | |
} | |
$found += count($media['nodes']); | |
if ( $media['page_info']['has_next_page'] && $found < $limit ) { | |
$iteration_url = $profile_url . '&max_id=' . $media['page_info']['end_cursor']; | |
$tryNext = true; | |
} | |
} | |
return $images; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://www.instagram.com/$username/?__a=1
This doesn't work anymore without Instagram login now.
What would be the alternative approach?