I'm trying to display the number of followers my account has in my php page and for some reason when I try to use the https://api.instagram.com/v1/users/{$userid}/follows?access_token={$accessToken} fetchData line, I get no information.
<?php
$userinfo = fetchData("https://api.instagram.com/v1/users/{$userid}/follows?access_token={$accessToken}");
$userinfo = json_decode($userinfo);
foreach($userinfo->data as $profile) {
$followers = $profile->counts->follow_by;
echo "Followed by: ".$followers;
}
?>
But if I use the same code with a different endpoint command and call different data, it works great (like below)
<?php
$userinfo = fetchData("https://api.instagram.com/v1/users/self/feed?access_token={$accessToken}");
$userinfo = json_decode($userinfo);
foreach($userinfo->data as $profile) {
$followers = $profile->likes->count;
echo "Followed by: ".$followers;
}
?>
Any idea what I may be doing wrong?
Thanks
The users/{userid}/follows
endpoint will not return any data on your profile. It only returns an array of user profiles that you follow.
The quickest way way to access your followers count is to use the users/{userid}
endpoint:
$userinfo = fetchData("https://api.instagram.com/v1/users/YOUR_USER_ID?access_token=YOUR_ACCESS_TOKEN");
$userinfo = json_decode($userinfo);
$followers = $userinfo['data']['counts']['followed_by'];
echo "Folled by: " . $followers . " people";
Also, for future reference, I highly recommend you test your calls using Instagram's API Console. That way you can see what data is being returned from each call, so that you can access it correctly.