批处理API,邀请朋友

时间:2012-10-19 18:05:19

标签: events facebook-page facebook-batch-request

我尝试使用批处理API邀请网页的所有粉丝加入facebook活动。这是我第一次使用这个API,而且此时我没有任何东西可以测试这部分代码......有人可以告诉我如何在不使用真实页面的情况下进行测试,真正的粉丝。 ..或者告诉我它看起来是否正确?

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

1 个答案:

答案 0 :(得分:0)

您的代码似乎没问题,但不要忘记您可能需要将访问令牌添加到批处理请求

像:

$params['access_token']=[USER_ACCESS_TOKEN];

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

为了进行测试,您可能希望创建一个新页面,其中添加了几个朋友并对其进行测试。

还记得你需要处理响应,批处理请求将使用与批处理数组相同长度的数组进行响应,并且每个对象可能是不同的代码,如果是发送则为true。

您也可以在Graph Explorer (clic here)

中试用您的FQL代码

似乎没问题,但它显示了一个额外的结果。