Forked from shrimp2t/get-instagram-images-without-api.php
Created
May 18, 2017 17:23
-
-
Save giryaev/2b52b8660ea639687b8d0f4db7770dff 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