2011-10-04 96 views
0

我目前正在開發一個網站,允許用戶通過Facebook登錄,並從中獲取他們的所有信息以便在我的網站上使用。使用Facebook開發人員API訪問用戶的朋友信息

例如,如果他們是單身或對女性男性或兩者都感興趣。

我一直在環視Facebook Developers方面的網站,並有一些示例代碼在那裏獲取有關他們喜歡什麼電影的信息,所以我只是想知道是否有可能改變和適應這個我需要。

下面是一些代碼,我從這個頁面發現它解釋它:

http://developers.facebook.com/blog/post/481

<div id="fb-root"></div> 
<script src="http://connect.facebook.net/en_US/all.js# 
appId=YOUR_APP_ID&amp;xfbml=1"></script> 

<fb:login-button show-faces="true" width="200" max-rows="1" 
perms="user_likes, friends_likes"></fb:login-button> 

FB.api('/me/friends', function(response) { }); 
FB.api(‘/USER_ID/movies’, function(response) { }); 


FB.api('/'+movieListSorted[i].id, function(response) { 
    var newDiv = document.createElement("DIV"); 
    newDiv.innerHTML = "<img src='"+response.picture+"'></img><br/>"; 
    if(response.link) { 
    newDiv.innerHTML+="<a href='"+response.link+"'>"+response.link 
    +"</a><br/>"; 
    newDiv.innerHTML+='<iframe src="http://www.facebook.com' 
    + '/plugins/like.php?' 
    + 'href='+response.link+'&amp;layout=standard&amp;' 
    + 'show_faces=true&amp;' 
    + 'width=450&amp;action=like&amp;colorscheme=light' 
    + '&amp;height=80"' 
    + 'scrolling="no" frameborder="0" style="border:none;' 
    + 'overflow:hidden;' 
    + 'width:450px; height:80px;" allowTransparency="true"> 
    + '</iframe><br/>'; 
    } 
    document.getElementById(response.id).appendChild(newDiv); 
}); 


<html> 
<head> 
<title>Test Page</title> 
<script> 
    var movieList = new Array(); 
    var movieListSorted = new Array(); 
    var friendCount = 0; 
    function showMovies() { 
    alert(movieList.length); 
    } 
    function compareMovies(movieA, movieB) { 
    if (movieA.name === movieB.name) return 0; 
    if (movieA.name > movieB.name) return 1; 
    return -1; 
    } 
    function popularMovies(movieA, movieB) { 
    return movieB.mCount - movieA.mCount; 
    } 
    function data_fetch_postproc() { 
    document.getElementById('test').innerHTML 
     = "Generating recommendations ... "; 
    movieList.sort(compareMovies); 
    // Now we have sorted list, dedupe and count 
    mCtr = 0; 
    for (i = 0; i < movieList.length; i++) 
    { 
     var count = 0; 
     movieListSorted[mCtr] = movieList[i]; 
     for (j = i; j < movieList.length; j++) 
     { 
     if (movieList[i].name === movieList[j].name) { 
      count++; 
     } else { 
      break; 
     } 
     } 
     i = i+count-1; 
     movieListSorted[mCtr++].mCount = count; 
    } 
    var maxResults = 100; 
    if(movieListSorted.length < 100) { 
     maxResults = movieListSorted.length; 
    } 
    movieListSorted.sort(popularMovies); 
    document.getElementById('test').innerHTML = ""; 
    for(i=0; i<maxResults; i++) { 
     var newDiv = document.createElement("DIV"); 
     newDiv.id = movieListSorted[i].id; 
     newDiv.innerHTML = movieListSorted[i].name + ' : Likes - ' 
     + movieListSorted[i].mCount; 
     document.getElementById("movies").appendChild(newDiv); 
     FB.api('/'+movieListSorted[i].id, function(response) { 
     var newDiv = document.createElement("DIV"); 
     newDiv.innerHTML = "<img src='"+response.picture+"'>" 
      + "</img><br/>"; 
     if(response.link) { 
      newDiv.innerHTML+= "<a href='"+response.link+"'>" 
      +response.link+"</a><br/>"; 
      newDiv.innerHTML+= '<iframe src=' 
      + '"http://www.facebook.com/plugins/like.php?' 
      + 'href='+response.link+'&amp;layout=standard' 
      + '&amp;show_faces=true&amp;' 
      + 'width=450&amp;action=like&amp;' 
      + 'colorscheme=light&amp;height=80"' 
      + 'scrolling="no" frameborder="0" style="' 
      + 'border:none; overflow:hidden;' 
      + 'width:450px; height:80px;"' 
      + 'allowTransparency="true"></iframe><br/>'; 
     } 
     document.getElementById(response.id).appendChild(newDiv); 
     }); 
    } 
    } 
    function get_friend_likes() { 
    document.getElementById('test').innerHTML = "Requesting " 
     + "data from Facebook ... "; 
    FB.api('/me/friends', function(response) { 
     friendCount = response.data.length; 
     for(i=0; i<response.data.length; i++) { 
      friendId = response.data[i].id; 
      FB.api('/'+friendId+'/movies', function(response) { 
      movieList = movieList.concat(response.data); 
      friendCount--; 
      document.getElementById('test').innerHTML = friendCount 
       + " friends to go ... "; 
      if(friendCount === 0) { data_fetch_postproc(); }; 
      }); 
     } 
     }); 
    } 
</script> 
</head> 
<body> 
<div id="fb-root"></div> 
<div id="login"></div> 
<div id="test"></div> 
<div id="movies"></div> 
<script src="http://connect.facebook.net/en_US/all.js"></script> 
<script> 
FB.init({ 
    appId : 'YOUR_APP_ID', 
    status : true, // check login status 
    cookie : true, // enable cookies 
    xfbml : true // parse XFBML 
    }); 
FB.Event.subscribe('auth.sessionChange', function(response) { 
    window.location.reload(); 
}); 
FB.getLoginStatus(function(response) { 
    if (response.session) { 
    // logged in and connected user, someone you know 
    get_friend_likes(); 
    document.getElementById('login').innerHTML 
     ='<a href="#" onclick="FB.logout();">Logout</a><br/>'; 
    } else { 
    document.getElementById('login').innerHTML 
     ='<fb:login-button show-faces="true" width="200"' 
     + ' max-rows="1" perms="user_likes, friends_likes">' 
     + '</fb:login-button>'; 
    FB.XFBML.parse(); 
    } 
}); 
</script> 
</body> 
</html> 

回答

0

我建議使用PHP SDK來獲取此信息,特別是如果你是初學者。然後你可以使用他們的功能。

您可以在以下網址找到這個源代碼(包括一些文檔): https://developers.facebook.com/docs/reference/php/

首先,爲了得到從圖形API什麼,你將需要獲得認證用戶對您的應用程序,這是記錄在這裏:

https://developers.facebook.com/docs/authentication/

然後,你可以得到你需要有關當前用戶的信息,使用API​​調用。這是記錄在這裏:

http://developers.facebook.com/docs/reference/php/facebook-api/

筆記的代碼在這裏下面,我已經編輯這個以提供用戶的「interested_in」字段當前登錄的記錄here

try { 

     $user_profile = $facebook->api('/me?fields=interested_in','GET'); 
     echo "Interested in: " . $user_profile['interested_in']; 

     } catch(FacebookApiException $e) { 
     // Failed API call 
     error_log($e->getType()); 
     error_log($e->getMessage()); 
     } 
相關問題