Search code examples
phpcurlasana

Error You have made too many requests recently. Please, be chill. in Asana API


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.


Solution

  • 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.