eventsfacebook-pagefacebook-batch-request

Batch API, inviting friends


I'm trying to invite all fans of a page to a facebook event using the batch API. It's the first time I'm using this API, and at this time I have nothing to test this part of code... can someone tell me how to test without using a real page, with real fans... or tell me if it looks correct ?

 //Getting all the likers of the page
 $result = $facebookObj->api(array(
     'method' => 'fql.query',
     'query' => 'select uid,name from user where uid in ( select uid from page_fan where uid in (select uid2 from friend where uid1 = me()) and page_id = '.$fbPage.')'
 ));


//If liker are more than 50 we use batch request
if($numLikers >50){

    // split array into several part of 50 users accounts                        
    $splitLikers = array_chunk($result, 50);
    // count how many arrays are generated by the array_chunk
    $countSplit = count($splitLikers);

    //Start a loop through the numbers of groups of 50 users  (or less if last group contains less than 50 users                      
    for($a=0; $a<$countSplit; $a++){
       //Second loop to go through the 50 likers in one group                                  
       for($b=0; $b<count($splitLikers[$a]); $b++){
           // construct an array containing the whole group                                
           $queries[$a] = array('method' => 'POST', 'relative_url' => $event_fbID . "/invited/" . $splitLikers[$a][$b]['uid']);

       }
       //Send POST batch request with the array above                            
       $ret_val = $facebookObj->api('/?batch='.json_encode($queries[$a]), 'POST');
    }


}else{

    foreach ($result as $value) {
        $ret_val = $facebookObj->api($event_fbID . "/invited/" . $value['uid'],'POST');
        if($ret_val) {
            // Success
            $numInvited++;
       }
   }
}

Solution

  • Your code seems ok, but dont forget you may need to add the access token to the batch request

    like:

    $params['access_token']=[USER_ACCESS_TOKEN];
    
    $ret_val = $facebookObj->api('/?batch='.json_encode($queries[$a]), 'POST', $params);
    

    And for testing you may want to create a new page with a couple of friends added and test it.

    Also remember you need to handle the response, the batch request will response with an array with the same length that the batch array, and each object may be a diferent code, true if it was send.

    Also you can try your FQL code in the Graph Explorer (clic here)

    Seems ok, but it shows a extrange result.