邀请50多位朋友参加批量API PHP活动

时间:2012-10-17 11:05:02

标签: facebook events batch-file facebook-fql facebook-page

我正在尝试使用批处理API让我的用户能够邀请所有页面粉丝参加活动。 这是我第一次尝试使用批处理API,我遇到了一些麻烦。这是我的代码,不使用批处理:

$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.')'
));

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

如何使此代码适应批处理API以查询超过50个页面的粉丝?

由于

2 个答案:

答案 0 :(得分:0)

  

如何使此代码适应批处理API以查询超过50个页面的粉丝?

由于50次操作是批量请求的限制 - 只需将其拆分为50个包,然后发出一个包含这50个操作的批处理请求,然后是下一个。

答案 1 :(得分:0)

抱歉这个答案太迟了。我的问题是我不知道如何测试我的代码。

我用你的答案写下这段代码。有人可以告诉我它是否可行,或者如何在不将其发送到生产的情况下进行测试?

//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++;
         }
    }
}