-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.php
198 lines (165 loc) · 6.68 KB
/
test.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?
// Remember to copy files from the SDK's src/ directory to a
// directory in your application on the server, such as php-sdk/
include_once('facebook.php');
$config = array(
'appId' => '274836979297194',
'secret' => '07bd57effc23dc91316d07fac68bba53',
);
// Initialize a Facebook instance from the PHP SDK
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
// Declare the variables we'll use to demonstrate
// the new event-management APIs
// We'll create an event in this example.
// We'll need create_event permission for this.
$event_id = 359893154086611;
$event_name = "New Event API Test Event";
$event_start = time(); // We'll just start the event now.
$event_privacy = "SECRET"; // We'll make it secret so we don't annoy folks.
// We'll use three users to demostrate the new APIs
$user_id1 = "100000061103790";
$user_id2 = "100000178962457";
$user_id3 = "100000782350531";
// Convenience method to print simple pre-formatted text.
function printMsg($msg) {
echo "<pre>$msg</pre>";
}
?>
<html>
<head></head>
<body>
<?
if($user_id) {
// We have a user ID, so probably a logged in user.
// If not, we'll get an exception, which we handle below.
try {
// Get the user profiles so we can print friendly messages
$me = $facebook->api('/me', 'GET');
//var_dump($me);
$user_1 = $facebook->api($user_id1, 'GET');
// var_dump($user_1);
$user_2 = $facebook->api($user_id2, 'GET');
// var_dump($user_2);
$user_3 = $facebook->api($user_id3, 'GET');
// var_dump($user_3);
printMsg('User 1: ' . $user_1['name']);
printMsg('User 2: ' . $user_2['name']);
printMsg('User 3: ' . $user_3['name']);
// Create an event
$ret_obj = $facebook->api('/me/events', 'POST', array(
'name' => $event_name,
'start_time' => $event_start,
'privacy' => $event_privacy
));
if(isset($ret_obj['id'])) {
// Success
$event_id = $ret_obj['id'];
printMsg('Event ID: ' . $event_id);
var_dump($event_id);
} else {
printMsg("Couldn't create event.");
}
// Invite user 1 to the event
printMsg('Inviting ' . $user_1['name']);
$ret_val = $facebook->api($event_id . "/invited/" . $user_id1,
'POST');
if($ret_val) {
// Success
printMsg($user_1['name'] . ' successfully invited.');
} else {
printMsg("Couldn't invite " . $user_1['name']);
}
// Check if user 1 is invited to the event
$ret_val = $facebook->api($event_id . '/invited/' . $user_id1, 'GET');
if($ret_val) {
printMsg($user_1['name'] . ' is invited (checked).');
} else {
printMsg($user_1['name'] . ' is not invited');
}
// User 1 should be no reply, so we check now.
// Check in the same way for /attending, /maybe, /declined
$ret_val = $facebook->api($event_id . '/noreply/' . $user_id1, 'GET');
if($ret_val) {
printMsg($user_1['name'] . ' has not replied.');
} else {
printMsg('Error: ' . $user_1['name'] . ' has replied');
}
// Check if user 2 is invited to the event (shouldn't be, yet)
$ret_obj = $facebook->api($event_id . '/invited/' . $user_id2, 'GET');
// If the user is not invited, we'll get an empty data[] array
if(count($ret_obj['data']) > 0) {
printMsg('Error: ' . $user_2['name'] . ' invited.');
} else {
printMsg($user_2['name'] . ' is not invited');
}
// Invite users 2 & 3
printMsg('Inviting ' . $user_2['name'] . ' and ' . $user_3['name']);
$ret_val = $facebook->api(
$event_id . '/invited?user=' .
$user_id1 . ',' . $user_id2,
'POST');
if($ret_val) {
printMsg($user_2['name'] . ' and ' . $user_3['name'] . ' invited');
} else {
printMsg("Couldn't invite " . $user_2['name'] .
' and ' . $user_3['name']);
}
// Now User 2 should be invited.
$ret_obj = $facebook->api($event_id . '/invited/' . $user_id2, 'GET');
// If the user is not invited, we'll get an empty data[] array
if(count($ret_obj['data']) > 0) {
printMsg($user_2['name'] . ' invited (checked).');
} else {
printMsg('Error: ' . $user_2['name'] . ' is not invited');
}
// Un-invite user 2
printMsg('Un-inviting ' . $user_2['name']);
$ret_val = $facebook->api($event_id . '/invited/' . $user_id2,
'DELETE');
if($ret_val) {
printMsg($user_2['name'] . ' un-invited.');
} else {
printMsg("Couldn't un-invite " . $user_2['name'] );
}
// Creator of the event ("me") should be attending by default
printMsg('Checking if creator of event (' .
$me['name'] . ') is attending');
$ret_obj = $facebook->api($event_id . '/attending/' . $me['id'],
'GET');
// If attending, the data[] array will have our ID in it.
if(isset($ret_obj['data'][0])) {
printMsg($ret_obj['data'][0]['name'] . ' is attending');
} else {
printMsg('Error: Creator of event is not attending ?!');
}
// Delete the event
printMsg('Deleting the event.');
$ret_val = $facebook->api($event_id, 'DELETE');
if($ret_val) {
printMsg('Event deleted');
} else {
printMsg("Error: Couldn't delete event");
}
} catch(FacebookApiException $e) {
// If the user is logged out, you can have a
// user ID even though the access token is invalid.
// In this case, we'll get an exception, so we'll
// just ask the user to login again here.
$login_url = $facebook->getLoginUrl( array(
'scope' => 'create_event, rsvp_event'
));
echo 'Please <a href="' . $login_url . '">login.</a>';
error_log($e->getType());
error_log($e->getMessage());
}
} else {
// No user, so print a link for the user to login
$login_url = $facebook->getLoginUrl( array(
'scope' => 'create_event,rsvp_event'
));
echo 'Please <a href="' . $login_url . '">login.</a>';
}
?>
</body>
</html>