0

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?

The50
  • 1,096
  • 2
  • 23
  • 47
  • What do you mean by "I run this code inside an array"? – Erwin Moller Apr 25 '18 at 14:42
  • @ErwinMoller He runs it in a `batch` I think. – deEr. Apr 25 '18 at 14:44
  • Post updated, I meant that it runs multiple times on foreach. And the error just stops the whole cycle. – The50 Apr 25 '18 at 14:46
  • OK thanks, clear now. Do you receive an Exception of maybe an error? (This can be confusing, depending on your PHP version.) Try to catch every exception catch (Exception $e) for starters. If that doesn't help, register your own errorhandler, and see if that catches it. – Erwin Moller Apr 25 '18 at 14:48
  • dumping $e doesn't give me anything, code even doesn't go to catch section I guess, it just dies after I make a request to URL with $oauth->fetch – The50 Apr 25 '18 at 14:52
  • Did you check the returnvalue of $oauth->fetch() ? In $data. Check if it is false. if ($data === false ) – Erwin Moller Apr 25 '18 at 14:55
  • Yes, it doesn't even return anything in case of an error. Here is the dump of all the oauth->fetch: https://d.pr/i/qAmPvU – The50 Apr 25 '18 at 14:59
  • Let me recap: The call to $oauth->fetch() errors out. Hence you cannot catch it as an exception, nor can you check it's returnvalue in $data. I think I would set up an errorhandler in your case (or suppress the error), check if the returnvalue makes sense (a json), and if not: ignore (maybe log) , just keep looping. – Erwin Moller Apr 25 '18 at 15:09
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/169773/discussion-between-erwin-moller-and-the50). – Erwin Moller Apr 25 '18 at 15:11
  • Thank you very much for your time, just found the problem. Posted below. – The50 Apr 25 '18 at 15:11

1 Answers1

0

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.

The50
  • 1,096
  • 2
  • 23
  • 47