0

I have tried to send only 5 call to fetch tasks from Asana API. Here is my code

$tasks = $client->fetch('https://app.asana.com/api/1.0/workspaces.json');
print_r($tasks);
$i=0;

while(isset($tasks['result']['data'][$i]['id']))
{ 
    $tasks_detail = $client->fetch('https://app.asana.com/api/1.0/workspaces/'.$tasks['result']['data'][$i]['id'].'.json');
    print_r($tasks_detail);

    while(isset($tasks_detail['result']['data'][$i]['id']))
    { 
        $tasks_details = $client->fetch('https://app.asana.com/api/1.0/tasks/'.$tasks_detail['result']['data'][$i]['id'].'.json');
        print_r($tasks_details);
        $i++;
    }

I have used only 2 while loops to fetch tasks from the API after which the api doesn't respond to the calls.

msturdy
  • 10,479
  • 11
  • 41
  • 52
mohd afeef
  • 35
  • 1
  • 10
  • please format your code – thescientist Feb 19 '14 at 12:57
  • have reformatted your code, but you aren't closing the first `while` in the code you posted.. please review it! – msturdy Feb 19 '14 at 13:02
  • If `$tasks_detail['result']['data'][$i]['id']` is not set for one result from the first API call in the loop, you've produced an endless loop - resulting in unlimited API calls. – Chris Feb 19 '14 at 13:03

1 Answers1

2

Consider this situation:

$tasks['result']['data'][$i]['id']

exists and

$tasks_detail['result']['data'][$i]['id']

does not exist.

You will loop forever since the $i never gets bigger; Consider this situation:

$tasks['result']['data'][1]['id']

exists and

$tasks_detail['result']['data'][1]['id']
$tasks_detail['result']['data'][2]['id']
$tasks_detail['result']['data'][3]['id']

exists.

next loop you will access

$tasks['result']['data'][4]['id']

directly and lose

$tasks['result']['data'][2]['id']
$tasks['result']['data'][3]['id']

.

You may want to do something like this:

while(isset($tasks['result']['data'][$i]['id'])){ 
    $tasks_detail = $client->fetch('https://app.asana.com/api/1.0/workspaces/'.$tasks['result']['data'][$i]['id'].'.json');
    print_r($tasks_detail);

    if(isset($tasks_detail['result']['data'][$i]['id'])){
        $tasks_details = $client->fetch('https://app.asana.com/api/1.0/tasks/'.$tasks_detail['result']['data'][$i]['id'].'.json');
        print_r($tasks_details);
    }
    $i++;
}

Please be careful to end the loop when you use while loop.

kyo
  • 645
  • 4
  • 7