Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get instagram images without API
<?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;
}
@vladimirlukyanov

This comment has been minimized.

Copy link

@vladimirlukyanov vladimirlukyanov commented Dec 9, 2019

No working anymore properly

@webistomin

This comment has been minimized.

Copy link

@webistomin webistomin commented May 13, 2020

Made a small package to download images, though it is written in TypeScript.
https://github.com/webistomin/nanogram.js

@topskilldev

This comment has been minimized.

Copy link

@topskilldev topskilldev commented Jun 24, 2020

https://www.instagram.com/$username/?__a=1

This doesn't work anymore without Instagram login now.
What would be the alternative approach?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment