2012-10-17 65 views
0

我正在嘗試使用批量API讓我的用戶能夠邀請所有頁面粉絲參加活動。 這是我試圖使用批處理API的第一個問題,我也遇到了一些麻煩。這裏是我沒有使用該批次的代碼:邀請超過50個朋友參與批量API活動PHP

$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個頁面粉絲?

感謝

+1

究竟是什麼「煩惱」你有?心理調試是一項艱鉅的工作;) – Lix

+0

請包括您的批處理PHP代碼,以便我們看到您的問題。 –

回答

0

如何適應這個代碼批量API查詢的頁面超過50名球迷?

由於50個操作是批處理請求的限制 - 只需將其分解爲50個包,並使一個批處理請求包含這50個操作,然後再執行下一個操作。

+0

我想象的是OP得到一個錯誤信息的效果......「你只能發送50邀請」。 – Lix

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

Up?你認爲這個腳本是正確的嗎?或者你有一個想法如何測試它? –