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

时间:2013-12-27 11:21:32

标签: php mysql facebook

我是Facebook API的初学者,所以我想我会教自己一些宠物项目。我目前正在尝试检索用户的好友列表,针对数据库进行检查并添加它是否不在。我的代码到目前为止是:

<?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);
        }
    }
}

在有人说出来之前,我已经更换了app id和secret,所以不是那个

1 个答案:

答案 0 :(得分:0)

您可以点击此页面https://developers.facebook.com/docs/php/howto/profilewithgraphapi/

并打印echo“Name:”然后就可以了,SDK正在工作.....

然后更改$ user_profile = $ facebook-&gt; api('/ me','GET'); $ friendsLists = $ facebook-&gt; api('/ me / friends');并使用print_r($ friendsLists);

打印$ friendsLists数组

如果结果正常,那么您可以将其与数据库进行比较

注意:确保sdk路径正确require_once('php-sdk / facebook.php');

另外,您可以尝试使用FQL(实现起来非常简单) 请尝试使用此代码打印好友,并在该阵列中添加条件。

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

&GT;