[英]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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.