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>';
    

    ?>

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

报告相同问题?

悬赏问题

  • ¥15 plotBAPC画图出错
  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理