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]
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With