ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/facebook/branches/official/facebookapi_php5_restlib.php
Revision: 1055
Committed: 2008-06-25T02:33:32-07:00 (17 years ago) by douglas
File size: 65139 byte(s)
Log Message:
Update to 2008-06-24!

File Contents

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

Properties

Name Value
svn:executable *