【问题标题】:Invite more than 50 friends to an event with batch API PHP使用批处理 API PHP 邀请 50 多个朋友参加活动
【发布时间】:2012-10-07 15:05:30
【问题描述】:

我正在尝试使用批处理 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多个粉丝?

谢谢

【问题讨论】:

  • 您遇到的“麻烦”究竟是什么?心理调试是一项艰巨的工作;)
  • 请包含您的批处理 PHP 代码,以便我们查看您的问题。

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


【解决方案1】:

如何将这段代码适配成批处理 API 来查询页面的 50 多个粉丝?

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

【讨论】:

  • 我想 OP 收到了一条错误信息……“你只能发送 50 个邀请”。
【解决方案2】:

很抱歉这么晚才知道答案。我的问题是我不知道如何测试我的代码。

我使用您的答案编写了这个代码示例。有人可以告诉我它是否可行,或者我如何在不将其发送到生产环境的情况下对其进行测试?

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

【讨论】:

  • 起来?你认为这个脚本是正确的吗?或者你知道如何测试它?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多