1 |
douglas |
943 |
<?php |
2 |
douglas |
946 |
# vim: expandtab shiftwidth=2 tabstop=2 |
3 |
|
|
# |
4 |
|
|
# Modified to use JSON when $json is set to true. |
5 |
|
|
# |
6 |
|
|
# Douglas Thrift |
7 |
|
|
# |
8 |
|
|
# $Id$ |
9 |
douglas |
943 |
// |
10 |
|
|
// +---------------------------------------------------------------------------+ |
11 |
douglas |
945 |
// | Facebook Platform PHP5 client | |
12 |
douglas |
943 |
// +---------------------------------------------------------------------------+ |
13 |
douglas |
945 |
// | Copyright (c) 2007 Facebook, Inc. | |
14 |
douglas |
943 |
// | All rights reserved. | |
15 |
|
|
// | | |
16 |
|
|
// | Redistribution and use in source and binary forms, with or without | |
17 |
|
|
// | modification, are permitted provided that the following conditions | |
18 |
|
|
// | are met: | |
19 |
|
|
// | | |
20 |
|
|
// | 1. Redistributions of source code must retain the above copyright | |
21 |
|
|
// | notice, this list of conditions and the following disclaimer. | |
22 |
|
|
// | 2. Redistributions in binary form must reproduce the above copyright | |
23 |
|
|
// | notice, this list of conditions and the following disclaimer in the | |
24 |
|
|
// | documentation and/or other materials provided with the distribution. | |
25 |
|
|
// | | |
26 |
|
|
// | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
27 |
|
|
// | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
28 |
|
|
// | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
29 |
|
|
// | IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | |
30 |
|
|
// | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
31 |
|
|
// | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
32 |
|
|
// | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
33 |
|
|
// | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
34 |
|
|
// | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
35 |
|
|
// | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
36 |
|
|
// +---------------------------------------------------------------------------+ |
37 |
|
|
// | For help with this library, contact developers-help@facebook.com | |
38 |
|
|
// +---------------------------------------------------------------------------+ |
39 |
|
|
// |
40 |
|
|
|
41 |
|
|
class FacebookRestClient { |
42 |
|
|
public $secret; |
43 |
|
|
public $session_key; |
44 |
|
|
public $api_key; |
45 |
|
|
public $friends_list; // to save making the friends.get api call, this will get prepopulated on canvas pages |
46 |
|
|
public $added; // to save making the users.isAppAdded api call, this will get prepopulated on canvas pages |
47 |
douglas |
946 |
public $json; |
48 |
douglas |
943 |
|
49 |
|
|
/** |
50 |
|
|
* Create the client. |
51 |
|
|
* @param string $session_key if you haven't gotten a session key yet, leave |
52 |
|
|
* this as null and then set it later by just |
53 |
|
|
* directly accessing the $session_key member |
54 |
|
|
* variable. |
55 |
|
|
*/ |
56 |
|
|
public function __construct($api_key, $secret, $session_key=null) { |
57 |
|
|
$this->secret = $secret; |
58 |
|
|
$this->session_key = $session_key; |
59 |
|
|
$this->api_key = $api_key; |
60 |
|
|
$this->last_call_id = 0; |
61 |
|
|
$this->server_addr = Facebook::get_facebook_url('api') . '/restserver.php'; |
62 |
|
|
if ($GLOBALS['facebook_config']['debug']) { |
63 |
|
|
$this->cur_id = 0; |
64 |
|
|
?> |
65 |
|
|
<script type="text/javascript"> |
66 |
|
|
var types = ['params', 'xml', 'php', 'sxml']; |
67 |
|
|
function toggleDisplay(id, type) { |
68 |
|
|
for each (var t in types) { |
69 |
|
|
if (t != type || document.getElementById(t + id).style.display == 'block') { |
70 |
|
|
document.getElementById(t + id).style.display = 'none'; |
71 |
|
|
} else { |
72 |
|
|
document.getElementById(t + id).style.display = 'block'; |
73 |
|
|
} |
74 |
|
|
} |
75 |
|
|
return false; |
76 |
|
|
} |
77 |
|
|
</script> |
78 |
|
|
<?php |
79 |
|
|
} |
80 |
|
|
} |
81 |
|
|
|
82 |
|
|
/** |
83 |
|
|
* Returns the session information available after current user logs in. |
84 |
|
|
* @param string $auth_token the token returned by auth_createToken or |
85 |
|
|
* passed back to your callback_url. |
86 |
|
|
* @return assoc array containing session_key, uid |
87 |
|
|
*/ |
88 |
|
|
public function auth_getSession($auth_token) { |
89 |
|
|
$result = $this->call_method('facebook.auth.getSession', array('auth_token'=>$auth_token)); |
90 |
|
|
$this->session_key = $result['session_key']; |
91 |
|
|
if (isset($result['secret']) && $result['secret']) { |
92 |
|
|
// desktop apps have a special secret |
93 |
|
|
$this->secret = $result['secret']; |
94 |
|
|
} |
95 |
|
|
return $result; |
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
/** |
99 |
|
|
* Returns events according to the filters specified. |
100 |
|
|
* @param int $uid Optional: User associated with events. |
101 |
|
|
* A null parameter will default to the session user. |
102 |
|
|
* @param array $eids Optional: Filter by these event ids. |
103 |
|
|
* A null parameter will get all events for the user. |
104 |
|
|
* @param int $start_time Optional: Filter with this UTC as lower bound. |
105 |
|
|
* A null or zero parameter indicates no lower bound. |
106 |
|
|
* @param int $end_time Optional: Filter with this UTC as upper bound. |
107 |
|
|
* A null or zero parameter indicates no upper bound. |
108 |
|
|
* @param string $rsvp_status Optional: Only show events where the given uid |
109 |
|
|
* has this rsvp status. This only works if you have specified a value for |
110 |
|
|
* $uid. Values are as in events.getMembers. Null indicates to ignore |
111 |
|
|
* rsvp status when filtering. |
112 |
|
|
* @return array of events |
113 |
|
|
*/ |
114 |
|
|
public function events_get($uid, $eids, $start_time, $end_time, $rsvp_status) { |
115 |
|
|
return $this->call_method('facebook.events.get', |
116 |
|
|
array( |
117 |
|
|
'uid' => $uid, |
118 |
|
|
'eids' => $eids, |
119 |
|
|
'start_time' => $start_time, |
120 |
|
|
'end_time' => $end_time, |
121 |
|
|
'rsvp_status' => $rsvp_status)); |
122 |
|
|
} |
123 |
|
|
|
124 |
|
|
/** |
125 |
|
|
* Returns membership list data associated with an event |
126 |
|
|
* @param int $eid : event id |
127 |
|
|
* @return assoc array of four membership lists, with keys 'attending', |
128 |
|
|
* 'unsure', 'declined', and 'not_replied' |
129 |
|
|
*/ |
130 |
|
|
public function events_getMembers($eid) { |
131 |
|
|
return $this->call_method('facebook.events.getMembers', |
132 |
|
|
array('eid' => $eid)); |
133 |
|
|
} |
134 |
|
|
|
135 |
|
|
/** |
136 |
|
|
* Makes an FQL query. This is a generalized way of accessing all the data |
137 |
|
|
* in the API, as an alternative to most of the other method calls. More |
138 |
|
|
* info at http://developers.facebook.com/documentation.php?v=1.0&doc=fql |
139 |
|
|
* @param string $query the query to evaluate |
140 |
|
|
* @return generalized array representing the results |
141 |
|
|
*/ |
142 |
|
|
public function fql_query($query) { |
143 |
|
|
return $this->call_method('facebook.fql.query', |
144 |
|
|
array('query' => $query)); |
145 |
|
|
} |
146 |
|
|
|
147 |
|
|
public function feed_publishStoryToUser($title, $body, |
148 |
|
|
$image_1=null, $image_1_link=null, |
149 |
|
|
$image_2=null, $image_2_link=null, |
150 |
|
|
$image_3=null, $image_3_link=null, |
151 |
douglas |
950 |
$image_4=null, $image_4_link=null) { |
152 |
douglas |
943 |
return $this->call_method('facebook.feed.publishStoryToUser', |
153 |
|
|
array('title' => $title, |
154 |
|
|
'body' => $body, |
155 |
|
|
'image_1' => $image_1, |
156 |
|
|
'image_1_link' => $image_1_link, |
157 |
|
|
'image_2' => $image_2, |
158 |
|
|
'image_2_link' => $image_2_link, |
159 |
|
|
'image_3' => $image_3, |
160 |
|
|
'image_3_link' => $image_3_link, |
161 |
|
|
'image_4' => $image_4, |
162 |
douglas |
950 |
'image_4_link' => $image_4_link)); |
163 |
douglas |
943 |
} |
164 |
|
|
|
165 |
|
|
public function feed_publishActionOfUser($title, $body, |
166 |
|
|
$image_1=null, $image_1_link=null, |
167 |
|
|
$image_2=null, $image_2_link=null, |
168 |
|
|
$image_3=null, $image_3_link=null, |
169 |
douglas |
950 |
$image_4=null, $image_4_link=null) { |
170 |
douglas |
943 |
return $this->call_method('facebook.feed.publishActionOfUser', |
171 |
|
|
array('title' => $title, |
172 |
|
|
'body' => $body, |
173 |
|
|
'image_1' => $image_1, |
174 |
|
|
'image_1_link' => $image_1_link, |
175 |
|
|
'image_2' => $image_2, |
176 |
|
|
'image_2_link' => $image_2_link, |
177 |
|
|
'image_3' => $image_3, |
178 |
|
|
'image_3_link' => $image_3_link, |
179 |
|
|
'image_4' => $image_4, |
180 |
douglas |
950 |
'image_4_link' => $image_4_link)); |
181 |
douglas |
943 |
} |
182 |
|
|
|
183 |
douglas |
951 |
public function feed_publishTemplatizedAction($actor_id, $title_template, $title_data, |
184 |
|
|
$body_template, $body_data, $body_general, |
185 |
|
|
$image_1=null, $image_1_link=null, |
186 |
|
|
$image_2=null, $image_2_link=null, |
187 |
|
|
$image_3=null, $image_3_link=null, |
188 |
|
|
$image_4=null, $image_4_link=null, |
189 |
|
|
$target_ids=null) { |
190 |
|
|
return $this->call_method('facebook.feed.publishTemplatizedAction', |
191 |
|
|
array('actor_id' => $actor_id, |
192 |
|
|
'title_template' => $title_template, |
193 |
|
|
'title_data' => $title_data, |
194 |
|
|
'body_template' => $body_template, |
195 |
|
|
'body_data' => $body_data, |
196 |
|
|
'body_general' => $body_general, |
197 |
|
|
'image_1' => $image_1, |
198 |
|
|
'image_1_link' => $image_1_link, |
199 |
|
|
'image_2' => $image_2, |
200 |
|
|
'image_2_link' => $image_2_link, |
201 |
|
|
'image_3' => $image_3, |
202 |
|
|
'image_3_link' => $image_3_link, |
203 |
|
|
'image_4' => $image_4, |
204 |
douglas |
952 |
'image_4_link' => $image_4_link, |
205 |
douglas |
951 |
'target_ids' => $target_ids)); |
206 |
|
|
} |
207 |
|
|
|
208 |
douglas |
943 |
/** |
209 |
|
|
* Returns whether or not pairs of users are friends. |
210 |
|
|
* Note that the Facebook friend relationship is symmetric. |
211 |
|
|
* @param array $uids1: array of ids (id_1, id_2,...) of some length X |
212 |
|
|
* @param array $uids2: array of ids (id_A, id_B,...) of SAME length X |
213 |
|
|
* @return array of uid pairs with bool, true if pair are friends, e.g. |
214 |
|
|
* array( 0 => array('uid1' => id_1, 'uid2' => id_A, 'are_friends' => 1), |
215 |
|
|
* 1 => array('uid1' => id_2, 'uid2' => id_B, 'are_friends' => 0) |
216 |
|
|
* ...) |
217 |
|
|
*/ |
218 |
|
|
public function friends_areFriends($uids1, $uids2) { |
219 |
|
|
return $this->call_method('facebook.friends.areFriends', |
220 |
|
|
array('uids1'=>$uids1, 'uids2'=>$uids2)); |
221 |
|
|
} |
222 |
|
|
|
223 |
|
|
/** |
224 |
|
|
* Returns the friends of the current session user. |
225 |
|
|
* @return array of friends |
226 |
|
|
*/ |
227 |
|
|
public function friends_get() { |
228 |
|
|
if (isset($this->friends_list)) { |
229 |
|
|
return $this->friends_list; |
230 |
|
|
} |
231 |
|
|
return $this->call_method('facebook.friends.get', array()); |
232 |
|
|
} |
233 |
|
|
|
234 |
|
|
/** |
235 |
|
|
* Returns the friends of the session user, who are also users |
236 |
|
|
* of the calling application. |
237 |
|
|
* @return array of friends |
238 |
|
|
*/ |
239 |
|
|
public function friends_getAppUsers() { |
240 |
|
|
return $this->call_method('facebook.friends.getAppUsers', array()); |
241 |
|
|
} |
242 |
|
|
|
243 |
|
|
/** |
244 |
|
|
* Returns groups according to the filters specified. |
245 |
|
|
* @param int $uid Optional: User associated with groups. |
246 |
|
|
* A null parameter will default to the session user. |
247 |
|
|
* @param array $gids Optional: group ids to query. |
248 |
|
|
* A null parameter will get all groups for the user. |
249 |
|
|
* @return array of groups |
250 |
|
|
*/ |
251 |
|
|
public function groups_get($uid, $gids) { |
252 |
|
|
return $this->call_method('facebook.groups.get', |
253 |
|
|
array( |
254 |
|
|
'uid' => $uid, |
255 |
|
|
'gids' => $gids)); |
256 |
|
|
} |
257 |
|
|
|
258 |
|
|
/** |
259 |
|
|
* Returns the membership list of a group |
260 |
|
|
* @param int $gid : Group id |
261 |
|
|
* @return assoc array of four membership lists, with keys |
262 |
|
|
* 'members', 'admins', 'officers', and 'not_replied' |
263 |
|
|
*/ |
264 |
|
|
public function groups_getMembers($gid) { |
265 |
|
|
return $this->call_method('facebook.groups.getMembers', |
266 |
|
|
array('gid' => $gid)); |
267 |
|
|
} |
268 |
|
|
|
269 |
|
|
/** |
270 |
|
|
* Returns the outstanding notifications for the session user. |
271 |
|
|
* @return assoc array of |
272 |
|
|
* notification count objects for 'messages', 'pokes' and 'shares', |
273 |
|
|
* a uid list of 'friend_requests', a gid list of 'group_invites', |
274 |
|
|
* and an eid list of 'event_invites' |
275 |
|
|
*/ |
276 |
|
|
public function notifications_get() { |
277 |
|
|
return $this->call_method('facebook.notifications.get', array()); |
278 |
|
|
} |
279 |
|
|
|
280 |
|
|
/** |
281 |
|
|
* Sends an email notification to the specified user. |
282 |
|
|
* @return string url which you should send the logged in user to to finalize the message. |
283 |
|
|
*/ |
284 |
|
|
public function notifications_send($to_ids, $notification, $email='') { |
285 |
|
|
return $this->call_method('facebook.notifications.send', |
286 |
|
|
array('to_ids' => $to_ids, 'notification' => $notification, 'email' => $email)); |
287 |
|
|
} |
288 |
|
|
|
289 |
|
|
/** |
290 |
|
|
* Sends a request to the specified user (e.g. "you have 1 event invitation") |
291 |
|
|
* @param array $to_ids user ids to receive the request (must be friends with sender, capped at 10) |
292 |
|
|
* @param string $type type of request, e.g. "event" (as in "You have an event invitation.") |
293 |
|
|
* @param string $content fbml content of the request. really stripped down fbml - just |
294 |
|
|
* text/names/links. also, use the special tag <fb:req-choice url="" label="" /> |
295 |
|
|
* to specify the buttons to be included. |
296 |
|
|
* @param string $image url of an image to show beside the request |
297 |
|
|
* @param bool $invite whether to call it an "invitation" or a "request" |
298 |
|
|
* @return string url which you should send the logged in user to to finalize the message. |
299 |
|
|
*/ |
300 |
|
|
public function notifications_sendRequest($to_ids, $type, $content, $image, $invite) { |
301 |
|
|
return $this->call_method('facebook.notifications.sendRequest', |
302 |
|
|
array('to_ids' => $to_ids, 'type' => $type, 'content' => $content, |
303 |
|
|
'image' => $image, 'invite' => $invite)); |
304 |
|
|
} |
305 |
|
|
|
306 |
|
|
/** |
307 |
|
|
* Returns photos according to the filters specified. |
308 |
|
|
* @param int $subj_id Optional: Filter by uid of user tagged in the photos. |
309 |
|
|
* @param int $aid Optional: Filter by an album, as returned by |
310 |
|
|
* photos_getAlbums. |
311 |
|
|
* @param array $pids Optional: Restrict to a list of pids |
312 |
|
|
* Note that at least one of these parameters needs to be specified, or an |
313 |
|
|
* error is returned. |
314 |
|
|
* @return array of photo objects. |
315 |
|
|
*/ |
316 |
|
|
public function photos_get($subj_id, $aid, $pids) { |
317 |
|
|
return $this->call_method('facebook.photos.get', |
318 |
|
|
array('subj_id' => $subj_id, 'aid' => $aid, 'pids' => $pids)); |
319 |
|
|
} |
320 |
|
|
|
321 |
|
|
/** |
322 |
|
|
* Returns the albums created by the given user. |
323 |
|
|
* @param int $uid Optional: the uid of the user whose albums you want. |
324 |
|
|
* A null value will return the albums of the session user. |
325 |
|
|
* @param array $aids Optional: a list of aids to restrict the query. |
326 |
|
|
* Note that at least one of the (uid, aids) parameters must be specified. |
327 |
|
|
* @returns an array of album objects. |
328 |
|
|
*/ |
329 |
|
|
public function photos_getAlbums($uid, $aids) { |
330 |
|
|
return $this->call_method('facebook.photos.getAlbums', |
331 |
|
|
array('uid' => $uid, |
332 |
|
|
'aids' => $aids)); |
333 |
|
|
} |
334 |
|
|
|
335 |
|
|
/** |
336 |
|
|
* Returns the tags on all photos specified. |
337 |
|
|
* @param string $pids : a list of pids to query |
338 |
|
|
* @return array of photo tag objects, with include pid, subject uid, |
339 |
|
|
* and two floating-point numbers (xcoord, ycoord) for tag pixel location |
340 |
|
|
*/ |
341 |
|
|
public function photos_getTags($pids) { |
342 |
|
|
return $this->call_method('facebook.photos.getTags', |
343 |
|
|
array('pids' => $pids)); |
344 |
|
|
} |
345 |
|
|
|
346 |
|
|
/** |
347 |
|
|
* Returns the requested info fields for the requested set of users |
348 |
|
|
* @param array $uids an array of user ids |
349 |
|
|
* @param array $fields an array of strings describing the info fields desired |
350 |
|
|
* @return array of users |
351 |
|
|
*/ |
352 |
|
|
public function users_getInfo($uids, $fields) { |
353 |
|
|
return $this->call_method('facebook.users.getInfo', array('uids' => $uids, 'fields' => $fields)); |
354 |
|
|
} |
355 |
|
|
|
356 |
|
|
/** |
357 |
|
|
* Returns the user corresponding to the current session object. |
358 |
|
|
* @return integer uid |
359 |
|
|
*/ |
360 |
|
|
public function users_getLoggedInUser(){ |
361 |
|
|
return $this->call_method('facebook.users.getLoggedInUser', array()); |
362 |
|
|
} |
363 |
|
|
|
364 |
|
|
|
365 |
|
|
/** |
366 |
|
|
* Returns whether or not the user corresponding to the current session object has the app installed |
367 |
|
|
* @return boolean |
368 |
|
|
*/ |
369 |
|
|
public function users_isAppAdded() { |
370 |
|
|
if (isset($this->added)) { |
371 |
|
|
return $this->added; |
372 |
|
|
} |
373 |
|
|
return $this->call_method('facebook.users.isAppAdded', array()); |
374 |
|
|
} |
375 |
|
|
|
376 |
|
|
/** |
377 |
|
|
* Sets the FBML for the profile of the user attached to this session |
378 |
|
|
* @param string $markup The FBML that describes the profile presence of this app for the user |
379 |
|
|
* @return array A list of strings describing any compile errors for the submitted FBML |
380 |
|
|
*/ |
381 |
|
|
public function profile_setFBML($markup, $uid = null) { |
382 |
|
|
return $this->call_method('facebook.profile.setFBML', array('markup' => $markup, 'uid' => $uid)); |
383 |
|
|
} |
384 |
|
|
|
385 |
|
|
public function profile_getFBML($uid) { |
386 |
|
|
return $this->call_method('facebook.profile.getFBML', array('uid' => $uid)); |
387 |
|
|
} |
388 |
|
|
|
389 |
|
|
public function fbml_refreshImgSrc($url) { |
390 |
|
|
return $this->call_method('facebook.fbml.refreshImgSrc', array('url' => $url)); |
391 |
|
|
} |
392 |
|
|
|
393 |
|
|
public function fbml_refreshRefUrl($url) { |
394 |
|
|
return $this->call_method('facebook.fbml.refreshRefUrl', array('url' => $url)); |
395 |
|
|
} |
396 |
|
|
|
397 |
|
|
public function fbml_setRefHandle($handle, $fbml) { |
398 |
|
|
return $this->call_method('facebook.fbml.setRefHandle', array('handle' => $handle, 'fbml' => $fbml)); |
399 |
|
|
} |
400 |
|
|
|
401 |
|
|
/* UTILITY FUNCTIONS */ |
402 |
|
|
|
403 |
|
|
public function call_method($method, $params) { |
404 |
douglas |
946 |
if ($this->json) { |
405 |
|
|
$json = $this->post_request($method, $params); |
406 |
|
|
# XXX: silly facebook with its invalid JSON |
407 |
|
|
$valid = preg_match('/^[\[{].*[\]}]$/', $json); |
408 |
|
|
$array = json_decode($valid ? $json : "[$json]", true); |
409 |
|
|
$result = $valid ? $array : $array[0]; |
410 |
|
|
} else { |
411 |
|
|
$xml = $this->post_request($method, $params); |
412 |
|
|
$sxml = simplexml_load_string($xml); |
413 |
|
|
$result = self::convert_simplexml_to_array($sxml); |
414 |
|
|
if ($GLOBALS['facebook_config']['debug']) { |
415 |
|
|
// output the raw xml and its corresponding php object, for debugging: |
416 |
|
|
print '<div style="margin: 10px 30px; padding: 5px; border: 2px solid black; background: gray; color: white; font-size: 12px; font-weight: bold;">'; |
417 |
|
|
$this->cur_id++; |
418 |
|
|
print $this->cur_id . ': Called ' . $method . ', show ' . |
419 |
|
|
'<a href=# onclick="return toggleDisplay(' . $this->cur_id . ', \'params\');">Params</a> | '. |
420 |
|
|
'<a href=# onclick="return toggleDisplay(' . $this->cur_id . ', \'xml\');">XML</a> | '. |
421 |
|
|
'<a href=# onclick="return toggleDisplay(' . $this->cur_id . ', \'sxml\');">SXML</a> | '. |
422 |
|
|
'<a href=# onclick="return toggleDisplay(' . $this->cur_id . ', \'php\');">PHP</a>'; |
423 |
|
|
print '<pre id="params'.$this->cur_id.'" style="display: none; overflow: auto;">'.print_r($params, true).'</pre>'; |
424 |
|
|
print '<pre id="xml'.$this->cur_id.'" style="display: none; overflow: auto;">'.htmlspecialchars($xml).'</pre>'; |
425 |
|
|
print '<pre id="php'.$this->cur_id.'" style="display: none; overflow: auto;">'.print_r($result, true).'</pre>'; |
426 |
|
|
print '<pre id="sxml'.$this->cur_id.'" style="display: none; overflow: auto;">'.print_r($sxml, true).'</pre>'; |
427 |
|
|
print '</div>'; |
428 |
|
|
} |
429 |
douglas |
943 |
} |
430 |
|
|
if (is_array($result) && isset($result['error_code'])) { |
431 |
|
|
throw new FacebookRestClientException($result['error_msg'], $result['error_code']); |
432 |
|
|
} |
433 |
|
|
return $result; |
434 |
|
|
} |
435 |
|
|
|
436 |
|
|
public function post_request($method, $params) { |
437 |
|
|
$params['method'] = $method; |
438 |
|
|
$params['session_key'] = $this->session_key; |
439 |
|
|
$params['api_key'] = $this->api_key; |
440 |
|
|
$params['call_id'] = microtime(true); |
441 |
|
|
if ($params['call_id'] <= $this->last_call_id) { |
442 |
|
|
$params['call_id'] = $this->last_call_id + 0.001; |
443 |
|
|
} |
444 |
|
|
$this->last_call_id = $params['call_id']; |
445 |
|
|
if (!isset($params['v'])) { |
446 |
|
|
$params['v'] = '1.0'; |
447 |
|
|
} |
448 |
douglas |
946 |
if ($this->json) |
449 |
|
|
$params['format'] = 'JSON'; |
450 |
douglas |
943 |
$post_params = array(); |
451 |
|
|
foreach ($params as $key => &$val) { |
452 |
|
|
if (is_array($val)) $val = implode(',', $val); |
453 |
|
|
$post_params[] = $key.'='.urlencode($val); |
454 |
|
|
} |
455 |
douglas |
946 |
$post_params[] = 'sig='.Facebook::generate_sig($params, $this->secret); |
456 |
douglas |
943 |
$post_string = implode('&', $post_params); |
457 |
|
|
|
458 |
douglas |
946 |
if ($this->json) { |
459 |
|
|
$result = file_get_contents($this->server_addr, false, stream_context_create( |
460 |
|
|
array('http' => |
461 |
|
|
array('method' => 'POST', |
462 |
|
|
'header' => 'Content-type: application/x-www-form-urlencoded'."\r\n". |
463 |
|
|
'User-Agent: Facebook API PHP5 Client 1.1 (non-curl) '.phpversion()."\r\n". |
464 |
|
|
'Content-length: ' . strlen($post_string), |
465 |
|
|
'content' => $post_string)))); |
466 |
|
|
} elseif (function_exists('curl_init')) { |
467 |
douglas |
943 |
// Use CURL if installed... |
468 |
|
|
$ch = curl_init(); |
469 |
|
|
curl_setopt($ch, CURLOPT_URL, $this->server_addr); |
470 |
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); |
471 |
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
472 |
|
|
curl_setopt($ch, CURLOPT_USERAGENT, 'Facebook API PHP5 Client 1.1 (curl) ' . phpversion()); |
473 |
|
|
$result = curl_exec($ch); |
474 |
|
|
curl_close($ch); |
475 |
|
|
} else { |
476 |
|
|
// Non-CURL based version... |
477 |
|
|
$context = |
478 |
|
|
array('http' => |
479 |
|
|
array('method' => 'POST', |
480 |
|
|
'header' => 'Content-type: application/x-www-form-urlencoded'."\r\n". |
481 |
|
|
'User-Agent: Facebook API PHP5 Client 1.1 (non-curl) '.phpversion()."\r\n". |
482 |
|
|
'Content-length: ' . strlen($post_string), |
483 |
|
|
'content' => $post_string)); |
484 |
|
|
$contextid=stream_context_create($context); |
485 |
|
|
$sock=fopen($this->server_addr, 'r', false, $contextid); |
486 |
|
|
if ($sock) { |
487 |
|
|
$result=''; |
488 |
|
|
while (!feof($sock)) |
489 |
|
|
$result.=fgets($sock, 4096); |
490 |
|
|
|
491 |
|
|
fclose($sock); |
492 |
|
|
} |
493 |
|
|
} |
494 |
|
|
return $result; |
495 |
|
|
} |
496 |
|
|
|
497 |
|
|
public static function convert_simplexml_to_array($sxml) { |
498 |
|
|
$arr = array(); |
499 |
|
|
if ($sxml) { |
500 |
|
|
foreach ($sxml as $k => $v) { |
501 |
|
|
if ($sxml['list']) { |
502 |
|
|
$arr[] = self::convert_simplexml_to_array($v); |
503 |
|
|
} else { |
504 |
|
|
$arr[$k] = self::convert_simplexml_to_array($v); |
505 |
|
|
} |
506 |
|
|
} |
507 |
|
|
} |
508 |
|
|
if (sizeof($arr) > 0) { |
509 |
|
|
return $arr; |
510 |
|
|
} else { |
511 |
|
|
return (string)$sxml; |
512 |
|
|
} |
513 |
|
|
} |
514 |
|
|
} |
515 |
|
|
|
516 |
|
|
class FacebookRestClientException extends Exception { |
517 |
|
|
} |
518 |
|
|
|
519 |
|
|
// Supporting methods and values------ |
520 |
|
|
|
521 |
|
|
/** |
522 |
|
|
* Error codes and descriptions for the Facebook API. |
523 |
|
|
*/ |
524 |
|
|
|
525 |
|
|
class FacebookAPIErrorCodes { |
526 |
|
|
|
527 |
|
|
const API_EC_SUCCESS = 0; |
528 |
|
|
|
529 |
|
|
/* |
530 |
|
|
* GENERAL ERRORS |
531 |
|
|
*/ |
532 |
|
|
const API_EC_UNKNOWN = 1; |
533 |
|
|
const API_EC_SERVICE = 2; |
534 |
|
|
const API_EC_METHOD = 3; |
535 |
|
|
const API_EC_TOO_MANY_CALLS = 4; |
536 |
|
|
const API_EC_BAD_IP = 5; |
537 |
|
|
|
538 |
|
|
/* |
539 |
|
|
* PARAMETER ERRORS |
540 |
|
|
*/ |
541 |
|
|
const API_EC_PARAM = 100; |
542 |
|
|
const API_EC_PARAM_API_KEY = 101; |
543 |
|
|
const API_EC_PARAM_SESSION_KEY = 102; |
544 |
|
|
const API_EC_PARAM_CALL_ID = 103; |
545 |
|
|
const API_EC_PARAM_SIGNATURE = 104; |
546 |
|
|
const API_EC_PARAM_USER_ID = 110; |
547 |
|
|
const API_EC_PARAM_USER_FIELD = 111; |
548 |
|
|
const API_EC_PARAM_SOCIAL_FIELD = 112; |
549 |
|
|
const API_EC_PARAM_ALBUM_ID = 120; |
550 |
|
|
|
551 |
|
|
/* |
552 |
|
|
* USER PERMISSIONS ERRORS |
553 |
|
|
*/ |
554 |
|
|
const API_EC_PERMISSION = 200; |
555 |
|
|
const API_EC_PERMISSION_USER = 210; |
556 |
|
|
const API_EC_PERMISSION_ALBUM = 220; |
557 |
|
|
const API_EC_PERMISSION_PHOTO = 221; |
558 |
|
|
|
559 |
|
|
const FQL_EC_PARSER = 601; |
560 |
|
|
const FQL_EC_UNKNOWN_FIELD = 602; |
561 |
|
|
const FQL_EC_UNKNOWN_TABLE = 603; |
562 |
|
|
const FQL_EC_NOT_INDEXABLE = 604; |
563 |
|
|
|
564 |
|
|
public static $api_error_descriptions = array( |
565 |
|
|
API_EC_SUCCESS => 'Success', |
566 |
|
|
API_EC_UNKNOWN => 'An unknown error occurred', |
567 |
|
|
API_EC_SERVICE => 'Service temporarily unavailable', |
568 |
|
|
API_EC_METHOD => 'Unknown method', |
569 |
|
|
API_EC_TOO_MANY_CALLS => 'Application request limit reached', |
570 |
|
|
API_EC_BAD_IP => 'Unauthorized source IP address', |
571 |
|
|
API_EC_PARAM => 'Invalid parameter', |
572 |
|
|
API_EC_PARAM_API_KEY => 'Invalid API key', |
573 |
|
|
API_EC_PARAM_SESSION_KEY => 'Session key invalid or no longer valid', |
574 |
|
|
API_EC_PARAM_CALL_ID => 'Call_id must be greater than previous', |
575 |
|
|
API_EC_PARAM_SIGNATURE => 'Incorrect signature', |
576 |
|
|
API_EC_PARAM_USER_ID => 'Invalid user id', |
577 |
|
|
API_EC_PARAM_USER_FIELD => 'Invalid user info field', |
578 |
|
|
API_EC_PARAM_SOCIAL_FIELD => 'Invalid user field', |
579 |
|
|
API_EC_PARAM_ALBUM_ID => 'Invalid album id', |
580 |
|
|
API_EC_PERMISSION => 'Permissions error', |
581 |
|
|
API_EC_PERMISSION_USER => 'User not visible', |
582 |
|
|
API_EC_PERMISSION_ALBUM => 'Album not visible', |
583 |
|
|
API_EC_PERMISSION_PHOTO => 'Photo not visible', |
584 |
|
|
FQL_EC_PARSER => 'FQL: Parser Error', |
585 |
|
|
FQL_EC_UNKNOWN_FIELD => 'FQL: Unknown Field', |
586 |
|
|
FQL_EC_UNKNOWN_TABLE => 'FQL: Unknown Table', |
587 |
|
|
FQL_EC_NOT_INDEXABLE => 'FQL: Statement not indexable', |
588 |
|
|
FQL_EC_UNKNOWN_FUNCTION => 'FQL: Attempted to call unknown function', |
589 |
|
|
FQL_EC_INVALID_PARAM => 'FQL: Invalid parameter passed in', |
590 |
|
|
); |
591 |
|
|
} |
592 |
|
|
|
593 |
|
|
$profile_field_array = array( |
594 |
|
|
"about_me", |
595 |
|
|
"activities", |
596 |
|
|
"affiliations", |
597 |
|
|
"birthday", |
598 |
|
|
"books", |
599 |
|
|
"current_location", |
600 |
|
|
"education_history", |
601 |
|
|
"first_name", |
602 |
|
|
"hometown_location", |
603 |
|
|
"hs_info", |
604 |
|
|
"interests", |
605 |
|
|
"is_app_user", |
606 |
|
|
"last_name", |
607 |
|
|
"meeting_for", |
608 |
|
|
"meeting_sex", |
609 |
|
|
"movies", |
610 |
|
|
"music", |
611 |
|
|
"name", |
612 |
|
|
"notes_count", |
613 |
|
|
"pic", |
614 |
|
|
"pic_big", |
615 |
|
|
"pic_small", |
616 |
|
|
"political", |
617 |
|
|
"profile_update_time", |
618 |
|
|
"quotes", |
619 |
|
|
"relationship_status", |
620 |
|
|
"religion", |
621 |
|
|
"sex", |
622 |
|
|
"significant_other_id", |
623 |
|
|
"status", |
624 |
|
|
"timezone", |
625 |
|
|
"tv", |
626 |
|
|
"wall_count", |
627 |
|
|
"work_history"); |
628 |
|
|
?> |