Que API calls in function

Viewed 32

I'm doing two calls to an API. The only thing that differ the two calls is the URL. What i basically wonder is how i can structure this a little (or alot) better? Maybe run them after each other in a que? Sometimes the second calls returns a "critical error" on the Wordpress page where it gets called.

Any suggestions?

    private $oauth_Key = 'xxx';
    private $oauth_consumer = 'xxx';
    private $api_url = 'xxx';
    private $cat_url = 'xxx';

    try {
      $oauth = new OAuth($this->oauth_Key, $this->oauth_consumer, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_AUTHORIZATION);
      $oauth->fetch($this->api_url);
      $response_info = $oauth->getLastResponseInfo();
      header("Content-Type: {$response_info["content_type"]}");
      $res = $oauth->getLastResponse();

      $oauth2 = new OAuth($this->oauth_Key, $this->oauth_consumer, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_AUTHORIZATION);
      $oaut2->fetch($this->cat_url);
      $response_info2 = $oauth2->getLastResponseInfo();
      header("Content-Type: {$response_info2["content_type"]}");
      $res2 = $oauth2->getLastResponse();

    } catch(OAuthException $e) {
      echo "Exception caught!\n";
      echo "Response: ". $e->lastResponse . "\n";
    }
1 Answers

Just figured it out. Re structured the code a bit. See below:

    private $oauth_Key = 'xxx';
    private $oauth_consumer = 'xxx';
    private $api_url = 'xxx';
    private $cat_url = 'xxx';

    try {
      $oauth = new OAuth($this->oauth_Key, $this->oauth_consumer, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_AUTHORIZATION);
      $oauth->fetch($this->api_url);
      $response_info = $oauth->getLastResponseInfo();
      header("Content-Type: {$response_info["content_type"]}");
      $res = $oauth->getLastResponse();

      $oauth2 = new OAuth($this->oauth_Key, $this->oauth_consumer, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_AUTHORIZATION);
      $oauth2->fetch($this->cat_url);
      $response_info2 = $oauth2->getLastResponseInfo();
      header("Content-Type: {$response_info2["content_type"]}");
      $res2 = $oauth2->getLastResponse();

    } catch(OAuthException $e) {
      echo "Exception caught!\n";
      echo "Response: ". $e->lastResponse . "\n";
    }
Related