Php 当API错误发生时,如何使用curl读取响应体?

Php 当API错误发生时,如何使用curl读取响应体?,php,api,curl,postman,slim-3,Php,Api,Curl,Postman,Slim 3,我使用slimv3开发了API,当引发异常时,我有一个ErrorHandler返回以下内容: public function __invoke(Request $request, Response $response, \Exception $exception) { $status = $exception->getCode() ? : 500; $error = [ "status" => ERROR_MSG, "data" =&g

我使用
slimv3
开发了
API
,当引发异常时,我有一个
ErrorHandler
返回以下内容:

public function __invoke(Request $request, Response $response, \Exception $exception)
{
    $status = $exception->getCode() ? : 500;
    $error = [
        "status" => ERROR_MSG,
        "data" => [
            "stack_trace" => ($this->_container['settings']['displayErrorDetails']) ? $exception->getTraceAsString() : '',
        ],
        "message" => $exception->getMessage()
    ];

    $body = json_encode($error, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);

    return $response
        ->withStatus($status)
        ->withHeader("Content-type", "application/json")
        ->write($body);
}
因此,基本上当
API
中发生异常时,例如:

throw new Exception('Invalid user credentials');
API
将返回以下响应:

{
    "status": "error",
    "data": {
        "stack_trace": "some stack trace.."
    },
    "message": "Invalid user credentials"
}
现在的问题是,当我使用
curl
启动请求时,我只得到:

字符串(59)“请求的URL返回错误:500内部服务器错误”

从以下代码:

curl_error($this->session);
其中
$this->session
包含请求

我还尝试使用以下方法调试请求:

$this->response = curl_exec($this->session);
$this->info = curl_getinfo($this->session);
$errno = curl_errno($this->session);
$error = curl_error($this->session);
我得到:

bool(false)
array(26) {
  ["url"]=>
  string(48) "http://localhost/ci3-api/v1/installation/install"
  ["content_type"]=>
  NULL
  ["http_code"]=>
  int(500)
  ["header_size"]=>
  int(0)
  ["request_size"]=>
  int(148)
  ["filetime"]=>
  int(-1)
  ["ssl_verify_result"]=>
  int(0)
  ["redirect_count"]=>
  int(0)
  ["total_time"]=>
  float(0.25)
  ["namelookup_time"]=>
  float(0.016)
  ["connect_time"]=>
  float(0.219)
  ["pretransfer_time"]=>
  float(0.219)
  ["size_upload"]=>
  float(0)
  ["size_download"]=>
  float(0)
  ["speed_download"]=>
  float(0)
  ["speed_upload"]=>
  float(0)
  ["download_content_length"]=>
  float(-1)
  ["upload_content_length"]=>
  float(0)
  ["starttransfer_time"]=>
  float(0.25)
  ["redirect_time"]=>
  float(0)
  ["redirect_url"]=>
  string(0) ""
  ["primary_ip"]=>
  string(9) "127.0.0.1"
  ["certinfo"]=>
  array(0) {
  }
  ["primary_port"]=>
  int(80)
  ["local_ip"]=>
  string(9) "127.0.0.1"
  ["local_port"]=>
  int(65036)
}
int(22)
string(59) "The requested URL returned error: 500 Internal Server Error"
如何读取
API
响应消息? 当从
API
引发错误
500
时,似乎无法执行此操作。
我尝试使用其他错误代码,如
400
,我可以读取响应,但使用错误代码
500
我的响应总是
false
,这是我的设置;我认为对你来说重要的是转会,主要是转会失败

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'your api url');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, false); // this is important for you I think

// other stuff you need to set

$output = curl_exec($ch);

$curlError = curl_error($ch);
$curlErrNo = curl_errno($ch);
$httpCode = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);

if($output === false) {
// if header CURLOPT_FAILONERROR is true, always throw here
throw new RuntimeException('curl fail');
}

curl_close($ch);
// here process your output

关键是将
CURLOPT_FAILONERROR
设置为false。当第三方web服务的http响应代码>=400时,我从第三方web服务获取响应主体时遇到了同样的问题,这解决了问题

curl_setopt($ch, CURLOPT_FAILONERROR, false);

从这个问题上看,这不是很明显,但它是
CURLOPT_RETURNTRANSFER
true和
var_dump($this->response)是否为
bool(false)
?另外,随机建议:如果代码没有抛出任何未处理的异常,那么显示堆栈跟踪和异常消息可能不是一个好主意。此外,
无效的用户凭据应为401。您可能希望创建一个由错误处理程序识别的特殊异常,任何常规异常都应该只返回一条通用错误消息,而不是异常消息。选项
CURLOPT_RETURNTRANSFER
设置为true,异常发生时响应始终为false。我仅在开发过程中显示堆栈跟踪,因此可以轻松调试请求。上面关于凭证的例外只是一个例子。所以,如果我理解得很好,我应该在异常发生时返回一个不是500的错误代码?