took me two hours today to figure this one out – how to let my users invite their friend to some Facebook app I’m developing, everything I looked at sent me to the same javascript SDK solution – “just use a little of FBML and you’re all set…”. Well iframe applications in Facebook don’t support most FBML so a server side solution is needed.
the next code snippet is nothing I wrote myself, it just took me hours to find so I thought I might as well post it here with a link to their page so it pops up higher in Google. I modified the code a little so it works inside my FB connector Class. you can find the original version here.
here is my code:
const FB_API_KEY = 'kaklhgkajhgkjhgkjdfhgkjdhgkljsdhklsjdhgsk'; const FB_SECRET = ';kjfdg09uq34t0jqeofpjie0pv34q'; const FB_APPID = '12121212121212'; const FB_CANVAS_URL = 'http://apps.facebook.com/app/'; const FB_APP_HOME_URL = 'http://www.website.com/'; function SendNewRequest($to, $typeword, $content, $actionText, $bInvitation = true) { $facebook = self::$FB; $bInviteAll = (!$to || $to == "" ? true : false); $excludeFriends = null; if (!$bInviteAll){ // Get all friends $excludeFriends = $facebook->api_client->friends_get(); }else{ // Get all friends with the app $excludeFriends = $facebook->api_client->friends_getAppUsers(); } $excludeFriendsStr = null; foreach ($excludeFriends as $userid) { $pos = strpos($to, (string)$userid); if ($pos !== false) continue; if ($excludeFriendsStr) $excludeFriendsStr .= ','; $excludeFriendsStr .= $userid; } $params = array(); $params['api_key'] = self::FB_API_KEY; $params['content'] = $content; // Don't use htmlentities() or urlencode() here $params['type'] = $typeword; $params['action'] = self::FB_CANVAS_URL; $params['actiontext'] = $actionText; $params['invite'] = ($bInvitation ? 'true' : 'false'); $params['rows'] = '5'; $params['max'] = '20'; $params['exclude_ids'] = $excludeFriendsStr; $params['sig'] = $facebook->generate_sig($params, self::FB_SECRET); $qstring = null; foreach ($params as $key => $value) { if ($qstring) $qstring .= '&'; $qstring .= "$key=".urlencode($value); } $inviteUrl = 'http://www.facebook.com/multi_friend_selector.php?'; $facebook->redirect($inviteUrl . $qstring); return true; }
This code make a redirect to the selector page. I really wanted my “invite friends” button to open a popup for the friend selector but from what I read it is impossible.
if you found out how to do this or know its possible I would love to hear about it.



