2

After a couple hours work and a little help from Sahil Mittal we've managed to retrieve the Instagram Follower Count using jQuery / json and PHP. This is what we've managed to put together and hope this helps anyone else looking to get Instagram information.

Our jQuery:

// INSTAGRAM COUNT WITH HOVER
    $('.instagram a').hover(
        function () {
            var instaurl = 'getdata.php'; // Add your PHP URL here.
            $.getJSON(instaurl, function(data){
                var instacount = data["data"]["counts"]["followed_by"];
                $('.instagram a').html(instacount);
            });
        },
        function () {
            $('.instagram a').html('Instagram');
    });

Our PHP (getdata.php) :

   header('Access-Control-Allow-Origin: *');
   header('Content-Type: application/json');    
   $instaurl = file_get_contents("https://api.instagram.com/v1/users/XXXXXXX/?access_token=XXXXXXXXXXXXXXXXXXXX"); // Add your ID & Access Token
   echo $instaurl;
Delete
  • 429
  • 1
  • 6
  • 25
  • Follow this answer work for me http://stackoverflow.com/questions/14414606/getting-basic-information-from-instagram-using-php/31812442#31812442 – Wasim A. Aug 04 '15 at 14:48
  • This api isn't support anymore, do you have a new suggestion for getting the followercount? – Mitch Jan 21 '21 at 12:05

2 Answers2

1

Just drop the strange brackets

var insta_count = data.data.counts.followed_by;
$('li.instagram a').html(insta_count);
adeneo
  • 312,895
  • 29
  • 395
  • 388
0

That's wrong- data["data.counts.followed_by"]

Try this instead - data["data"]["counts"]["followed_by"];

Edit

You cannot fetch the data from api.instagram.com from ajax call due to No 'Access-Control-Allow-Origin'. Read here for details on this.

Workaround

Make a server call instead. Make a php file, say getdata.php

getdata.php

<?php
 $a = file_get_contents("https://api.instagram.com/v1/users/481503861/?access_token=53042481.ab103e5.0c6f8f50471a4e1f97595f8db529a47a");
 echo json_encode($a);
?>

your ajax call

 var instaurl = 'getdata.php';
 $.getJSON(instaurl, function(data){
     var instacount = data.data.counts.followed_by;
     $('.instagram a').html(instacount);
 });

Hope that helps!

Community
  • 1
  • 1
Sahil Mittal
  • 20,697
  • 12
  • 65
  • 90
  • Sahil Mittal, Thank you for your ressponse unfortently I can't seem to get this working. I've updated my code above. Could you double check that everything looks good. – Delete Apr 16 '14 at 08:24
  • `console.log(data)` and tell me whats the output? – Sahil Mittal Apr 16 '14 at 08:28
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/50761/discussion-between-adam-wadsworth-and-sahil-mittal) – Delete Apr 16 '14 at 08:52
  • Add `header('Access-Control-Allow-Origin: *');` to the php file and then check . – Sahil Mittal Apr 16 '14 at 09:00