/
get_friends_list.php
102 lines (66 loc) · 2.48 KB
/
get_friends_list.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
include_once "facebook.php";
$facebook = new Facebook(array(
'appId' => "you app id", // Update your app id
'secret' => "your app seceret", // Update your app secret
'cookie' => false, // enable optional cookie support
));
// This function handles inserts the data to your db
function db_insert($insert_query)
{
// Instantiate the mysqli class
$mysqli = new mysqli();
//Provide your DB credentials here
// Connect to the database server and select a database
$mysqli->connect('your db server', 'your db id', 'your password', 'your db name');
//Execute query
$result = $mysqli->query($insert_query);
//commit
$commit = $mysqli->commit();
// Close the connection
$mysqli->close();
}
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Purple BI: Facebook Data Extraction & Analytics </title>
</head>
<body>
<h2>Facebook Data Extraction</h2>
<h3>Retrieves and stores friends list</h3>
<?php
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => 'user_about_me',
'redirect_uri' => "http://your URL here/get_friends_list.php" // Give absolute path of current file
)
);
$logoutUrl = $facebook->getLogoutUrl();
$user = $facebook->getUser();
//if user is logged in and session is valid.
if ($user) {
try {
$friends = $facebook->api('/me/friends?limit=100'); // Number limited to 100; You can change this
$count = count($friends['data']);
echo $count; echo " friends retrieved"; // Displays number of friends retrieved
} catch (FacebookApiException $e) {
error_log($e);
echo "Error in retrieving data";
}
//Prepare insert query string
$query = "INSERT INTO friends_list(name, uid) VALUES ('Nawendu',10000000),"; // You can insert your name as one of the rows
for ($i = 0; $i < $count; $i++) {
$query .= '(' . "'" . $friends[data][$i]['name'] . "'" . ',' . $friends[data][$i]['id'] . ')' ;
if ( $i !==$count -1 ) $query.= ', ';
}
//Call DB insert function
db_insert($query);
echo '<a href="http://nawendubharti.com"><p>Go back to Purple BI</a>'; // You can have your homepage URL here
} else {
echo '<a href="' . $loginUrl . '"><p>Login to Facebook to proceed</a>';
}
?>
</body>
</html>