Search code examples
phpapioauthoauth-2.0etsy

PHP OAuth errors handling


I am using an OAuth library to connect to Etsy shop API. Code looks like this:

foreach ($transactions as $transactionSingle) {
    try {
        $oauth = new OAuth('xxx', 'xxx',
            OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_URI);
        $oauth->setToken($access_token, $access_token_secret);

        $data = $oauth->fetch("https://openapi.etsy.com/v2/users/".$transactionSingle['buyer_id']."/profile", null, OAUTH_HTTP_METHOD_GET);
        $json = $oauth->getLastResponse();
        $results = json_decode($json, true);
    } catch (OAuthException $e) {
        return null;
    }
}

Now the problem is that I run this code multiple times on foreach and if this URL is wrong and it fails to get any data - the whole function stops and doesn't continue anymore. It works perfectly until an old user ID is passed to URL and $oauth->fetch just reuturns message :

Invalid auth/bad request (got a 404, expected HTTP/1.1 20X or a redirect)

Any ideas how to continue to run the function despite any errors?


Solution

  • The problem was in error cathing itself. Needed backslash on

    catch (\OAuthException $e) {
    }
    

    Now the code catches the errors and continues if no code is provided inside catch.