我正在对一个API进行两次调用。唯一不同的是这两个调用的URL。我基本上想知道的是,我如何才能更好地构建它?也许让他们排成一排追着对方跑?有时第二次调用会在它被调用的Wordpress页面上返回一个“严重错误”。
有什么建议吗?
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";
}发布于 2021-04-20 02:48:17
我就想明白了。对代码进行了一些结构化。如下所示:
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";
}https://stackoverflow.com/questions/67151959
复制相似问题