Marki Marika
Marki Marika

Reputation: 11

Limit for facebook batch requests

I try to call user posts via batch request. So my idea was to create an multidimensional array. Every array has 50 request

$fb->request('GET','/'.$user.'?fields=id,posts{created_time,message,id}');

First question: Is this one request? I think so...

After that, I want to make the batch call for every array...

foreach($batch_array as $batch){
    try {
        $responses = $fb->sendBatchRequest($batch);     
[...]

and here is the problem: If I set the number of requests to 50/array, I get the error after the first call:

You cannot send more than 50 batch requests at a time

Is only one or 10 requests in one array, I get this error after 50 or 5 batch calls, too.

Has anyone an idea what I am doing wrong?

Thanks, Markus

EDIT:

while($userFB= mysqli_fetch_assoc($userFB_result)){
    if($req < 49){
        $batch[] = $fb->request('GET','/'.$userFB['facebook_id'].'?fields=id,posts{created_time,message,id}');
        $req++;
    } else {
        $batch_array[] = $batch;
        $req = 0;
    }
}
$batch_array[] = $batch;

foreach($batch_array as $batch){
    try {
        $responses = $fb->sendBatchRequest($batch);      
        $responses_array[] = $responses;
    } catch(FacebookExceptionsFacebookResponseException $e) {
        // When Graph returns an error
        echo 'Graph returned an error: ' . $e->getMessage();
        exit;
    } catch(FacebookExceptionsFacebookSDKException $e) {
        // When validation fails or other local issues
        echo 'Facebook SDK returned an error: ' . $e->getMessage();
        exit;
    } 
}

Upvotes: 1

Views: 586

Answers (1)

jokerman
jokerman

Reputation: 41

You're not setting $batch back to an empty array, therefore your $batch variable keeps growing.

else {
        $batch_array[] = $batch;
        $req = 0;
        $batch = [];
    }

Upvotes: 0

Related Questions