Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mgilank/ca00b07122fd2d75ee43d31a1086a06a to your computer and use it in GitHub Desktop.
Save mgilank/ca00b07122fd2d75ee43d31a1086a06a to your computer and use it in GitHub Desktop.
Fecth Instagram Media by Username using PHP without Instagram API
<?php
/**
* [getMediaByUsername description]
*
* Author : Imam Ali Mustofa - Betta Dev Indonesia
* Website : www.bettadevindonesia.com
*
* This function will scrap data from instagram without API
* Define username you need and define how many media will you get!
*
* @param [type] $userName [Define username]
* @param [type] $count [Define number of media]
* @return [type] JSON [Return value in JSON]
*
* Thank for solution from : @rimvy
* from thread : https://www.blackhatworld.com/seo/load-more-than-12-pictures-instagram-api-php.1004450/#post-10762797
*
*/
function getMediaByUsername($userName, $count = 16) {
$uname = $userName; // from parameter
$username = strtolower(str_replace(' ','_',$uname));
$url = "https://www.instagram.com/$username/?__a=1";
$userinfo = file_get_contents($url);
$userdata = json_decode($userinfo,true);
$user = $userdata['graphql']['user'];
$iteration_url = $url;
// echo json_encode($userdata);
// die();
if(!empty($user)){
$followers = $user['edge_followed_by']['count'];
$follow = $user['edge_follow']['count'];
$fullname = $user['full_name'];
$usernamee = $user['username'];
$profilepic = $user['profile_pic_url_hd'];
$limit = $count; // from parameter
$tryNext = true;
$found = 0;
$images_all = array();
while ($tryNext) {
$tryNext = false;
$remote = file_get_contents( $iteration_url );
$response = $remote;
if ($response === false) {
return false;
}
$data = json_decode($response, true);
if ( $data === null) {
return false;
}
$media = $data['graphql']['user']['edge_owner_to_timeline_media'];
foreach ( $media['edges'] as $index => $node ) {
if ( $found + $index < $limit ) {
if (isset($node['node']['is_video']) && $node['node']['is_video'] == true) {
$type = 'video';
} else {
$type = 'image';
}
$image = array(
'caption' => $node['node']['edge_media_to_caption']['edges'][0]['node']['text'],
'timestamp' => $node['node']['taken_at_timestamp'],
'link' => 'https://instagram.com/p/' . $node['node']['shortcode'],
'comments' => $node['node']['edge_media_to_comment']['count'],
'likes' => $node['node']['edge_liked_by']['count'],
'thumbnail' => $node['node']['thumbnail_src'],
'small' => $node['node']['thumbnail_src'],
'full' => $node['node']['display_url'],
'original' => $node['node']['display_url']
);
array_push($images_all, $image);
}
}
$found += count($media['edges']);
if ( $media['page_info']['has_next_page'] && $found < $limit ) {
$iteration_url = $url . '&max_id=' . $media['page_info']['end_cursor'];
$tryNext = true;
}
}
$instagramMedia = array(
'user_info' => array(
'fullname' => $fullname,
'username' => $username,
'followers' => $followers,
'follow' => $follow,
'profilepic' => $profilepic
),
'user_media' => $images_all
);
return json_encode($instagramMedia);
} else{
return 'Username does not exists';
}
}
if(isset($_GET['usernameOfInstagram'])) {
echo getMediaByUsername($_GET['usernameOfInstagram'], $_GET['countMedia']);
} else {
echo "Please define Username will you fetch!";
}
<?php
/**
* [getMediaByUsername description]
*
* Author : Imam Ali Mustofa - Betta Dev Indonesia
* Website : www.bettadevindonesia.com
*
* This function will scrap data from instagram without API
* Define username you need and define how many media will you get!
*
* @param [type] $userName [Define username]
* @param [type] $count [Define number of media]
* @return [type] JSON [Return value in JSON]
*
* Thank for solution from : @rimvy
* from thread : https://www.blackhatworld.com/seo/load-more-than-12-pictures-instagram-api-php.1004450/#post-10762797
*
*/
function getMediaByUsername($userName, $count) {
$uname = $userName; // from parameter
$username = strtolower(str_replace(' ','_',$uname));
$url = "https://www.instagram.com/$username/?__a=1";
$userinfo = file_get_contents($url);
$userdata = json_decode($userinfo,true);
$user = $userdata['graphql']['user'];
$iteration_url = $url;
// echo json_encode($userdata);
// die();
if(!empty($user)){
$followers = $user['edge_followed_by']['count'];
$follow = $user['edge_follow']['count'];
$fullname = $user['full_name'];
$usernamee = $user['username'];
$profilepic = $user['profile_pic_url_hd'];
$limit = $count; // from parameter
$tryNext = true;
$found = 0;
$images_all = array();
while ($tryNext) {
$tryNext = false;
$remote = file_get_contents( $iteration_url );
$response = $remote;
if ($response === false) {
return false;
}
$data = json_decode($response, true);
if ( $data === null) {
return false;
}
$media = $data['graphql']['user']['edge_owner_to_timeline_media'];
foreach ( $media['edges'] as $index => $node ) {
if ( $found + $index < $limit ) {
if (isset($node['node']['is_video']) && $node['node']['is_video'] == true) {
$type = 'video';
} else {
$type = 'image';
}
$image = array(
'caption' => $node['node']['edge_media_to_caption']['edges'][0]['node']['text'],
'timestamp' => $node['node']['taken_at_timestamp'],
'link' => 'https://instagram.com/p/' . $node['node']['shortcode'],
'comments' => $node['node']['edge_media_to_comment']['count'],
'likes' => $node['node']['edge_liked_by']['count'],
'thumbnail' => $node['node']['thumbnail_src'],
'small' => $node['node']['thumbnail_src'],
'full' => $node['node']['display_url'],
'original' => $node['node']['display_url']
);
array_push($images_all, $image);
}
}
$found += count($media['edges']);
if ( $media['page_info']['has_next_page'] && $found < $limit ) {
$iteration_url = $url . '&max_id=' . $media['page_info']['end_cursor'];
$tryNext = true;
}
}
$instagramMedia = array(
'user_info' => array(
'fullname' => $fullname,
'username' => $username,
'followers' => $followers,
'follow' => $follow,
'profilepic' => $profilepic
),
'user_media' => $images_all
);
return json_encode($instagramMedia);
} else{
return 'Username does not exists';
}
}
echo getMediaByUsername('instagram', 20);
@mrtglrcll
Copy link

this is just reloading page can you help me with this please if necessary for a fee

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