开发者

Import Facebook friends list to mysql database using php

开发者 https://www.devze.com 2023-02-17 22:09 出处:网络
I am building a small facebook app for my website. Using the Facebook friends API I am able toget a list of friends of the user. Now I want to see if any of his friends are already members in my site

I am building a small facebook app for my website. Using the Facebook friends API I am able to get a list of friends of the user. Now I want to see if any of his friends are already members in my site if yes I connect them as friends in my site.

I have a user table which has two columns:

User id | Facebook id 
---------------------
   8901 |    98764021

I want to run a check of all the friends id given by facebook and see if they have a user id in my site if yes link them as friends.

I get the facebook friends list as JSON format which might run to few thousands. This is the form in which i get it.

{
    "name": "Chris Anderson",
    "id": "401085912"
}

How do i compare this 开发者_C百科against MySQL table in my database. Is it possible to create a temporary table and import the JSON values and do the comparison. If so how? or if there is a better method pls suggest. Kindly share any sample code you might have.


Using json_decode(), put all your user's friend facebook ids in an array, then use the IN SQL keyword :

$facebook_ids = array( 25364646, 2353634646, 2352352335) //example array
$sqlized_ids = implode(', ', $facebook_ids);

$sql_query = 'SELECT user_id FROM user_facebook_table WHERE facebook_id IN ('.$sqlized_ids.')'; 
$friends_in_your_db = $res= mysql_query($sql_query); 

Better performance : to PHP the logic, to SQL the data and search.


convert the array from json object to a php array and check each item of the array with each tuple of your table. simple. if you understood what i want to say then its okay, else comment back to get a code example from my side.


UPDATES

<?php
$json = '{ "name": "Chris Anderson", "id": "401085912" }';
$arr=new array();
$arr=var_dump(json_decode($json, true));
?>

The result is a 2d array. so now you can easily compare the array contents with the data that you fetch from your records:

$query= "select * from user_details"; //this is the query to pick all data from your table
$res= mysql_query($query);
while($arr_db=mysql_fetch_assoc($res))
{

//compare the current values of the $arr_db with the full $arr array

}   
0

精彩评论

暂无评论...
验证码 换一张
取 消