English 中文(简体)
如何使用 PHP在 MySQL 中存储Facebook 用户朋友列表?
原标题:How to store a Facebook user s friends list in MySQL using PHP?

如何使用 PHP在 MySQL 中存储Facebook 用户朋友列表?

我可以用下面的PHP代码 获取并打印用户朋友名单?

//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.....

我怎样才能在 MySQL 存储朋友名单?

最佳回答

friends 连接将返回嵌入 data 阵列中的朋友 s name id 。 因此,您需要在 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
问题回答

>var_dump( $friends_ list_ array) 开始, 这样您就可以看到您正在实际工作的内容。 然后写一个循环, 获取您想要的相关数据, 并将其插入数据库 。

大致如下:

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.

然而,这或许最好避免。 人们的朋友总是变换,他们可能不希望你储存他们的数据,除非他们同意。

试一下,我测试过下面

$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: Kolink 指出的问题非常有效。 对于您正在做的事情, 可能有其他出路 。


<强度 > UPDATE :

如果您需要将您朋友的所有 ID 存储在 字段中( 实际上这是一个坏主意, 见下文), 那么您必须使用 JSON 将朋友列表数据( 可能是) 序列化, 并将其存储为字符串 。

$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..
} 

在单一字段中存储序列数据实际上是一个坏主意,除非您同时访问/修改整个列表。更好的选择是将数据跨行传播。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签