2012-10-07 90 views
7

如何在沒有用戶身份驗證的情況下用instagram API獲取用戶名和用戶名?如果我嘗試用戶/搜索,它會返回多個研究結果,那麼如何才能確保我只能得到確切用戶名的結果?Instagram API如何獲取用戶ID?

例如,以下請求返回具有與aliciakeys類似的用戶名的多個用戶,我只想獲取具有精確用戶名的用戶的數據。

https://api.instagram.com/v1/users/search?q=aliciakeys&access_token=[your token] 

回答

14

如果您知道搜索詞永遠是滿的用戶名,你可以簡單地遍歷這些結果和停止時的用戶名是搜索查詢的精確匹配。

此外,千萬不要公開您的訪問令牌。

這是你會怎麼做它在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 

?> 

其實,機會是,如果你正在尋找完整的用戶名,幾乎每次搜索它,第一個結果將是一個你會尋找。

+5

肯定第一個結果總是你要找的結果是錯誤的。看看這個例子 - https://snap.apigee.com/1PuJeFw#content=request。當尋找「音樂」用戶時,這不是第一個結果。更甚者,它甚至不在結果集中,因爲它們僅限於一定數量的項目。 – ecdeveloper

相關問題