List of User IDs From Requests Dialog | Migrating from fb:request-form to FB.ui apprequests | Facebook API | JavaScript | PHP

So the facebook requests dialog is not very well documented. It assumes you know how to do a 
few things in the documentation. For instance, the "GET /ID/" listed actually means in PHP:

$result = $facebook->api('/'.$requestID.'/');

Here is the original documentation to better help you understand this article:
http://developers.facebook.com/docs/reference/dialogs/requests/

So if we are migrating from the old fb:request-form to the FB.ui requests dialog the behavior
is going to be different. Notably there will be no user IDs of the reciepients of the requests
returned in the action url. The old FBML would return a bunch of indexes of users who recieved
the request and the requests dialogue returns a list of request IDs with special data attached
to each ID. For reference the depricated FBML looked like this (scroll down):

<fb:fbml> 
        <fb:request-form
                action="" 
                target=""
                method="POST"
                invite="true"
                type="Sketch 'n Pass"
                content="Check out my drawing :)
                        <fb:req-choice url='<?php echo $config->topURL."/".$config->fbAppName."/?".$this->inviteURLVars;?>'
                        label='Check it out'
                        /> 
                " >
                <fb:request-form-submit
                        import_external_friends="false"
                />
                <fb:multi-friend-selector
                        showborder="true"
                        exclude_ids=""
                        condensed="true" 
                        import_external_friends="false"
                        selected_rows="6"
                        style="width:400px;height:450px;"
                /> 
                
        </fb:request-form> 
    </fb:fbml>


and then I could extract all the user IDs in PHP like this:

$sentToUsersArray = array ();
if (isset ( $_REQUEST ['ids'] )) {
$sentToUsersArray = $_REQUEST ['ids'];
}
Yuck!
So now we use the FB.ui and ajax to call up the requests dialog
then the callback onInviteFriend is called and you can get the request objects by their ids which are returned in a list in the response object response.request_ids


FB.ui({
                   method: 'apprequests', 
                   title:'Suggest '+fbAppName+' to your friends', 
                   message: 'I thought you would dig this app!', 
                   data: 'gameID=96'
       }, onInviteFriend);

//jquery required for ajax function
    onInviteFriend = function(response) {
        if (response && response.request_ids) {
            var idsLength = response.request_ids.length;
            var idList = "";
            var data = {};
            var ajaxSettings = {};
            for (var i = 0; i < idsLength; i++){
                idList += response.request_ids[i];
                if (i < idsLength-1){
                    idList += ",";
                }
            }
            if (idsLength > 0){
                data.idList = idList;
                ajaxSettings = {
                    type: "GET",
                    url: sketchnpass.root+"/ajax/log-invite-sent/",
                    data: data,
                    success: sketchnpass.onSaveInvites
                };
                $.ajax(ajaxSettings);
            }
            //was published
        } else {
            //was not published
        }
    }
then we send the request id list to the PHP script which requires the Facebook php api and curl installed.
pass the comma delimitate string into $sentRequestsStr and it will return a comma delimited string of the user IDs

public function getUserIDsFromRequests($sentRequestsStr){
        $config = Zend_Registry::get ( 'config' );
        $sentRequestsArray = explode(",", $sentRequestsStr);
        $sentRequestsLen = count ( $sentRequestsArray );
        $sentUsersStr = "";
        if ($config->isFacebookApp){
            $facebook = new Facebook(array(
                              'appId'  => $config->appID,
                              'secret' => $config->appSecretKey,
                              'cookie' => true
                            ));
            for ($i = 0; $i < $sentRequestsLen; $i++){
                $cmd = "/".$sentRequestsArray[$i]."/";
                //echo $cmd."\n";
                try{
                    $result = $facebook->api($cmd);
                    //echo "success\n";
                    if ($result['to']['id']){
                        $sentUsersStr .= $result['to']['id'];
                        if ($i < $sentRequestsLen-1){
                            $sentUsersStr .= ",";
                        }
                    }
                    
                    //print_r($result);
                    //exit();
                    //echo "\n";
                    
                } catch (FacebookApiException $e) {
                    //$this->_logger->log ( $result, Zend_Log::INFO );
                    //print_r($e);
                }
            }
            return $sentUsersStr;
        }
    }

A more elequent solution at the cost of an extra step. Ahh sigh oh well.
Here is how $result is structured so that you can get other data out of the request object.

Array (
[id] => 101501913919285
[application] => Array
(
[name] => appname
[id] => 145161830442
)
[to] => Array
(
[name] => Recieving User Name
[id] => 639139284
)
[from] => Array
(
[name] => Sent From User Name
[id] => 16037411
)
[data] => gameID=96
[message] => I thought you would dig this app!
[created_time] => 2011-05-25T04:43:26+0000
)
Trackback(0)
Comments (0)add comment

Write comment
quote
bold
italicize
underline
strike
url
image
quote
quote
smile
wink
laugh
grin
angry
sad
shocked
cool
tongue
kiss
cry
smaller | bigger

security image
Write the displayed characters


busy