Last active
December 29, 2020 18:51
-
-
Save darkterminal/6e20489eda3afafb1a724e74d9b7a6a8 to your computer and use it in GitHub Desktop.
Fecth Instagram Media by Username using PHP without Instagram API
This file contains hidden or 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 | |
/** | |
* [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 = 12) { | |
$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; | |
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'; | |
} | |
$caption = $node['node']['edge_media_to_caption']['edges']; | |
$mediaCaption = ''; | |
for ($i=0; $i < sizeof($caption); $i++) { | |
$mediaCaption = $caption[$i]['node']['text']; | |
} | |
$image = array( | |
'caption' => $mediaCaption, | |
'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!"; | |
} |
This file contains hidden or 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 | |
/** | |
* [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 = 12) { | |
$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; | |
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'; | |
} | |
$caption = $node['node']['edge_media_to_caption']['edges']; | |
$mediaCaption = ''; | |
for ($i=0; $i < sizeof($caption); $i++) { | |
$mediaCaption = $caption[$i]['node']['text']; | |
} | |
$image = array( | |
'caption' => $mediaCaption, | |
'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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this is just reloading page can you help me with this please if necessary for a fee