首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >函数中的Que API调用

函数中的Que API调用
EN

Stack Overflow用户
提问于 2021-04-19 02:17:13
回答 1查看 29关注 0票数 1

我正在对一个API进行两次调用。唯一不同的是这两个调用的URL。我基本上想知道的是,我如何才能更好地构建它?也许让他们排成一排追着对方跑?有时第二次调用会在它被调用的Wordpress页面上返回一个“严重错误”。

有什么建议吗?

代码语言:javascript
复制
    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";
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-20 02:48:17

我就想明白了。对代码进行了一些结构化。如下所示:

代码语言:javascript
复制
    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";
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67151959

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档