开发者

facebook graph api

I am trying to get the facebook username using graph api. Here is what I got so far, but its not giving me any data back. please help .

$(document).ready( function() {

    var arr = 1101892195;
    var url = 'http://graph.facebook.com/' + arr;

    $.getJSON(url, function(data) {
        var items = [];
        $.each(data, function(key, val) {
            items.push('li id="' + key + '"' + val + '/li');
        });
        $('ul', {
            'class': 'my-new-list',
            htm开发者_如何学JAVAl: items.join('')
        }).appendTo('body');
    });

});


This is restriction of same origin policy you can't do this with jquery

Proxy script:

<?php 
echo file_get_contents("http://graph.facebook.com/".$_GET['atr']);
?>

Modified jS:

$(document).ready( function() {

    var arr = 1101892195;
    var url = 'proxy.php?atr=' + arr;

    $.getJSON(url, function(data) {
        var items = [];
        $.each(data, function(key, val) {
            items.push('li id="' + key + '"' + val + '/li');
        });
        $('ul', {
            'class': 'my-new-list',
            html: items.join('')
        }).appendTo('body');
    });

});


It would be alot simpler using the Facebook javascript SDK. If you are only trying to get basic public information, you won't need to have the user authenticate or even specify an app id.

Here is a full example:

<!DOCTYPE html>
<html>
<body>
<div id="fb-root"></div>
<a href="#" onclick="getUser('1101892195');return false;">Get User Info</a>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
 function getUser(id) {  
    FB.api('/' + id,  function(response) {
        alert('Full response: ' + JSON.stringify(response));
        alert('User: ' + response.name);
      }
    );
}
</script>
</body>
</html>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜