/
facebook-get-data.php
130 lines (116 loc) · 3.94 KB
/
facebook-get-data.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
include_once("modules/config.php");
if(!loggedIn()):
header('Location: login.php');
exit();
else:
$query = $coll->findOne(array('username' => $_SESSION["username"]));
if(isset($query['facebook_access_token'])):
$access_token = $query['facebook_access_token'];
else:
callFacebookAuth();
endif;
if(isset($query['facebook_page'])):
foreach ($query['facebook_page'] as $obj_page):
//Run fql and opengraph queries
$fql_query_page = json_decode(getFacebookPageDetails($access_token, $obj_page['facebook_page_id']), true);
$opengraph_query_page = json_decode(getFacebookPageFeed($access_token, $obj_page['facebook_page_id']), true);
//Check for errors
if ($fql_query_page['error']):
if ($fql_query_page['error']['type'] == "OAuthException"):
callFacebookAuth();
else:
echo "Other Facebook authentication error has happened";
endif;
endif;
echo 'Page Details:';
echo '<br>';
echo '<pre>';
print_r($fql_query_page);
echo '</pre>';
echo '<br><br>';
// echo '<pre>';
// echo 'Page Feed:';
// echo '<br>';
// print_r($opengraph_query_page);
// echo '</pre>';
// echo '<br><br>';
echo 'Page Feed:';
echo '<br>';
foreach ($opengraph_query_page['data'] as $obj):
echo '<pre>';
echo $obj['from']['name'];
echo '<br>';
echo $obj['message'];
echo '<br>';
if (isset ($obj['likes']['data'])):
echo 'Likes: ' . count($obj['likes']['data']);
else: echo 'Likes: 0';
endif;
echo '<br>';
if (isset ($obj['comments']['data'])):
echo ' Comments';
echo '<br>';
foreach ($obj['comments']['data'] as $obj2):
echo ' ' . $obj2['from']['name'];
echo '<br>';
echo ' ' . $obj2['message'];
echo '<br>';
echo ' ' . 'Likes: '. $obj2['like_count'];
echo '<br>';
endforeach;
endif;
echo '</pre>';
echo '<br><br>';
endforeach;
endforeach;
endif;
if(isset($_POST["page"])):
// First check that required fields have been filled in.
if (empty($_POST['post_page'])):
$errors['post_page'] = "Post cannot be empty.";
endif;
endif;
if(isset($_POST["page"]) && empty($errors)):
foreach ($query['facebook_page'] as $obj_user):
$fql_query_post = postFacebookPage($_POST['post_page'], $access_token, $obj_user['facebook_page_id']);
if (isset($fql_query_post['id'])):
echo 'Post was published.';
else:
echo 'Post was not published.';
print_r($fql_query_post);
endif;
endforeach;
endif;
endif;
?>
<html>
<head>
<title>Get Facebook Data and Post</title>
</head>
<body>
<form action="<?=$_SERVER["PHP_SELF"];?>" method="POST">
<table>
<tr>
<td>
Post to Page Wall:
</td>
<td>
<input type="text" name="post_page" value="">
<span class="error">
<?php echo isset($errors['post_page']) ? $errors['post_page'] : ''; ?>
</span><br />
</td>
</tr>
<tr>
<td>
</td>
<td>
<input name="page" type="submit" value="Post">
</td>
</tr>
</table>
</form>
</body>
</html>