dongmei5168 2013-12-27 11:21
浏览 47
已采纳

Facebook API - 将朋友存储在数据库中

I'm a beginner with the Facebook APIs, so I thought I'd teach myself with a few pet projects. I'm currently trying to retrieve the user's friend list, check it against a database and add if it's not in. My code so far is:

<?php

$database = new mysqli('host', 'username', 'password', 'db');

if ($users = $database->query('SELECT id FROM `friends`')) {
    while ($row = $users->fetch_assoc()) {
        $my_friend[] = $row['id'];
    }
}
require('src/facebook.php');
$facebook = new Facebook(array(
            'appId' => 'id',
            'secret' => 'secret',
            'cookie' => true,
        ));
// $session is only != null, when you have the session-cookie, that is set by facebook,      after the user logs in
$session = $facebook->getSession();
// you dont get a list of friends, but a list, which contains other friendlists
$friendsLists = $facebook->api('/me/friends');

// Save all Friends and FriendConnections
foreach ($friendsLists as $friends) {
    foreach ($friends as $friend) {
        // do something with the friend, but you only have id and name
        $id = $friend['id'];
        $name = $friend['name'];
        echo $name . " (" . $id . ") <br>";
        if (!in_array($id, $my_friends)) {
            $query = "INSERT into friends (id,name) VALUES ($id, $name)";
            mysql_query($query);
        }
    }
}

And before anyone says it, I have replaced the app id and secret so it's not that

  • 写回答

1条回答 默认 最新

  • douwojiao5919 2013-12-27 11:35
    关注

    You can just follow with this page https://developers.facebook.com/docs/php/howto/profilewithgraphapi/

    and print the echo "Name: " then it is ok , SDK is working.....

    then change $user_profile =$facebook->api('/me','GET'); to $friendsLists = $facebook->api('/me/friends'); and print the $friendsLists array using print_r($friendsLists);

    if result is ok then you can compare it with your DB

    NB : Make sure sdk path is correct require_once('php-sdk/facebook.php');

    OTHERWISE you can try with FQL ( It is very simple to implement) please try this code to print friends, and add your conditions in that array.

    <?php
      $app_id = 'YOUR_APP_ID';
      $app_secret = 'YOUR_APP_SECRET';
      $my_url = 'POST_AUTH_URL';
    
      $code = $_REQUEST["code"];
    
     // auth user
     if(empty($code)) {
        $dialog_url = 'https://www.facebook.com/dialog/oauth?client_id=' 
        . $app_id . '&redirect_uri=' . urlencode($my_url) ;
        echo("<script>top.location.href='" . $dialog_url . "'</script>");
      }
    
      // get user access_token
      $token_url = 'https://graph.facebook.com/oauth/access_token?client_id='
        . $app_id . '&redirect_uri=' . urlencode($my_url) 
        . '&client_secret=' . $app_secret 
        . '&code=' . $code;
    
      // response is of the format "access_token=AAAC..."
      $access_token = substr(file_get_contents($token_url), 13);
    
      // run fql query
      $fql_query_url = 'https://graph.facebook.com/'
    . 'fql?q=SELECT+uid2+FROM+friend+WHERE+uid1=me()'
    . '&access_token=' . $access_token;
      $fql_query_result = file_get_contents($fql_query_url);
      $fql_query_obj = json_decode($fql_query_result, true);
    
      // display results of fql query
      echo '<pre>';
      print_r("query results:");
      print_r($fql_query_obj);
      echo '</pre>';
    
      // run fql multiquery
      $fql_multiquery_url = 'https://graph.facebook.com/'
    . 'fql?q={"all+friends":"SELECT+uid2+FROM+friend+WHERE+uid1=me()",'
    . '"my+name":"SELECT+name+FROM+user+WHERE+uid=me()"}'
    . '&access_token=' . $access_token;
      $fql_multiquery_result = file_get_contents($fql_multiquery_url);
      $fql_multiquery_obj = json_decode($fql_multiquery_result, true);
    
      // display results of fql multiquery
      echo '<pre>';
      print_r("multi query results:");
      print_r($fql_multiquery_obj);
      echo '</pre>';
    

    ?>

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP