我试图找出如何使用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;
}
}
答案 0 :(得分:0)
使用评论中的建议,我可以使用此代码从用户名获取用户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;
答案 1 :(得分:0)
将用户名放在“”中,例如:
https://api.instagram.com/v1/users/search?q=%22bordeux1%22&access_token=TOKEN
对我来说工作正常。