0

So im accessing some data via an oAuth2 access token and i want to catch the expiration of the access token. When it expires i catch the error code and refresh the token. After that i call the function recursivly. If the refresh token for some reason does not work, it would loop infinitly. How do i jump out of this loop after 1 try?

function companyList($page){
//send http-request
    if($obj['errors']['status'] == '401'){
        Authentication::refreshToken();
        companyList($page);
    }
}
Failbot12
  • 1
  • 3

1 Answers1

0

Use a global counter that increases 1 step each time it refresh the token and add a if clause that compares it to the refresh limit you wish.

Example:

$counter = 0;
function companyList($page){
    global $counter;
    //send http-request
    if($obj['errors']['status'] == '401' && $counter < 5){
        Authentication::refreshToken();
        $counter++;
        companyList($page);
    } elseif($counter >= 5){
        $counter = 0;
    }
}

Hope this helps you!

Jorge C.M
  • 375
  • 3
  • 11
  • This is helpfull, but this function will be called a lot which means, that if the token expired 5 times the function will not try to refresh the token anymore. – Failbot12 Oct 04 '19 at 12:03
  • Oh, yes. My fault. One more code line hahaha I'll update my answer. Now when the counter reachs 5 it just resets itself and stills ends the infinite loop. – Jorge C.M Oct 04 '19 at 12:05
  • that fixed it now! thank you :) – Failbot12 Oct 04 '19 at 12:45