簡體   English   中英

從用戶名Instagram獲取用戶ID

[英]Get User ID from username Instagram

我正在嘗試找出如何使用Instagram API從用戶名獲取用戶ID,但是我沒有運氣。

到目前為止,我的代碼是:

$username = 'some user name';
$username = strtolower($username); // sanitization
$token = "ACCESS_TOKEN_HERE";
$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;
    }
}

使用注釋中的建議,我可以使用此代碼從用戶名獲取用戶ID。 請讓我知道,因為我不是最出色的PHP開發人員,因此可能會有更簡潔的方法。

$user = USER_NAME;
$count = NUMBER_TO_RETURN;
$token = ACCESS_TOKEN_HERE;

$query = http_build_query(
                array(
                    'q'=>$user,
                    'count'=>$count,
                    'access_token'=>$token

                )
            );  
    $url = "https://api.instagram.com/v1/users/search?{$query}";

    // Gets our data
    function fetchData($url){
         $ch = curl_init();
         curl_setopt($ch, CURLOPT_URL, $url);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt($ch, CURLOPT_TIMEOUT, 20);
         $result = curl_exec($ch);
         curl_close($ch); 
         return $result;
    }

    // Pulls and parses data.
    $result = fetchData($url);
    $result = json_decode($result);
    $userid = $result->data[0]->id;
echo $userid;

將用戶名放在“”中,例如:

https://api.instagram.com/v1/users/search?q=%22bordeux1%22&access_token=TOKEN

對我來說很好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM