0

How to store a Facebook user's friends list in MySQL using PHP?

I am able to get and print the user's friends list using the PHP code below?

//get friends list
$friends_list = file_get_contents('https://graph.facebook.com/me/friends?access_token=' . $atoken );

$friends_list_array  = json_decode($friends_list,true); 

//convert so that store in mysql 
$my_friends = implode("," , $friends_list_array['data']);
    echo "</br> my friends" . $my_friends;

//result my friendsArray,Array,Array,Array,Array,Array,Array,Array.....

How can I store a friends list in MySQL?

dda
  • 6,030
  • 2
  • 25
  • 34
Abdullah Adam
  • 178
  • 5
  • 19

3 Answers3

3

the friends connection will return the friend's name and id encapsulated in a data array. So you need to go one step down the array and collect the ids before implode:

$friends_list = file_get_contents('https://graph.facebook.com/me/friends?access_token=' . $atoken );
$friends_list_array  = json_decode($friends_list,true);
$arr= $friends_list_array['data'];
$friend_ids_arr = array();
foreach($arr as $friend) {
    $friend_ids_arr[] = $friend['id'];
}
$friend_ids = implode("," , $friend_ids_arr);
echo $friend_ids; // output: id1,id2,id3...etc
ifaour
  • 38,035
  • 12
  • 72
  • 79
  • Great idea its solves my problem amazing how simple and tricky thank you so much Ibrahim can you please can we talk in chat or somewhere else – Abdullah Adam May 27 '12 at 17:16
  • You are welcome. You can reach me through the [MasteringAPI.com Contact Page](http://www.masteringapi.com/contact-us/) – ifaour May 27 '12 at 17:30
  • already got this link from your Profile :) and your SO Careers page is also nice thanks again :) – Abdullah Adam May 27 '12 at 17:33
0

Start with a var_dump($friends_list_array) so you can see what you're actually working with. Then write a loop that gets the relevant data you want and inserts it in the database.

Something along the lines of:

get the data
decode the data
create an empty array "arr"
foreach data as row
    do something with row, output should be something like
    "(123, 'John Smith', .....)"
    add this value to "arr"
implode "arr" with ","
add the start of the query ("insert into... ...values")
run the query.

However, this is probably best avoided. People's friends change all the time, and they may not want you storing their data unless they consent to it.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
0

Give the following a try. I haven't tested the below.

$sql_string = "insert into friends_table(friend_id,friend_name) values";
$insertValues = array();
$friends_list_array = json_decode(....)['data'];
foreach($friends_list_array as $friends){
    $insertValues[] = "({$friend['id']}, {$friend['name']})";
}
if (mysql_query($sql_string . implode(",", $insertValues) . ";")){
    //added all the data..
}else{
    //some error..
} 

PS: Issues pointed out by Kolink are very valid. There may be some other way out for what you are doing.


UPDATE:

If you need to store all the ids of your friends in a single field (which is actually a bad idea, see below), then you have to serialize the friends-list-data (may be) using JSON, and store it as a string.

$friends_list_array = json_decode(....)['data'];
$friends_id = array_map(function($friend){ return $friend['id'];},$friends_list_array);
$friends_list_json = json_encode($friends_id);
if (mysql_query("insert into friends_table(user, friends) values ('$curUserId','$friends_list_json';")){
    //added all the data..
}else{
    //some error..
} 

Storing a serialized data in a single field is actually a bad idea, unless you are accessing/modifying the entire list all at once. Better option would be spreading the data across rows.

UltraInstinct
  • 43,308
  • 12
  • 81
  • 104