8
$url = 'https://api.instagram.com/v1/users/XXXX?access_token=XXXX';
echo json_decode(file_get_contents($url))->{'followed_by'};

I am using this code and I do not understand what the issue is. I'm new to PHP so excuse the newbie mistake. I'm trying to get the "followed_by" to display on its own. I've managed to get Facebook's "like" and twitter's followers to display this way.

Lincoln White
  • 639
  • 2
  • 12
  • 29
Nazar Abubaker
  • 495
  • 3
  • 7
  • 17

5 Answers5

29

In case you need to grab follower count (or other fields) without logging in, Instagram is nice enough to put them in JSON inside the page source:

$raw = file_get_contents('https://www.instagram.com/USERNAME'); //replace with user
preg_match('/\"edge_followed_by\"\:\s?\{\"count\"\:\s?([0-9]+)/',$raw,$m);
print intval($m[1]);

//returns "123"

Hope that helps.

24 May 2016 Updated to be more tolerant of spaces in JSON.

19 Apr 2018 Updated to use new "edge_" prefix.

Ben
  • 391
  • 3
  • 6
  • 2
    After **hours** trying to implement this using the Instagram API, I ended up using this solution. **Thank you**. Worst API design and documentation I've never seen. You even have to send a _screencast_ explaining what you want to do with the API to get out of sandbox mode. I just want to get the followers count for any given user, which is public information anyways. It's madness. – Marc Jul 03 '17 at 16:44
  • Great solution! I currently use it as fallback when the token expires or the api gives up :p – jo.On Nov 21 '17 at 20:28
  • @Ben This solution worked perfectly for me up until a few weeks ago, I'm guessing Instagram may have changed something on the page? Would you mind checking and updating the solution? I now get an Undefined Offset error (for $m[1]). Thanks! – user6122500 Apr 18 '18 at 03:03
  • @user6122500 Thanks for the heads up. Looks like they've added a prefix to the variable name, so I've updated the sample code above. Glad it's been helpful! – Ben Apr 18 '18 at 21:05
  • what about any profile's following count? how many people do they follow. – Muneeb Zulfiqar Nov 30 '18 at 07:17
  • @MuneebZulfiqar should be able to replace "edge_followed_by" with "edge_follow". – Ben Nov 30 '18 at 11:02
  • Finally it stopped working for me. If anyone have any workaround, please let me know. – Dezefy Aug 29 '19 at 23:15
  • @MashR. Just ran a test and it's still functioning for me, and IG hasn't changed their output either. Perhaps there's an issue getting the page? – Ben Aug 31 '19 at 06:32
  • @Ben , Thanks for your quick reply. Its working in one website but not working in two other host where i test. I checked the output, instagram is redirecting to login page and its returning the content of login page. – Dezefy Aug 31 '19 at 17:44
  • @MashR. Just a theory, but perhaps those two non-working host IPs have been rate-limited? I'm sure IG is aggressive about that. – Ben Sep 02 '19 at 07:44
  • This solution isn't working on serverside. Do anybody has an idea how to get it via serverside (by api?)? – Mitch Jan 21 '21 at 12:07
2

According to the Instagram API Docs, followed_by is a child of counts which is a child of data.

https://api.instagram.com/v1/users/1574083/?access_token=ACCESS-TOKEN

Returns:

{
"data": {
    "id": "1574083",
    "username": "snoopdogg",
    "full_name": "Snoop Dogg",
    "profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_1574083_75sq_1295469061.jpg",
    "bio": "This is my bio",
    "website": "http://snoopdogg.com",
    "counts": {
        "media": 1320,
        "follows": 420,
        "followed_by": 3410
    }
}

The following should therefore work.

<?php 
$url = 'https://api.instagram.com/v1/users/XXXX?access_token=XXXX';
$api_response = file_get_contents($url);
$record = json_decode($api_response);
echo $record->data->counts->followed_by;

// if nothing is echoed try
echo '<pre>' . print_r($api_response, true) . '</pre>';
echo '<pre>' . print_r($record, true) . '</pre>';
// to see what is in the $api_response and $record object
PassKit
  • 12,231
  • 5
  • 57
  • 75
  • I've just tried the code you've given but it does not work (I've tried both echo's on its own). I've even made sure that my api.instagram.com/xxx is valid and it is. – Nazar Abubaker Jan 19 '13 at 14:09
  • @NazarAbubaker - Are you sure that your access_token is correct - how are you generating it? Try the edited code above and let us know what is actually being returned by the API. – PassKit Jan 19 '13 at 14:17
  • I used this to generate the access_token [link]http://jelled.com/instagram/access-token[/link]. Taken from [link]http://stackoverflow.com/questions/12677551/how-to-get-access-token-from-instagram-token-using-jquery-or-php[/link] All I get is "Warning: file_get_contents() [function.file-get-contents]: Unable to find the wrapper "https" - did you forget to enable it when you configured PHP? in XXX on line 36" – Nazar Abubaker Jan 19 '13 at 14:29
  • @NazarAbubaker - So what is $api_response showing? And what happens if you manually paste the generated URL into a web browser? – PassKit Jan 19 '13 at 14:30
  • @NazarAbubaker - For the PHP error, see the comment with 31 upvotes in this question http://stackoverflow.com/questions/5444249/unable-to-find-the-wrapper-https-did-you-forget-to-enable-it-when-you-config – PassKit Jan 19 '13 at 14:33
  • When I paste the URL into a web browser I see all my instagram information. Looking at the error the "file_get_contents" is what is causing the issue – Nazar Abubaker Jan 19 '13 at 14:36
  • @NazarAbubaker - well that's good news at least! Looks like your problem is because your PHP configuration does not have the library required to load a https site. So the URL is correct, but PHP cannot retrieve it because it does not have the openssl library. Editing your PHP config and restarting Apache should work. You may also need to install openssl if you do not have it. – PassKit Jan 19 '13 at 14:39
  • @PassKit. How we can get all the api response without using pagination? – user1788736 Sep 08 '13 at 15:14
2

Try this..

<?php 
$instagram = "https://api.instagram.com/v1/users/xxxxx/?access_token=xxxxx";
$instagram_follows = json_decode(file_get_contents($instagram))->data->counts->followed_by;
echo $instagram_follows;
?>
Mayank Jain
  • 5,663
  • 7
  • 32
  • 65
1
function get_https_content($url=NULL,$method="GET"){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0');
    curl_setopt($ch, CURLOPT_URL,$url);
    return curl_exec($ch);
}

function ig_count($username) {
    return json_decode(get_https_content("https://api.instagram.com/v1/users/1460891826/?client_id=ea69458ef6a34f13949b99e84d79ccf2"))->data->counts->followed_by;
}

Here is my code :)

Wasim A.
  • 9,660
  • 22
  • 90
  • 120
0

Try this one...

$url = 'https://api.instagram.com/v1/users/USER_ID?access_token=YOUR_TOKEN';
$api_response = file_get_contents($url);
$record = json_decode($api_response);

echo $followed_by = $record->data->counts->followed_by;

Click to get all info of user

Hardik
  • 1,283
  • 14
  • 34