ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/facebook/trunk/facebookapi_php5_restlib.php
Revision: 1058
Committed: 2008-06-25T02:40:10-07:00 (16 years, 11 months ago) by douglas
File size: 65740 byte(s)
Log Message:
Merged with 2008-06-24!

File Contents

# Content
1 <?php
2 # 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
10 // Copyright 2004-2008 Facebook. All Rights Reserved.
11 //
12 // +---------------------------------------------------------------------------+
13 // | Facebook Platform PHP5 client |
14 // +---------------------------------------------------------------------------+
15 // | Copyright (c) 2007-2008 Facebook, Inc. |
16 // | All rights reserved. |
17 // | |
18 // | Redistribution and use in source and binary forms, with or without |
19 // | modification, are permitted provided that the following conditions |
20 // | are met: |
21 // | |
22 // | 1. Redistributions of source code must retain the above copyright |
23 // | notice, this list of conditions and the following disclaimer. |
24 // | 2. Redistributions in binary form must reproduce the above copyright |
25 // | notice, this list of conditions and the following disclaimer in the |
26 // | documentation and/or other materials provided with the distribution. |
27 // | |
28 // | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
29 // | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
30 // | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
31 // | IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
32 // | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
33 // | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
34 // | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
35 // | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
36 // | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
37 // | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
38 // +---------------------------------------------------------------------------+
39 // | For help with this library, contact developers-help@facebook.com |
40 // +---------------------------------------------------------------------------+
41 //
42
43 class FacebookRestClient {
44 public $secret;
45 public $session_key;
46 public $api_key;
47 public $friends_list; // to save making the friends.get api call, this will get prepopulated on canvas pages
48 public $added; // to save making the users.isAppAdded api call, this will get prepopulated on canvas pages
49 public $json;
50 public $batch_mode;
51 private $batch_queue;
52 private $call_as_apikey;
53
54 const BATCH_MODE_DEFAULT = 0;
55 const BATCH_MODE_SERVER_PARALLEL = 0;
56 const BATCH_MODE_SERIAL_ONLY = 2;
57
58 /**
59 * Create the client.
60 * @param string $session_key if you haven't gotten a session key yet, leave
61 * this as null and then set it later by just
62 * directly accessing the $session_key member
63 * variable.
64 */
65 public function __construct($api_key, $secret, $session_key=null) {
66 $this->secret = $secret;
67 $this->session_key = $session_key;
68 $this->api_key = $api_key;
69 $this->batch_mode = FacebookRestClient::BATCH_MODE_DEFAULT;
70 $this->last_call_id = 0;
71 $this->call_as_apikey = '';
72 $this->server_addr = Facebook::get_facebook_url('api') . '/restserver.php';
73 if (!empty($GLOBALS['facebook_config']['debug'])) {
74 $this->cur_id = 0;
75 ?>
76 <script type="text/javascript">
77 var types = ['params', 'xml', 'php', 'sxml'];
78 function getStyle(elem, style) {
79 if (elem.getStyle) {
80 return elem.getStyle(style);
81 } else {
82 return elem.style[style];
83 }
84 }
85 function setStyle(elem, style, value) {
86 if (elem.setStyle) {
87 elem.setStyle(style, value);
88 } else {
89 elem.style[style] = value;
90 }
91 }
92 function toggleDisplay(id, type) {
93 for (var i = 0; i < types.length; i++) {
94 var t = types[i];
95 var pre = document.getElementById(t + id);
96 if (pre) {
97 if (t != type || getStyle(pre, 'display') == 'block') {
98 setStyle(pre, 'display', 'none');
99 } else {
100 setStyle(pre, 'display', 'block');
101 }
102 }
103 }
104 return false;
105 }
106 </script>
107 <?php
108 }
109 }
110
111
112 /**
113 * Start a batch operation.
114 */
115 public function begin_batch() {
116 if($this->batch_queue !== null)
117 {
118 throw new FacebookRestClientException(FacebookAPIErrorCodes::API_EC_BATCH_ALREADY_STARTED,
119 FacebookAPIErrorCodes::$api_error_descriptions[FacebookAPIErrorCodes::API_EC_BATCH_ALREADY_STARTED]);
120 }
121
122 $this->batch_queue = array();
123 }
124
125 /*
126 * End current batch operation
127 */
128 public function end_batch() {
129 if($this->batch_queue === null) {
130 throw new FacebookRestClientException(FacebookAPIErrorCodes::API_EC_BATCH_NOT_STARTED,
131 FacebookAPIErrorCodes::$api_error_descriptions[FacebookAPIErrorCodes::API_EC_BATCH_NOT_STARTED]);
132 }
133
134 $this->execute_server_side_batch();
135
136 $this->batch_queue = null;
137 }
138
139
140 private function execute_server_side_batch() {
141
142
143 $item_count = count($this->batch_queue);
144 $method_feed = array();
145 foreach($this->batch_queue as $batch_item) {
146 $method_feed[] = $this->create_post_string($batch_item['m'], $batch_item['p']);
147 }
148
149 $method_feed_json = json_encode($method_feed);
150
151 $serial_only = $this->batch_mode == FacebookRestClient::BATCH_MODE_SERIAL_ONLY ;
152 $params = array('method_feed' => $method_feed_json, 'serial_only' => $serial_only);
153 if ($this->call_as_apikey) {
154 $params['call_as_apikey'] = $this->call_as_apikey;
155 }
156
157 $xml = $this->post_request('batch.run', $params);
158
159 $result = $this->convert_xml_to_result($xml, 'batch.run', $params);
160
161
162 if (is_array($result) && isset($result['error_code'])) {
163 throw new FacebookRestClientException($result['error_msg'], $result['error_code']);
164 }
165
166 for($i = 0; $i < $item_count; $i++) {
167 $batch_item = $this->batch_queue[$i];
168 $batch_item_result_xml = $result[$i];
169 $batch_item_result = $this->convert_xml_to_result($batch_item_result_xml, $batch_item['m'], $batch_item['p']);
170
171 if (is_array($batch_item_result) && isset($batch_item_result['error_code'])) {
172 throw new FacebookRestClientException($batch_item_result['error_msg'], $batch_item_result['error_code']);
173 }
174 $batch_item['r'] = $batch_item_result;
175 }
176 }
177
178 public function begin_permissions_mode($permissions_apikey) {
179 $this->call_as_apikey = $permissions_apikey;
180 }
181
182 public function end_permissions_mode() {
183 $this->call_as_apikey = '';
184 }
185
186 /**
187 * Returns the session information available after current user logs in.
188 * @param string $auth_token the token returned by auth_createToken or
189 * passed back to your callback_url.
190 * @param bool $generate_session_secret whether the session returned should include a session secret
191 *
192 * @return assoc array containing session_key, uid
193 */
194 public function auth_getSession($auth_token, $generate_session_secret=false) {
195 //Check if we are in batch mode
196 if($this->batch_queue === null) {
197 $result = $this->call_method('facebook.auth.getSession',
198 array('auth_token' => $auth_token, 'generate_session_secret' => $generate_session_secret));
199 $this->session_key = $result['session_key'];
200
201 if (!empty($result['secret']) && !$generate_session_secret) {
202 // desktop apps have a special secret
203 $this->secret = $result['secret'];
204 }
205 return $result;
206 }
207 }
208
209 /**
210 * Generates a session specific secret. This is for integration with client-side API calls, such as the
211 * JS library.
212 * @error API_EC_PARAM_SESSION_KEY
213 * API_EC_PARAM_UNKNOWN
214 * @return session secret for the current promoted session
215 */
216 public function auth_promoteSession() {
217 return $this->call_method('facebook.auth.promoteSession', array());
218 }
219
220 /**
221 * Expires the session that is currently being used. If this call is successful, no further calls to the
222 * API (which require a session) can be made until a valid session is created.
223 *
224 * @return bool true if session expiration was successful, false otherwise
225 */
226 public function auth_expireSession() {
227 return $this->call_method('facebook.auth.expireSession', array());
228 }
229
230 /**
231 * Returns events according to the filters specified.
232 * @param int $uid Optional: User associated with events.
233 * A null parameter will default to the session user.
234 * @param array $eids Optional: Filter by these event ids.
235 * A null parameter will get all events for the user.
236 * @param int $start_time Optional: Filter with this UTC as lower bound.
237 * A null or zero parameter indicates no lower bound.
238 * @param int $end_time Optional: Filter with this UTC as upper bound.
239 * A null or zero parameter indicates no upper bound.
240 * @param string $rsvp_status Optional: Only show events where the given uid
241 * has this rsvp status. This only works if you have specified a value for
242 * $uid. Values are as in events.getMembers. Null indicates to ignore
243 * rsvp status when filtering.
244 * @return array of events
245 */
246 public function &events_get($uid, $eids, $start_time, $end_time, $rsvp_status) {
247 return $this->call_method('facebook.events.get',
248 array(
249 'uid' => $uid,
250 'eids' => $eids,
251 'start_time' => $start_time,
252 'end_time' => $end_time,
253 'rsvp_status' => $rsvp_status));
254 }
255
256 /**
257 * Returns membership list data associated with an event
258 * @param int $eid : event id
259 * @return assoc array of four membership lists, with keys 'attending',
260 * 'unsure', 'declined', and 'not_replied'
261 */
262 public function &events_getMembers($eid) {
263 return $this->call_method('facebook.events.getMembers',
264 array('eid' => $eid));
265 }
266
267 /**
268 * Makes an FQL query. This is a generalized way of accessing all the data
269 * in the API, as an alternative to most of the other method calls. More
270 * info at http://developers.facebook.com/documentation.php?v=1.0&doc=fql
271 * @param string $query the query to evaluate
272 * @return generalized array representing the results
273 */
274 public function &fql_query($query) {
275 return $this->call_method('facebook.fql.query',
276 array('query' => $query));
277 }
278
279 public function &feed_publishStoryToUser($title, $body,
280 $image_1=null, $image_1_link=null,
281 $image_2=null, $image_2_link=null,
282 $image_3=null, $image_3_link=null,
283 $image_4=null, $image_4_link=null) {
284 return $this->call_method('facebook.feed.publishStoryToUser',
285 array('title' => $title,
286 'body' => $body,
287 'image_1' => $image_1,
288 'image_1_link' => $image_1_link,
289 'image_2' => $image_2,
290 'image_2_link' => $image_2_link,
291 'image_3' => $image_3,
292 'image_3_link' => $image_3_link,
293 'image_4' => $image_4,
294 'image_4_link' => $image_4_link));
295 }
296
297 public function &feed_publishActionOfUser($title, $body,
298 $image_1=null, $image_1_link=null,
299 $image_2=null, $image_2_link=null,
300 $image_3=null, $image_3_link=null,
301 $image_4=null, $image_4_link=null) {
302 return $this->call_method('facebook.feed.publishActionOfUser',
303 array('title' => $title,
304 'body' => $body,
305 'image_1' => $image_1,
306 'image_1_link' => $image_1_link,
307 'image_2' => $image_2,
308 'image_2_link' => $image_2_link,
309 'image_3' => $image_3,
310 'image_3_link' => $image_3_link,
311 'image_4' => $image_4,
312 'image_4_link' => $image_4_link));
313 }
314
315 public function &feed_publishTemplatizedAction($title_template, $title_data,
316 $body_template, $body_data, $body_general,
317 $image_1=null, $image_1_link=null,
318 $image_2=null, $image_2_link=null,
319 $image_3=null, $image_3_link=null,
320 $image_4=null, $image_4_link=null,
321 $target_ids='', $page_actor_id=null) {
322 return $this->call_method('facebook.feed.publishTemplatizedAction',
323 array('title_template' => $title_template,
324 'title_data' => is_array($title_data) ? json_encode($title_data) : $title_data,
325 'body_template' => $body_template,
326 'body_data' => is_array($body_data) ? json_encode($body_data) : $body_data,
327 'body_general' => $body_general,
328 'image_1' => $image_1,
329 'image_1_link' => $image_1_link,
330 'image_2' => $image_2,
331 'image_2_link' => $image_2_link,
332 'image_3' => $image_3,
333 'image_3_link' => $image_3_link,
334 'image_4' => $image_4,
335 'image_4_link' => $image_4_link,
336 'target_ids' => $target_ids,
337 'page_actor_id' => $page_actor_id));
338 }
339
340 /**
341 * Returns whether or not pairs of users are friends.
342 * Note that the Facebook friend relationship is symmetric.
343 * @param array $uids1: array of ids (id_1, id_2,...) of some length X
344 * @param array $uids2: array of ids (id_A, id_B,...) of SAME length X
345 * @return array of uid pairs with bool, true if pair are friends, e.g.
346 * array( 0 => array('uid1' => id_1, 'uid2' => id_A, 'are_friends' => 1),
347 * 1 => array('uid1' => id_2, 'uid2' => id_B, 'are_friends' => 0)
348 * ...)
349 */
350 public function &friends_areFriends($uids1, $uids2) {
351 return $this->call_method('facebook.friends.areFriends',
352 array('uids1'=>$uids1, 'uids2'=>$uids2));
353 }
354
355 /**
356 * Returns the friends of the current session user.
357 * @return array of friends
358 */
359 public function &friends_get() {
360 if (isset($this->friends_list)) {
361 return $this->friends_list;
362 }
363 return $this->call_method('facebook.friends.get', array());
364 }
365
366 /**
367 * Returns the friends of the session user, who are also users
368 * of the calling application.
369 * @return array of friends
370 */
371 public function &friends_getAppUsers() {
372 return $this->call_method('facebook.friends.getAppUsers', array());
373 }
374
375 /**
376 * Returns groups according to the filters specified.
377 * @param int $uid Optional: User associated with groups.
378 * A null parameter will default to the session user.
379 * @param array $gids Optional: group ids to query.
380 * A null parameter will get all groups for the user.
381 * @return array of groups
382 */
383 public function &groups_get($uid, $gids) {
384 return $this->call_method('facebook.groups.get',
385 array(
386 'uid' => $uid,
387 'gids' => $gids));
388 }
389
390 /**
391 * Returns the membership list of a group
392 * @param int $gid : Group id
393 * @return assoc array of four membership lists, with keys
394 * 'members', 'admins', 'officers', and 'not_replied'
395 */
396 public function &groups_getMembers($gid) {
397 return $this->call_method('facebook.groups.getMembers',
398 array('gid' => $gid));
399 }
400
401 /**
402 * Returns cookies according to the filters specified.
403 * @param int $uid Required: User for which the cookies are needed.
404 * @param string $name Optional:
405 * A null parameter will get all cookies for the user.
406 * @return array of cookies
407 */
408 public function data_getCookies($uid, $name) {
409 return $this->call_method('facebook.data.getCookies',
410 array(
411 'uid' => $uid,
412 'name' => $name));
413 }
414
415 /**
416 * Sets cookies according to the params specified.
417 * @param int $uid Required: User for which the cookies are needed.
418 * @param string $name Required: name of the cookie
419 * @param string $value Optional if expires specified and is in the past
420 * @param int$expires Optional
421 * @param string $path Optional
422 *
423 * @return bool
424 */
425 public function data_setCookie($uid, $name, $value, $expires, $path) {
426 return $this->call_method('facebook.data.setCookie',
427 array(
428 'uid' => $uid,
429 'name' => $name,
430 'value' => $value,
431 'expires' => $expires,
432 'path' => $path));
433 }
434
435 /**
436 * Permissions API
437 */
438
439 /**
440 * Checks API-access granted by self to the specified application
441 * @param string $permissions_apikey: Required
442 *
443 * @return array: API methods/namespaces which are allowed access
444 */
445 public function permissions_checkGrantedApiAccess($permissions_apikey) {
446 return $this->call_method('facebook.permissions.checkGrantedApiAccess',
447 array(
448 'permissions_apikey' => $permissions_apikey));
449 }
450
451 /**
452 * Checks API-access granted to self by the specified application
453 * @param string $permissions_apikey: Required
454 *
455 * @return array: API methods/namespaces which are allowed access
456 */
457 public function permissions_checkAvailableApiAccess($permissions_apikey) {
458 return $this->call_method('facebook.permissions.checkAvailableApiAccess',
459 array(
460 'permissions_apikey' => $permissions_apikey));
461 }
462
463 /**
464 * Grant API-access to the specified methods/namespaces to the specified application
465 * @param string $permissions_apikey: Required
466 * @param array(string) : Optional: API methods/namespaces to be allowed
467 *
468 * @return array: API methods/namespaces which are allowed access
469 */
470 public function permissions_grantApiAccess($permissions_apikey, $method_arr) {
471 return $this->call_method('facebook.permissions.grantApiAccess',
472 array(
473 'permissions_apikey' => $permissions_apikey,
474 'method_arr' => $method_arr));
475 }
476
477 /**
478 * Revoke API-access granted to the specified application
479 * @param string $permissions_apikey: Required
480 *
481 * @return bool
482 */
483 public function permissions_revokeApiAccess($permissions_apikey) {
484 return $this->call_method('facebook.permissions.revokeApiAccess',
485 array(
486 'permissions_apikey' => $permissions_apikey));
487 }
488
489 /**
490 * Returns the outstanding notifications for the session user.
491 * @return assoc array of
492 * notification count objects for 'messages', 'pokes' and 'shares',
493 * a uid list of 'friend_requests', a gid list of 'group_invites',
494 * and an eid list of 'event_invites'
495 */
496 public function &notifications_get() {
497 return $this->call_method('facebook.notifications.get', array());
498 }
499
500 /**
501 * Sends a notification to the specified users.
502 * @return (nothing)
503 */
504 public function &notifications_send($to_ids, $notification) {
505 return $this->call_method('facebook.notifications.send',
506 array('to_ids' => $to_ids, 'notification' => $notification));
507 }
508
509 /**
510 * Sends an email to the specified user of the application.
511 * @param array $recipients : id of the recipients
512 * @param string $subject : subject of the email
513 * @param string $text : (plain text) body of the email
514 * @param string $fbml : fbml markup if you want an html version of the email
515 * @return comma separated list of successful recipients
516 */
517 public function &notifications_sendEmail($recipients, $subject, $text, $fbml) {
518 return $this->call_method('facebook.notifications.sendEmail',
519 array('recipients' => $recipients,
520 'subject' => $subject,
521 'text' => $text,
522 'fbml' => $fbml));
523 }
524
525 /**
526 * Returns the requested info fields for the requested set of pages
527 * @param array $page_ids an array of page ids
528 * @param array $fields an array of strings describing the info fields desired
529 * @param int $uid Optionally, limit results to pages of which this user is a fan.
530 * @param string type limits results to a particular type of page.
531 * @return array of pages
532 */
533 public function &pages_getInfo($page_ids, $fields, $uid, $type) {
534 return $this->call_method('facebook.pages.getInfo', array('page_ids' => $page_ids, 'fields' => $fields, 'uid' => $uid, 'type' => $type));
535 }
536
537 /**
538 * Returns true if logged in user is an admin for the passed page
539 * @param int $page_id target page id
540 * @return boolean
541 */
542 public function &pages_isAdmin($page_id) {
543 return $this->call_method('facebook.pages.isAdmin', array('page_id' => $page_id));
544 }
545
546 /**
547 * Returns whether or not the page corresponding to the current session object has the app installed
548 * @return boolean
549 */
550 public function &pages_isAppAdded() {
551 if (isset($this->added)) {
552 return $this->added;
553 }
554 return $this->call_method('facebook.pages.isAppAdded', array());
555 }
556
557 /**
558 * Returns true if logged in user is a fan for the passed page
559 * @param int $page_id target page id
560 * @param int $uid user to compare. If empty, the logged in user.
561 * @return bool
562 */
563 public function &pages_isFan($page_id, $uid) {
564 return $this->call_method('facebook.pages.isFan', array('page_id' => $page_id, 'uid' => $uid));
565 }
566
567 /**
568 * Returns photos according to the filters specified.
569 * @param int $subj_id Optional: Filter by uid of user tagged in the photos.
570 * @param int $aid Optional: Filter by an album, as returned by
571 * photos_getAlbums.
572 * @param array $pids Optional: Restrict to a list of pids
573 * Note that at least one of these parameters needs to be specified, or an
574 * error is returned.
575 * @return array of photo objects.
576 */
577 public function &photos_get($subj_id, $aid, $pids) {
578 return $this->call_method('facebook.photos.get',
579 array('subj_id' => $subj_id, 'aid' => $aid, 'pids' => $pids));
580 }
581
582 /**
583 * Returns the albums created by the given user.
584 * @param int $uid Optional: the uid of the user whose albums you want.
585 * A null value will return the albums of the session user.
586 * @param array $aids Optional: a list of aids to restrict the query.
587 * Note that at least one of the (uid, aids) parameters must be specified.
588 * @returns an array of album objects.
589 */
590 public function &photos_getAlbums($uid, $aids) {
591 return $this->call_method('facebook.photos.getAlbums',
592 array('uid' => $uid,
593 'aids' => $aids));
594 }
595
596 /**
597 * Returns the tags on all photos specified.
598 * @param string $pids : a list of pids to query
599 * @return array of photo tag objects, with include pid, subject uid,
600 * and two floating-point numbers (xcoord, ycoord) for tag pixel location
601 */
602 public function &photos_getTags($pids) {
603 return $this->call_method('facebook.photos.getTags',
604 array('pids' => $pids));
605 }
606
607
608 /**
609 * Returns the requested info fields for the requested set of users
610 * @param array $uids an array of user ids
611 * @param array $fields an array of strings describing the info fields desired
612 * @return array of users
613 */
614 public function &users_getInfo($uids, $fields) {
615 return $this->call_method('facebook.users.getInfo', array('uids' => $uids, 'fields' => $fields));
616 }
617
618 /**
619 * Returns the user corresponding to the current session object.
620 * @return integer uid
621 */
622 public function &users_getLoggedInUser() {
623 return $this->call_method('facebook.users.getLoggedInUser', array());
624 }
625
626 /**
627 * Returns whether or not the user corresponding to the current session object has the app installed
628 * @return boolean
629 */
630 public function &users_isAppAdded($uid=null) {
631 if (isset($this->added)) {
632 return $this->added;
633 }
634 return $this->call_method('facebook.users.isAppAdded', array('uid' => $uid));
635 }
636
637 /**
638 * Sets the FBML for the profile of the user attached to this session
639 * @param string $markup The FBML that describes the profile presence of this app for the user
640 * @param int $uid The user
641 * @param string $profile Profile FBML
642 * @param string $profile_action Profile action FBML
643 * @param string $mobile_profile Mobile profile FBML
644 * @return array A list of strings describing any compile errors for the submitted FBML
645 */
646 function profile_setFBML($markup, $uid = null, $profile='', $profile_action='', $mobile_profile='') {
647 return $this->call_method('facebook.profile.setFBML', array('markup' => $markup,
648 'uid' => $uid,
649 'profile' => $profile,
650 'profile_action' => $profile_action,
651 'mobile_profile' => $mobile_profile));
652 }
653
654 public function &profile_getFBML($uid) {
655 return $this->call_method('facebook.profile.getFBML', array('uid' => $uid));
656 }
657
658 public function &fbml_refreshImgSrc($url) {
659 return $this->call_method('facebook.fbml.refreshImgSrc', array('url' => $url));
660 }
661
662 public function &fbml_refreshRefUrl($url) {
663 return $this->call_method('facebook.fbml.refreshRefUrl', array('url' => $url));
664 }
665
666 public function &fbml_setRefHandle($handle, $fbml) {
667 return $this->call_method('facebook.fbml.setRefHandle', array('handle' => $handle, 'fbml' => $fbml));
668 }
669
670 /**
671 * Get all the marketplace categories
672 *
673 * @return array A list of category names
674 */
675 function marketplace_getCategories() {
676 return $this->call_method('facebook.marketplace.getCategories', array());
677 }
678
679 /**
680 * Get all the marketplace subcategories for a particular category
681 *
682 * @param category The category for which we are pulling subcategories
683 * @return array A list of subcategory names
684 */
685 function marketplace_getSubCategories($category) {
686 return $this->call_method('facebook.marketplace.getSubCategories', array('category' => $category));
687 }
688
689 /**
690 * Get listings by either listing_id or user
691 *
692 * @param listing_ids An array of listing_ids (optional)
693 * @param uids An array of user ids (optional)
694 * @return array The data for matched listings
695 */
696 function marketplace_getListings($listing_ids, $uids) {
697 return $this->call_method('facebook.marketplace.getListings', array('listing_ids' => $listing_ids, 'uids' => $uids));
698 }
699
700 /**
701 * Search for Marketplace listings. All arguments are optional, though at least
702 * one must be filled out to retrieve results.
703 *
704 * @param category The category in which to search (optional)
705 * @param subcategory The subcategory in which to search (optional)
706 * @param query A query string (optional)
707 * @return array The data for matched listings
708 */
709 function marketplace_search($category, $subcategory, $query) {
710 return $this->call_method('facebook.marketplace.search', array('category' => $category, 'subcategory' => $subcategory, 'query' => $query));
711 }
712
713 /**
714 * Remove a listing from Marketplace
715 *
716 * @param listing_id The id of the listing to be removed
717 * @param status 'SUCCESS', 'NOT_SUCCESS', or 'DEFAULT'
718 * @return bool True on success
719 */
720 function marketplace_removeListing($listing_id, $status='DEFAULT', $uid=null) {
721 return $this->call_method('facebook.marketplace.removeListing',
722 array('listing_id'=>$listing_id,
723 'status'=>$status,
724 'uid' => $uid));
725 }
726
727 /**
728 * Create/modify a Marketplace listing for the loggedinuser
729 *
730 * @param int listing_id The id of a listing to be modified, 0 for a new listing.
731 * @param show_on_profile bool Should we show this listing on the user's profile
732 * @param listing_attrs array An array of the listing data
733 * @return int The listing_id (unchanged if modifying an existing listing)
734 */
735 function marketplace_createListing($listing_id, $show_on_profile, $attrs, $uid=null) {
736 return $this->call_method('facebook.marketplace.createListing',
737 array('listing_id'=>$listing_id,
738 'show_on_profile'=>$show_on_profile,
739 'listing_attrs'=>json_encode($attrs),
740 'uid' => $uid));
741 }
742
743
744 /////////////////////////////////////////////////////////////////////////////
745 // Data Store API
746
747 /**
748 * Set a user preference.
749 *
750 * @param pref_id preference identifier (0-200)
751 * @param value preferece's value
752 * @error
753 * API_EC_DATA_DATABASE_ERROR
754 * API_EC_PARAM
755 * API_EC_DATA_QUOTA_EXCEEDED
756 * API_EC_DATA_UNKNOWN_ERROR
757 */
758 public function &data_setUserPreference($pref_id, $value) {
759 return $this->call_method
760 ('facebook.data.setUserPreference',
761 array('pref_id' => $pref_id,
762 'value' => $value));
763 }
764
765 /**
766 * Set a user's all preferences for this application.
767 *
768 * @param values preferece values in an associative arrays
769 * @param replace whether to replace all existing preferences or
770 * merge into them.
771 * @error
772 * API_EC_DATA_DATABASE_ERROR
773 * API_EC_PARAM
774 * API_EC_DATA_QUOTA_EXCEEDED
775 * API_EC_DATA_UNKNOWN_ERROR
776 */
777 public function &data_setUserPreferences($values, $replace = false) {
778 return $this->call_method
779 ('facebook.data.setUserPreferences',
780 array('values' => json_encode($values),
781 'replace' => $replace));
782 }
783
784 /**
785 * Get a user preference.
786 *
787 * @param pref_id preference identifier (0-200)
788 * @return preference's value
789 * @error
790 * API_EC_DATA_DATABASE_ERROR
791 * API_EC_PARAM
792 * API_EC_DATA_QUOTA_EXCEEDED
793 * API_EC_DATA_UNKNOWN_ERROR
794 */
795 public function &data_getUserPreference($pref_id) {
796 return $this->call_method
797 ('facebook.data.getUserPreference',
798 array('pref_id' => $pref_id));
799 }
800
801 /**
802 * Get a user preference.
803 *
804 * @return preference values
805 * @error
806 * API_EC_DATA_DATABASE_ERROR
807 * API_EC_DATA_QUOTA_EXCEEDED
808 * API_EC_DATA_UNKNOWN_ERROR
809 */
810 public function &data_getUserPreferences() {
811 return $this->call_method
812 ('facebook.data.getUserPreferences',
813 array());
814 }
815
816 /**
817 * Create a new object type.
818 *
819 * @param name object type's name
820 * @error
821 * API_EC_DATA_DATABASE_ERROR
822 * API_EC_DATA_OBJECT_ALREADY_EXISTS
823 * API_EC_PARAM
824 * API_EC_PERMISSION
825 * API_EC_DATA_INVALID_OPERATION
826 * API_EC_DATA_QUOTA_EXCEEDED
827 * API_EC_DATA_UNKNOWN_ERROR
828 */
829 public function &data_createObjectType($name) {
830 return $this->call_method
831 ('facebook.data.createObjectType',
832 array('name' => $name));
833 }
834
835 /**
836 * Delete an object type.
837 *
838 * @param obj_type object type's name
839 * @error
840 * API_EC_DATA_DATABASE_ERROR
841 * API_EC_DATA_OBJECT_NOT_FOUND
842 * API_EC_PARAM
843 * API_EC_PERMISSION
844 * API_EC_DATA_INVALID_OPERATION
845 * API_EC_DATA_QUOTA_EXCEEDED
846 * API_EC_DATA_UNKNOWN_ERROR
847 */
848 public function &data_dropObjectType($obj_type) {
849 return $this->call_method
850 ('facebook.data.dropObjectType',
851 array('obj_type' => $obj_type));
852 }
853
854 /**
855 * Rename an object type.
856 *
857 * @param obj_type object type's name
858 * @param new_name new object type's name
859 * @error
860 * API_EC_DATA_DATABASE_ERROR
861 * API_EC_DATA_OBJECT_NOT_FOUND
862 * API_EC_DATA_OBJECT_ALREADY_EXISTS
863 * API_EC_PARAM
864 * API_EC_PERMISSION
865 * API_EC_DATA_INVALID_OPERATION
866 * API_EC_DATA_QUOTA_EXCEEDED
867 * API_EC_DATA_UNKNOWN_ERROR
868 */
869 public function &data_renameObjectType($obj_type, $new_name) {
870 return $this->call_method
871 ('facebook.data.renameObjectType',
872 array('obj_type' => $obj_type,
873 'new_name' => $new_name));
874 }
875
876 /**
877 * Add a new property to an object type.
878 *
879 * @param obj_type object type's name
880 * @param prop_name name of the property to add
881 * @param prop_type 1: integer; 2: string; 3: text blob
882 * @error
883 * API_EC_DATA_DATABASE_ERROR
884 * API_EC_DATA_OBJECT_ALREADY_EXISTS
885 * API_EC_PARAM
886 * API_EC_PERMISSION
887 * API_EC_DATA_INVALID_OPERATION
888 * API_EC_DATA_QUOTA_EXCEEDED
889 * API_EC_DATA_UNKNOWN_ERROR
890 */
891 public function &data_defineObjectProperty($obj_type, $prop_name, $prop_type) {
892 return $this->call_method
893 ('facebook.data.defineObjectProperty',
894 array('obj_type' => $obj_type,
895 'prop_name' => $prop_name,
896 'prop_type' => $prop_type));
897 }
898
899 /**
900 * Remove a previously defined property from an object type.
901 *
902 * @param obj_type object type's name
903 * @param prop_name name of the property to remove
904 * @error
905 * API_EC_DATA_DATABASE_ERROR
906 * API_EC_DATA_OBJECT_NOT_FOUND
907 * API_EC_PARAM
908 * API_EC_PERMISSION
909 * API_EC_DATA_INVALID_OPERATION
910 * API_EC_DATA_QUOTA_EXCEEDED
911 * API_EC_DATA_UNKNOWN_ERROR
912 */
913 public function &data_undefineObjectProperty($obj_type, $prop_name) {
914 return $this->call_method
915 ('facebook.data.undefineObjectProperty',
916 array('obj_type' => $obj_type,
917 'prop_name' => $prop_name));
918 }
919
920 /**
921 * Rename a previously defined property of an object type.
922 *
923 * @param obj_type object type's name
924 * @param prop_name name of the property to rename
925 * @param new_name new name to use
926 * @error
927 * API_EC_DATA_DATABASE_ERROR
928 * API_EC_DATA_OBJECT_NOT_FOUND
929 * API_EC_DATA_OBJECT_ALREADY_EXISTS
930 * API_EC_PARAM
931 * API_EC_PERMISSION
932 * API_EC_DATA_INVALID_OPERATION
933 * API_EC_DATA_QUOTA_EXCEEDED
934 * API_EC_DATA_UNKNOWN_ERROR
935 */
936 public function &data_renameObjectProperty($obj_type, $prop_name,
937 $new_name) {
938 return $this->call_method
939 ('facebook.data.renameObjectProperty',
940 array('obj_type' => $obj_type,
941 'prop_name' => $prop_name,
942 'new_name' => $new_name));
943 }
944
945 /**
946 * Retrieve a list of all object types that have defined for the application.
947 *
948 * @return a list of object type names
949 * @error
950 * API_EC_DATA_DATABASE_ERROR
951 * API_EC_PERMISSION
952 * API_EC_DATA_QUOTA_EXCEEDED
953 * API_EC_DATA_UNKNOWN_ERROR
954 */
955 public function &data_getObjectTypes() {
956 return $this->call_method
957 ('facebook.data.getObjectTypes',
958 array());
959 }
960
961 /**
962 * Get definitions of all properties of an object type.
963 *
964 * @param obj_type object type's name
965 * @return pairs of property name and property types
966 * @error
967 * API_EC_DATA_DATABASE_ERROR
968 * API_EC_PARAM
969 * API_EC_PERMISSION
970 * API_EC_DATA_OBJECT_NOT_FOUND
971 * API_EC_DATA_QUOTA_EXCEEDED
972 * API_EC_DATA_UNKNOWN_ERROR
973 */
974 public function &data_getObjectType($obj_type) {
975 return $this->call_method
976 ('facebook.data.getObjectType',
977 array('obj_type' => $obj_type));
978 }
979
980 /**
981 * Create a new object.
982 *
983 * @param obj_type object type's name
984 * @param properties (optional) properties to set initially
985 * @return newly created object's id
986 * @error
987 * API_EC_DATA_DATABASE_ERROR
988 * API_EC_PARAM
989 * API_EC_PERMISSION
990 * API_EC_DATA_INVALID_OPERATION
991 * API_EC_DATA_QUOTA_EXCEEDED
992 * API_EC_DATA_UNKNOWN_ERROR
993 */
994 public function &data_createObject($obj_type, $properties = null) {
995 return $this->call_method
996 ('facebook.data.createObject',
997 array('obj_type' => $obj_type,
998 'properties' => json_encode($properties)));
999 }
1000
1001 /**
1002 * Update an existing object.
1003 *
1004 * @param obj_id object's id
1005 * @param properties new properties
1006 * @param replace true for replacing existing properties; false for merging
1007 * @error
1008 * API_EC_DATA_DATABASE_ERROR
1009 * API_EC_DATA_OBJECT_NOT_FOUND
1010 * API_EC_PARAM
1011 * API_EC_PERMISSION
1012 * API_EC_DATA_INVALID_OPERATION
1013 * API_EC_DATA_QUOTA_EXCEEDED
1014 * API_EC_DATA_UNKNOWN_ERROR
1015 */
1016 public function &data_updateObject($obj_id, $properties, $replace = false) {
1017 return $this->call_method
1018 ('facebook.data.updateObject',
1019 array('obj_id' => $obj_id,
1020 'properties' => json_encode($properties),
1021 'replace' => $replace));
1022 }
1023
1024 /**
1025 * Delete an existing object.
1026 *
1027 * @param obj_id object's id
1028 * @error
1029 * API_EC_DATA_DATABASE_ERROR
1030 * API_EC_DATA_OBJECT_NOT_FOUND
1031 * API_EC_PARAM
1032 * API_EC_PERMISSION
1033 * API_EC_DATA_INVALID_OPERATION
1034 * API_EC_DATA_QUOTA_EXCEEDED
1035 * API_EC_DATA_UNKNOWN_ERROR
1036 */
1037 public function &data_deleteObject($obj_id) {
1038 return $this->call_method
1039 ('facebook.data.deleteObject',
1040 array('obj_id' => $obj_id));
1041 }
1042
1043 /**
1044 * Delete a list of objects.
1045 *
1046 * @param obj_ids objects to delete
1047 * @error
1048 * API_EC_DATA_DATABASE_ERROR
1049 * API_EC_PARAM
1050 * API_EC_PERMISSION
1051 * API_EC_DATA_INVALID_OPERATION
1052 * API_EC_DATA_QUOTA_EXCEEDED
1053 * API_EC_DATA_UNKNOWN_ERROR
1054 */
1055 public function &data_deleteObjects($obj_ids) {
1056 return $this->call_method
1057 ('facebook.data.deleteObjects',
1058 array('obj_ids' => json_encode($obj_ids)));
1059 }
1060
1061 /**
1062 * Get a single property value of an object.
1063 *
1064 * @param obj_id object's id
1065 * @param prop_name individual property's name
1066 * @return individual property's value
1067 * @error
1068 * API_EC_DATA_DATABASE_ERROR
1069 * API_EC_DATA_OBJECT_NOT_FOUND
1070 * API_EC_PARAM
1071 * API_EC_PERMISSION
1072 * API_EC_DATA_INVALID_OPERATION
1073 * API_EC_DATA_QUOTA_EXCEEDED
1074 * API_EC_DATA_UNKNOWN_ERROR
1075 */
1076 public function &data_getObjectProperty($obj_id, $prop_name) {
1077 return $this->call_method
1078 ('facebook.data.getObjectProperty',
1079 array('obj_id' => $obj_id,
1080 'prop_name' => $prop_name));
1081 }
1082
1083 /**
1084 * Get properties of an object.
1085 *
1086 * @param obj_id object's id
1087 * @param prop_names (optional) properties to return; null for all.
1088 * @return specified properties of an object
1089 * @error
1090 * API_EC_DATA_DATABASE_ERROR
1091 * API_EC_DATA_OBJECT_NOT_FOUND
1092 * API_EC_PARAM
1093 * API_EC_PERMISSION
1094 * API_EC_DATA_INVALID_OPERATION
1095 * API_EC_DATA_QUOTA_EXCEEDED
1096 * API_EC_DATA_UNKNOWN_ERROR
1097 */
1098 public function &data_getObject($obj_id, $prop_names = null) {
1099 return $this->call_method
1100 ('facebook.data.getObject',
1101 array('obj_id' => $obj_id,
1102 'prop_names' => json_encode($prop_names)));
1103 }
1104
1105 /**
1106 * Get properties of a list of objects.
1107 *
1108 * @param obj_ids object ids
1109 * @param prop_names (optional) properties to return; null for all.
1110 * @return specified properties of an object
1111 * @error
1112 * API_EC_DATA_DATABASE_ERROR
1113 * API_EC_DATA_OBJECT_NOT_FOUND
1114 * API_EC_PARAM
1115 * API_EC_PERMISSION
1116 * API_EC_DATA_INVALID_OPERATION
1117 * API_EC_DATA_QUOTA_EXCEEDED
1118 * API_EC_DATA_UNKNOWN_ERROR
1119 */
1120 public function &data_getObjects($obj_ids, $prop_names = null) {
1121 return $this->call_method
1122 ('facebook.data.getObjects',
1123 array('obj_ids' => json_encode($obj_ids),
1124 'prop_names' => json_encode($prop_names)));
1125 }
1126
1127 /**
1128 * Set a single property value of an object.
1129 *
1130 * @param obj_id object's id
1131 * @param prop_name individual property's name
1132 * @param prop_value new value to set
1133 * @error
1134 * API_EC_DATA_DATABASE_ERROR
1135 * API_EC_DATA_OBJECT_NOT_FOUND
1136 * API_EC_PARAM
1137 * API_EC_PERMISSION
1138 * API_EC_DATA_INVALID_OPERATION
1139 * API_EC_DATA_QUOTA_EXCEEDED
1140 * API_EC_DATA_UNKNOWN_ERROR
1141 */
1142 public function &data_setObjectProperty($obj_id, $prop_name,
1143 $prop_value) {
1144 return $this->call_method
1145 ('facebook.data.setObjectProperty',
1146 array('obj_id' => $obj_id,
1147 'prop_name' => $prop_name,
1148 'prop_value' => $prop_value));
1149 }
1150
1151 /**
1152 * Read hash value by key.
1153 *
1154 * @param obj_type object type's name
1155 * @param key hash key
1156 * @param prop_name (optional) individual property's name
1157 * @return hash value
1158 * @error
1159 * API_EC_DATA_DATABASE_ERROR
1160 * API_EC_PARAM
1161 * API_EC_PERMISSION
1162 * API_EC_DATA_INVALID_OPERATION
1163 * API_EC_DATA_QUOTA_EXCEEDED
1164 * API_EC_DATA_UNKNOWN_ERROR
1165 */
1166 public function &data_getHashValue($obj_type, $key, $prop_name = null) {
1167 return $this->call_method
1168 ('facebook.data.getHashValue',
1169 array('obj_type' => $obj_type,
1170 'key' => $key,
1171 'prop_name' => $prop_name));
1172 }
1173
1174 /**
1175 * Write hash value by key.
1176 *
1177 * @param obj_type object type's name
1178 * @param key hash key
1179 * @param value hash value
1180 * @param prop_name (optional) individual property's name
1181 * @error
1182 * API_EC_DATA_DATABASE_ERROR
1183 * API_EC_PARAM
1184 * API_EC_PERMISSION
1185 * API_EC_DATA_INVALID_OPERATION
1186 * API_EC_DATA_QUOTA_EXCEEDED
1187 * API_EC_DATA_UNKNOWN_ERROR
1188 */
1189 public function &data_setHashValue($obj_type, $key, $value, $prop_name = null) {
1190 return $this->call_method
1191 ('facebook.data.setHashValue',
1192 array('obj_type' => $obj_type,
1193 'key' => $key,
1194 'value' => $value,
1195 'prop_name' => $prop_name));
1196 }
1197
1198 /**
1199 * Increase a hash value by specified increment atomically.
1200 *
1201 * @param obj_type object type's name
1202 * @param key hash key
1203 * @param prop_name individual property's name
1204 * @param increment (optional) default is 1
1205 * @return incremented hash value
1206 * @error
1207 * API_EC_DATA_DATABASE_ERROR
1208 * API_EC_PARAM
1209 * API_EC_PERMISSION
1210 * API_EC_DATA_INVALID_OPERATION
1211 * API_EC_DATA_QUOTA_EXCEEDED
1212 * API_EC_DATA_UNKNOWN_ERROR
1213 */
1214 public function &data_incHashValue($obj_type, $key, $prop_name, $increment = 1) {
1215 return $this->call_method
1216 ('facebook.data.incHashValue',
1217 array('obj_type' => $obj_type,
1218 'key' => $key,
1219 'prop_name' => $prop_name,
1220 'increment' => $increment));
1221 }
1222
1223 /**
1224 * Remove a hash key and its values.
1225 *
1226 * @param obj_type object type's name
1227 * @param key hash key
1228 * @error
1229 * API_EC_DATA_DATABASE_ERROR
1230 * API_EC_PARAM
1231 * API_EC_PERMISSION
1232 * API_EC_DATA_INVALID_OPERATION
1233 * API_EC_DATA_QUOTA_EXCEEDED
1234 * API_EC_DATA_UNKNOWN_ERROR
1235 */
1236 public function &data_removeHashKey($obj_type, $key) {
1237 return $this->call_method
1238 ('facebook.data.removeHashKey',
1239 array('obj_type' => $obj_type,
1240 'key' => $key));
1241 }
1242
1243 /**
1244 * Remove hash keys and their values.
1245 *
1246 * @param obj_type object type's name
1247 * @param keys hash keys
1248 * @error
1249 * API_EC_DATA_DATABASE_ERROR
1250 * API_EC_PARAM
1251 * API_EC_PERMISSION
1252 * API_EC_DATA_INVALID_OPERATION
1253 * API_EC_DATA_QUOTA_EXCEEDED
1254 * API_EC_DATA_UNKNOWN_ERROR
1255 */
1256 public function &data_removeHashKeys($obj_type, $keys) {
1257 return $this->call_method
1258 ('facebook.data.removeHashKeys',
1259 array('obj_type' => $obj_type,
1260 'keys' => json_encode($keys)));
1261 }
1262
1263
1264 /**
1265 * Define an object association.
1266 *
1267 * @param name name of this association
1268 * @param assoc_type 1: one-way 2: two-way symmetric 3: two-way asymmetric
1269 * @param assoc_info1 needed info about first object type
1270 * @param assoc_info2 needed info about second object type
1271 * @param inverse (optional) name of reverse association
1272 * @error
1273 * API_EC_DATA_DATABASE_ERROR
1274 * API_EC_DATA_OBJECT_ALREADY_EXISTS
1275 * API_EC_PARAM
1276 * API_EC_PERMISSION
1277 * API_EC_DATA_INVALID_OPERATION
1278 * API_EC_DATA_QUOTA_EXCEEDED
1279 * API_EC_DATA_UNKNOWN_ERROR
1280 */
1281 public function &data_defineAssociation($name, $assoc_type, $assoc_info1,
1282 $assoc_info2, $inverse = null) {
1283 return $this->call_method
1284 ('facebook.data.defineAssociation',
1285 array('name' => $name,
1286 'assoc_type' => $assoc_type,
1287 'assoc_info1' => json_encode($assoc_info1),
1288 'assoc_info2' => json_encode($assoc_info2),
1289 'inverse' => $inverse));
1290 }
1291
1292 /**
1293 * Undefine an object association.
1294 *
1295 * @param name name of this association
1296 * @error
1297 * API_EC_DATA_DATABASE_ERROR
1298 * API_EC_DATA_OBJECT_NOT_FOUND
1299 * API_EC_PARAM
1300 * API_EC_PERMISSION
1301 * API_EC_DATA_INVALID_OPERATION
1302 * API_EC_DATA_QUOTA_EXCEEDED
1303 * API_EC_DATA_UNKNOWN_ERROR
1304 */
1305 public function &data_undefineAssociation($name) {
1306 return $this->call_method
1307 ('facebook.data.undefineAssociation',
1308 array('name' => $name));
1309 }
1310
1311 /**
1312 * Rename an object association or aliases.
1313 *
1314 * @param name name of this association
1315 * @param new_name (optional) new name of this association
1316 * @param new_alias1 (optional) new alias for object type 1
1317 * @param new_alias2 (optional) new alias for object type 2
1318 * @error
1319 * API_EC_DATA_DATABASE_ERROR
1320 * API_EC_DATA_OBJECT_ALREADY_EXISTS
1321 * API_EC_DATA_OBJECT_NOT_FOUND
1322 * API_EC_PARAM
1323 * API_EC_PERMISSION
1324 * API_EC_DATA_INVALID_OPERATION
1325 * API_EC_DATA_QUOTA_EXCEEDED
1326 * API_EC_DATA_UNKNOWN_ERROR
1327 */
1328 public function &data_renameAssociation($name, $new_name, $new_alias1 = null,
1329 $new_alias2 = null) {
1330 return $this->call_method
1331 ('facebook.data.renameAssociation',
1332 array('name' => $name,
1333 'new_name' => $new_name,
1334 'new_alias1' => $new_alias1,
1335 'new_alias2' => $new_alias2));
1336 }
1337
1338 /**
1339 * Get definition of an object association.
1340 *
1341 * @param name name of this association
1342 * @return specified association
1343 * @error
1344 * API_EC_DATA_DATABASE_ERROR
1345 * API_EC_DATA_OBJECT_NOT_FOUND
1346 * API_EC_PARAM
1347 * API_EC_PERMISSION
1348 * API_EC_DATA_QUOTA_EXCEEDED
1349 * API_EC_DATA_UNKNOWN_ERROR
1350 */
1351 public function &data_getAssociationDefinition($name) {
1352 return $this->call_method
1353 ('facebook.data.getAssociationDefinition',
1354 array('name' => $name));
1355 }
1356
1357 /**
1358 * Get definition of all associations.
1359 *
1360 * @return all defined associations
1361 * @error
1362 * API_EC_DATA_DATABASE_ERROR
1363 * API_EC_PERMISSION
1364 * API_EC_DATA_QUOTA_EXCEEDED
1365 * API_EC_DATA_UNKNOWN_ERROR
1366 */
1367 public function &data_getAssociationDefinitions() {
1368 return $this->call_method
1369 ('facebook.data.getAssociationDefinitions',
1370 array());
1371 }
1372
1373 /**
1374 * Create or modify an association between two objects.
1375 *
1376 * @param name name of association
1377 * @param obj_id1 id of first object
1378 * @param obj_id2 id of second object
1379 * @param data (optional) extra string data to store
1380 * @param assoc_time (optional) extra time data; default to creation time
1381 * @error
1382 * API_EC_DATA_DATABASE_ERROR
1383 * API_EC_PARAM
1384 * API_EC_PERMISSION
1385 * API_EC_DATA_INVALID_OPERATION
1386 * API_EC_DATA_QUOTA_EXCEEDED
1387 * API_EC_DATA_UNKNOWN_ERROR
1388 */
1389 public function &data_setAssociation($name, $obj_id1, $obj_id2, $data = null,
1390 $assoc_time = null) {
1391 return $this->call_method
1392 ('facebook.data.setAssociation',
1393 array('name' => $name,
1394 'obj_id1' => $obj_id1,
1395 'obj_id2' => $obj_id2,
1396 'data' => $data,
1397 'assoc_time' => $assoc_time));
1398 }
1399
1400 /**
1401 * Create or modify associations between objects.
1402 *
1403 * @param assocs associations to set
1404 * @param name (optional) name of association
1405 * @error
1406 * API_EC_DATA_DATABASE_ERROR
1407 * API_EC_PARAM
1408 * API_EC_PERMISSION
1409 * API_EC_DATA_INVALID_OPERATION
1410 * API_EC_DATA_QUOTA_EXCEEDED
1411 * API_EC_DATA_UNKNOWN_ERROR
1412 */
1413 public function &data_setAssociations($assocs, $name = null) {
1414 return $this->call_method
1415 ('facebook.data.setAssociations',
1416 array('assocs' => json_encode($assocs),
1417 'name' => $name));
1418 }
1419
1420 /**
1421 * Remove an association between two objects.
1422 *
1423 * @param name name of association
1424 * @param obj_id1 id of first object
1425 * @param obj_id2 id of second object
1426 * @error
1427 * API_EC_DATA_DATABASE_ERROR
1428 * API_EC_DATA_OBJECT_NOT_FOUND
1429 * API_EC_PARAM
1430 * API_EC_PERMISSION
1431 * API_EC_DATA_QUOTA_EXCEEDED
1432 * API_EC_DATA_UNKNOWN_ERROR
1433 */
1434 public function &data_removeAssociation($name, $obj_id1, $obj_id2) {
1435 return $this->call_method
1436 ('facebook.data.removeAssociation',
1437 array('name' => $name,
1438 'obj_id1' => $obj_id1,
1439 'obj_id2' => $obj_id2));
1440 }
1441
1442 /**
1443 * Remove associations between objects by specifying pairs of object ids.
1444 *
1445 * @param assocs associations to remove
1446 * @param name (optional) name of association
1447 * @error
1448 * API_EC_DATA_DATABASE_ERROR
1449 * API_EC_DATA_OBJECT_NOT_FOUND
1450 * API_EC_PARAM
1451 * API_EC_PERMISSION
1452 * API_EC_DATA_QUOTA_EXCEEDED
1453 * API_EC_DATA_UNKNOWN_ERROR
1454 */
1455 public function &data_removeAssociations($assocs, $name = null) {
1456 return $this->call_method
1457 ('facebook.data.removeAssociations',
1458 array('assocs' => json_encode($assocs),
1459 'name' => $name));
1460 }
1461
1462 /**
1463 * Remove associations between objects by specifying one object id.
1464 *
1465 * @param name name of association
1466 * @param obj_id who's association to remove
1467 * @error
1468 * API_EC_DATA_DATABASE_ERROR
1469 * API_EC_DATA_OBJECT_NOT_FOUND
1470 * API_EC_PARAM
1471 * API_EC_PERMISSION
1472 * API_EC_DATA_INVALID_OPERATION
1473 * API_EC_DATA_QUOTA_EXCEEDED
1474 * API_EC_DATA_UNKNOWN_ERROR
1475 */
1476 public function &data_removeAssociatedObjects($name, $obj_id) {
1477 return $this->call_method
1478 ('facebook.data.removeAssociatedObjects',
1479 array('name' => $name,
1480 'obj_id' => $obj_id));
1481 }
1482
1483 /**
1484 * Retrieve a list of associated objects.
1485 *
1486 * @param name name of association
1487 * @param obj_id who's association to retrieve
1488 * @param no_data only return object ids
1489 * @return associated objects
1490 * @error
1491 * API_EC_DATA_DATABASE_ERROR
1492 * API_EC_DATA_OBJECT_NOT_FOUND
1493 * API_EC_PARAM
1494 * API_EC_PERMISSION
1495 * API_EC_DATA_INVALID_OPERATION
1496 * API_EC_DATA_QUOTA_EXCEEDED
1497 * API_EC_DATA_UNKNOWN_ERROR
1498 */
1499 public function &data_getAssociatedObjects($name, $obj_id, $no_data = true) {
1500 return $this->call_method
1501 ('facebook.data.getAssociatedObjects',
1502 array('name' => $name,
1503 'obj_id' => $obj_id,
1504 'no_data' => $no_data));
1505 }
1506
1507 /**
1508 * Count associated objects.
1509 *
1510 * @param name name of association
1511 * @param obj_id who's association to retrieve
1512 * @return associated object's count
1513 * @error
1514 * API_EC_DATA_DATABASE_ERROR
1515 * API_EC_DATA_OBJECT_NOT_FOUND
1516 * API_EC_PARAM
1517 * API_EC_PERMISSION
1518 * API_EC_DATA_INVALID_OPERATION
1519 * API_EC_DATA_QUOTA_EXCEEDED
1520 * API_EC_DATA_UNKNOWN_ERROR
1521 */
1522 public function &data_getAssociatedObjectCount($name, $obj_id) {
1523 return $this->call_method
1524 ('facebook.data.getAssociatedObjectCount',
1525 array('name' => $name,
1526 'obj_id' => $obj_id));
1527 }
1528
1529 /**
1530 * Get a list of associated object counts.
1531 *
1532 * @param name name of association
1533 * @param obj_ids whose association to retrieve
1534 * @return associated object counts
1535 * @error
1536 * API_EC_DATA_DATABASE_ERROR
1537 * API_EC_DATA_OBJECT_NOT_FOUND
1538 * API_EC_PARAM
1539 * API_EC_PERMISSION
1540 * API_EC_DATA_INVALID_OPERATION
1541 * API_EC_DATA_QUOTA_EXCEEDED
1542 * API_EC_DATA_UNKNOWN_ERROR
1543 */
1544 public function &data_getAssociatedObjectCounts($name, $obj_ids) {
1545 return $this->call_method
1546 ('facebook.data.getAssociatedObjectCounts',
1547 array('name' => $name,
1548 'obj_ids' => json_encode($obj_ids)));
1549 }
1550
1551 /**
1552 * Find all associations between two objects.
1553 *
1554 * @param obj_id1 id of first object
1555 * @param obj_id2 id of second object
1556 * @param no_data only return association names without data
1557 * @return all associations between objects
1558 * @error
1559 * API_EC_DATA_DATABASE_ERROR
1560 * API_EC_PARAM
1561 * API_EC_PERMISSION
1562 * API_EC_DATA_QUOTA_EXCEEDED
1563 * API_EC_DATA_UNKNOWN_ERROR
1564 */
1565 public function &data_getAssociations($obj_id1, $obj_id2, $no_data = true) {
1566 return $this->call_method
1567 ('facebook.data.getAssociations',
1568 array('obj_id1' => $obj_id1,
1569 'obj_id2' => $obj_id2,
1570 'no_data' => $no_data));
1571 }
1572
1573 /**
1574 * Get the properties that you have set for an app.
1575 *
1576 * @param properties list of properties names to fetch
1577 * @return a map from property name to value
1578 */
1579 public function admin_getAppProperties($properties) {
1580 return json_decode($this->call_method
1581 ('facebook.admin.getAppProperties',
1582 array('properties' => json_encode($properties))), true);
1583 }
1584
1585 /**
1586 * Set properties for an app.
1587 *
1588 * @param properties a map from property names to values
1589 * @return true on success
1590 */
1591 public function admin_setAppProperties($properties) {
1592 return $this->call_method
1593 ('facebook.admin.setAppProperties',
1594 array('properties' => json_encode($properties)));
1595 }
1596
1597 /**
1598 * Returns the allocation limit value for a specified integration point name
1599 * Integration point names are defined in lib/api/karma/constants.php in the limit_map
1600 * @param string $integration_point_name
1601 * @return integration point allocation value
1602 */
1603 public function &admin_getAllocation($integration_point_name) {
1604 return $this->call_method('facebook.admin.getAllocation', array('integration_point_name' => $integration_point_name));
1605 }
1606
1607 /**
1608 * Returns values for the specified daily metrics for the current
1609 * application, in the given date range (inclusive).
1610 *
1611 * @param start_date unix time for the start of the date range
1612 * @param end_date unix time for the end of the date range
1613 * @param metrics list of daily metrics to look up
1614 * @return a list of the values for those metrics
1615 */
1616 public function &admin_getDailyMetrics($start_date, $end_date, $metrics) {
1617 return $this->call_method('admin.getDailyMetrics',
1618 array('start_date' => $start_date,
1619 'end_date' => $end_date,
1620 'metrics' => json_encode($metrics)));
1621 }
1622
1623 /**
1624 * Returns values for the specified metrics for the current
1625 * application, in the given time range. The metrics are collected
1626 * for fixed-length periods, and the times represent midnight at
1627 * the end of each period.
1628 *
1629 * @param start_time unix time for the start of the range
1630 * @param end_time unix time for the end of the range
1631 * @param period number of seconds in the desired period
1632 * @param metrics list of metrics to look up
1633 * @return a list of the values for those metrics
1634 */
1635 public function &admin_getMetrics($start_time, $end_time, $period, $metrics) {
1636 return $this->call_method('admin.getMetrics',
1637 array('start_time' => $start_time,
1638 'end_time' => $end_time,
1639 'period' => $period,
1640 'metrics' => json_encode($metrics)));
1641 }
1642
1643
1644
1645 /* UTILITY FUNCTIONS */
1646
1647 public function & call_method($method, $params) {
1648
1649 //Check if we are in batch mode
1650 if($this->batch_queue === null) {
1651 if ($this->call_as_apikey) {
1652 $params['call_as_apikey'] = $this->call_as_apikey;
1653 }
1654 if ($this->json)
1655 {
1656 $json = $this->post_request($method, $params);
1657 # XXX: silly facebook with its invalid JSON
1658 $valid = preg_match('/^[\[{].*[\]}]$/', $json);
1659 $array = json_decode($valid ? $json : "[$json]", true);
1660 $result = $valid ? $array : $array[0];
1661 } else {
1662 $xml = $this->post_request($method, $params);
1663 $result = $this->convert_xml_to_result($xml, $method, $params);
1664 }
1665 if (is_array($result) && isset($result['error_code'])) {
1666 throw new FacebookRestClientException($result['error_msg'], $result['error_code']);
1667 }
1668 }
1669 else {
1670 $result = null;
1671 $batch_item = array('m' => $method, 'p' => $params, 'r' => & $result);
1672 $this->batch_queue[] = $batch_item;
1673 }
1674
1675 return $result;
1676 }
1677
1678 private function convert_xml_to_result($xml, $method, $params) {
1679 $sxml = simplexml_load_string($xml);
1680 $result = self::convert_simplexml_to_array($sxml);
1681
1682
1683 if (!empty($GLOBALS['facebook_config']['debug'])) {
1684 // output the raw xml and its corresponding php object, for debugging:
1685 print '<div style="margin: 10px 30px; padding: 5px; border: 2px solid black; background: gray; color: white; font-size: 12px; font-weight: bold;">';
1686 $this->cur_id++;
1687 print $this->cur_id . ': Called ' . $method . ', show ' .
1688 '<a href=# onclick="return toggleDisplay(' . $this->cur_id . ', \'params\');">Params</a> | '.
1689 '<a href=# onclick="return toggleDisplay(' . $this->cur_id . ', \'xml\');">XML</a> | '.
1690 '<a href=# onclick="return toggleDisplay(' . $this->cur_id . ', \'sxml\');">SXML</a> | '.
1691 '<a href=# onclick="return toggleDisplay(' . $this->cur_id . ', \'php\');">PHP</a>';
1692 print '<pre id="params'.$this->cur_id.'" style="display: none; overflow: auto;">'.print_r($params, true).'</pre>';
1693 print '<pre id="xml'.$this->cur_id.'" style="display: none; overflow: auto;">'.htmlspecialchars($xml).'</pre>';
1694 print '<pre id="php'.$this->cur_id.'" style="display: none; overflow: auto;">'.print_r($result, true).'</pre>';
1695 print '<pre id="sxml'.$this->cur_id.'" style="display: none; overflow: auto;">'.print_r($sxml, true).'</pre>';
1696 print '</div>';
1697 }
1698 return $result;
1699 }
1700
1701
1702
1703 private function create_post_string($method, $params) {
1704 $params['method'] = $method;
1705 $params['session_key'] = $this->session_key;
1706 $params['api_key'] = $this->api_key;
1707 $params['call_id'] = microtime(true);
1708 if ($params['call_id'] <= $this->last_call_id) {
1709 $params['call_id'] = $this->last_call_id + 0.001;
1710 }
1711 $this->last_call_id = $params['call_id'];
1712 if (!isset($params['v'])) {
1713 $params['v'] = '1.0';
1714 }
1715 if ($this->json)
1716 $params['format'] = 'JSON';
1717 $post_params = array();
1718 foreach ($params as $key => &$val) {
1719 if (is_array($val)) $val = implode(',', $val);
1720 $post_params[] = $key.'='.urlencode($val);
1721 }
1722 $post_params[] = 'sig='.Facebook::generate_sig($params, $this->secret);
1723 return implode('&', $post_params);
1724 }
1725
1726 public function post_request($method, $params) {
1727
1728 $post_string = $this->create_post_string($method, $params);
1729
1730 if (function_exists('curl_init')) {
1731 // Use CURL if installed...
1732 $ch = curl_init();
1733 curl_setopt($ch, CURLOPT_URL, $this->server_addr);
1734 curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
1735 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
1736 curl_setopt($ch, CURLOPT_USERAGENT, 'Facebook API PHP5 Client 1.1 (curl) ' . phpversion());
1737 $result = curl_exec($ch);
1738 curl_close($ch);
1739 } else {
1740 // Non-CURL based version...
1741 $context =
1742 array('http' =>
1743 array('method' => 'POST',
1744 'header' => 'Content-type: application/x-www-form-urlencoded'."\r\n".
1745 'User-Agent: Facebook API PHP5 Client 1.1 (non-curl) '.phpversion()."\r\n".
1746 'Content-length: ' . strlen($post_string),
1747 'content' => $post_string));
1748 $contextid=stream_context_create($context);
1749 $sock=fopen($this->server_addr, 'r', false, $contextid);
1750 if ($sock) {
1751 $result='';
1752 while (!feof($sock))
1753 $result.=fgets($sock, 4096);
1754
1755 fclose($sock);
1756 }
1757 }
1758 return $result;
1759 }
1760
1761 public static function convert_simplexml_to_array($sxml) {
1762 $arr = array();
1763 if ($sxml) {
1764 foreach ($sxml as $k => $v) {
1765 if ($sxml['list']) {
1766 $arr[] = self::convert_simplexml_to_array($v);
1767 } else {
1768 $arr[$k] = self::convert_simplexml_to_array($v);
1769 }
1770 }
1771 }
1772 if (sizeof($arr) > 0) {
1773 return $arr;
1774 } else {
1775 return (string)$sxml;
1776 }
1777 }
1778
1779 }
1780
1781
1782 class FacebookRestClientException extends Exception {
1783 }
1784
1785 // Supporting methods and values------
1786
1787 /**
1788 * Error codes and descriptions for the Facebook API.
1789 */
1790
1791 class FacebookAPIErrorCodes {
1792
1793 const API_EC_SUCCESS = 0;
1794
1795 /*
1796 * GENERAL ERRORS
1797 */
1798 const API_EC_UNKNOWN = 1;
1799 const API_EC_SERVICE = 2;
1800 const API_EC_METHOD = 3;
1801 const API_EC_TOO_MANY_CALLS = 4;
1802 const API_EC_BAD_IP = 5;
1803
1804 /*
1805 * PARAMETER ERRORS
1806 */
1807 const API_EC_PARAM = 100;
1808 const API_EC_PARAM_API_KEY = 101;
1809 const API_EC_PARAM_SESSION_KEY = 102;
1810 const API_EC_PARAM_CALL_ID = 103;
1811 const API_EC_PARAM_SIGNATURE = 104;
1812 const API_EC_PARAM_USER_ID = 110;
1813 const API_EC_PARAM_USER_FIELD = 111;
1814 const API_EC_PARAM_SOCIAL_FIELD = 112;
1815 const API_EC_PARAM_ALBUM_ID = 120;
1816
1817 /*
1818 * USER PERMISSIONS ERRORS
1819 */
1820 const API_EC_PERMISSION = 200;
1821 const API_EC_PERMISSION_USER = 210;
1822 const API_EC_PERMISSION_ALBUM = 220;
1823 const API_EC_PERMISSION_PHOTO = 221;
1824
1825 const FQL_EC_PARSER = 601;
1826 const FQL_EC_UNKNOWN_FIELD = 602;
1827 const FQL_EC_UNKNOWN_TABLE = 603;
1828 const FQL_EC_NOT_INDEXABLE = 604;
1829
1830 /**
1831 * DATA STORE API ERRORS
1832 */
1833 const API_EC_DATA_UNKNOWN_ERROR = 800;
1834 const API_EC_DATA_INVALID_OPERATION = 801;
1835 const API_EC_DATA_QUOTA_EXCEEDED = 802;
1836 const API_EC_DATA_OBJECT_NOT_FOUND = 803;
1837 const API_EC_DATA_OBJECT_ALREADY_EXISTS = 804;
1838 const API_EC_DATA_DATABASE_ERROR = 805;
1839
1840
1841 /*
1842 * Batch ERROR
1843 */
1844 const API_EC_BATCH_ALREADY_STARTED = 900;
1845 const API_EC_BATCH_NOT_STARTED = 901;
1846 const API_EC_BATCH_METHOD_NOT_ALLOWED_IN_BATCH_MODE = 902;
1847
1848 public static $api_error_descriptions = array(
1849 API_EC_SUCCESS => 'Success',
1850 API_EC_UNKNOWN => 'An unknown error occurred',
1851 API_EC_SERVICE => 'Service temporarily unavailable',
1852 API_EC_METHOD => 'Unknown method',
1853 API_EC_TOO_MANY_CALLS => 'Application request limit reached',
1854 API_EC_BAD_IP => 'Unauthorized source IP address',
1855 API_EC_PARAM => 'Invalid parameter',
1856 API_EC_PARAM_API_KEY => 'Invalid API key',
1857 API_EC_PARAM_SESSION_KEY => 'Session key invalid or no longer valid',
1858 API_EC_PARAM_CALL_ID => 'Call_id must be greater than previous',
1859 API_EC_PARAM_SIGNATURE => 'Incorrect signature',
1860 API_EC_PARAM_USER_ID => 'Invalid user id',
1861 API_EC_PARAM_USER_FIELD => 'Invalid user info field',
1862 API_EC_PARAM_SOCIAL_FIELD => 'Invalid user field',
1863 API_EC_PARAM_ALBUM_ID => 'Invalid album id',
1864 API_EC_PERMISSION => 'Permissions error',
1865 API_EC_PERMISSION_USER => 'User not visible',
1866 API_EC_PERMISSION_ALBUM => 'Album not visible',
1867 API_EC_PERMISSION_PHOTO => 'Photo not visible',
1868 FQL_EC_PARSER => 'FQL: Parser Error',
1869 FQL_EC_UNKNOWN_FIELD => 'FQL: Unknown Field',
1870 FQL_EC_UNKNOWN_TABLE => 'FQL: Unknown Table',
1871 FQL_EC_NOT_INDEXABLE => 'FQL: Statement not indexable',
1872 FQL_EC_UNKNOWN_FUNCTION => 'FQL: Attempted to call unknown function',
1873 FQL_EC_INVALID_PARAM => 'FQL: Invalid parameter passed in',
1874 API_EC_DATA_UNKNOWN_ERROR => 'Unknown data store API error',
1875 API_EC_DATA_INVALID_OPERATION => 'Invalid operation',
1876 API_EC_DATA_QUOTA_EXCEEDED => 'Data store allowable quota was exceeded',
1877 API_EC_DATA_OBJECT_NOT_FOUND => 'Specified object cannot be found',
1878 API_EC_DATA_OBJECT_ALREADY_EXISTS => 'Specified object already exists',
1879 API_EC_DATA_DATABASE_ERROR => 'A database error occurred. Please try again',
1880 API_EC_BATCH_ALREADY_STARTED => 'begin_batch already called, please make sure to call end_batch first',
1881 API_EC_BATCH_NOT_STARTED => 'end_batch called before start_batch',
1882 API_EC_BATCH_METHOD_NOT_ALLOWED_IN_BATCH_MODE => 'this method is not allowed in batch mode',
1883 );
1884 }

Properties

Name Value
svn:keywords Id