2012-10-19 127 views
1

我想邀請頁面的所有球迷使用批處理API Facebook的事件。這是我第一次使用這個API,並且此時我沒有任何東西可以測試這部分代碼......有人能告訴我如何在不使用真實頁面的情況下測試,真正的粉絲......還是告訴我是否它看起來正確?批量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++; 
     } 
    } 
} 

回答

0

你的代碼看起來不錯,但不要忘記,你可能需要訪問令牌添加到批量要求

,如:

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

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

而且測試你可能要創建一個新的與幾個朋友的頁面添加並測試它。

還記得需要處理的響應,批處理請求將響應與具有相同長度,該批次陣列,並且每個對象可以是diferent代碼,真正的數組,如果它是發送。你

也可以嘗試在Graph Explorer (clic here)

您FQL代碼看起來不錯,但它顯示了一個交流中心的結果。

+0

實際上正在使用Facebook不允許邀請球迷,唯一的朋友是球迷太...悲傷...但感謝您的回答! –