Instagram API如何获取用户ID? [英] Instagram API how to get user ID?

查看:566
本文介绍了Instagram API如何获取用户ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在没有用户身份验证的情况下使用instagram API获得带有用户名的用户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?

例如,以下请求将多个用户名与aliciakeys相似的用户返回,我只想获取具有准确用户名的用户的数据.

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]

推荐答案

如果您知道搜索项将始终是完整的用户名,并且您可以简单地遍历结果并在用户名与搜索项完全匹配时停止搜索查询.

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.

这是您将在PHP中完成的方式

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.

这篇关于Instagram API如何获取用户ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆