Instagram Follower Count - Using jQuery / json & PHP

偶尔善良 提交于 2019-12-11 04:09:27

问题


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;

回答1:


Just drop the strange brackets

var insta_count = data.data.counts.followed_by;
$('li.instagram a').html(insta_count);



回答2:


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!



来源:https://stackoverflow.com/questions/23090178/instagram-follower-count-using-jquery-json-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!