Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instagram API how to get user ID?

How can I get the user ID with the user name with instagram API without user authentication? If I try users/search, it returns multiple research results, so how can I be sure that I get the result of exact user name only?

For example, following requests return multiple users having their usernames similiar to aliciakeys, I want to get only the data of the user with exact user name.

https://api.instagram.com/v1/users/search?q=aliciakeys&access_token=[your token]
like image 204
user1355300 Avatar asked Oct 07 '12 15:10

user1355300


People also ask

How do I find my Instagram user ID number?

Go to instagram.com and login with your Instagram account. Click on your profile image next to the heart in the right upper corner. Your username is showing on the left side next to the "Edit profile" button.

What information can I get from Instagram API?

The API can be used to get and publish their media, manage and reply to comments on their media, identify media where they have been @mentioned by other Instagram users, find hashtagged media, and get basic metadata and metrics about other Instagram Businesses and Creators.

What is the use of user ID in Instagram?

Often referred to as your Instagram handle, a username is the name a person uses on the app to define their profile address. This may be any configuration of numbers, letters, and certain symbols, and does not have to relate to their actual name.


1 Answers

If you know that the search term will always be the full username and you can simply iterate through the results and stop when the username is an exact match of the search query.

Also, don't ever expose your access tokens in public.

This is how you would do it in PHP

<?php


function getInstaID($username)
{

    $username = strtolower($username); // sanitization
    $token = "InsertThatHere";
    $url = "https://api.instagram.com/v1/users/search?q=".$username."&access_token=".$token;
    $get = file_get_contents($url);
    $json = json_decode($get);

    foreach($json->data as $user)
    {
        if($user->username == $username)
        {
            return $user->id;
        }
    }

    return '00000000'; // return this if nothing is found
}

echo getInstaID('aliciakeys'); // this should print 20979117

?>

Actually, chances are that if you are searching for the full username, almost every time you search for it, the first result will be the one you would be looking for.

like image 143
Kartik Avatar answered Sep 22 '22 22:09

Kartik