Question

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

Was it helpful?

Solution

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.

OTHER TIPS

I know its an old Question but it may help some one else.

$api_key = 'YOR_API_KEY';
$user_id = 'XXX';
$data = @file_get_contents("https://api.instagram.com/v1/users/$user_id/?client_id=$api_key");
$data = json_decode($data, true);
echo $data['data']['counts']['followed_by'];
//echo '<pre/>';
//print_r($data);

This do not required access token its only required API key. If you are not able to get the user id of the user but you know his/her name just go to the page and simply inspect profile image in browser you will figure out the user id from there.

I hope it will help some one else.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top