php - 使用 curl 发生 API 错误时如何读取响应正文?
问题描述
我开发了一个API
using Slim v3
,当引发异常时,我有一个ErrorHandler
which 返回以下内容:
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
我只得到:
string(59) "请求的 URL 返回错误:500 内部服务器错误"
从以下代码:
curl_error($this->session);
where$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
响应消息?当500
从API
. 我尝试使用其他错误代码,例如400
我可以读取响应,但是使用错误代码500
我总是有响应false
。
解决方案
这是我的设置;我认为对你来说重要的是 CURLOPT_RETURNTRANSFER 主要是 CURLOPT_FAILONERROR
$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
推荐阅读
- python - GDAL 已安装但不会导入到 Jupyter Notebooks 尽管修复了 PATH?[Python]
- php - codeigniter smtp 邮件无法正常工作
- amazon-web-services - 使用 boto3 获取 S3 公共策略
- android - Recompose 方法在 Jetpack Compose 中不起作用
- python - pyinstaller 可执行文件的差异更新(修改嵌入式 PYZ-00.pyz)
- android - 如何使用 Play 应用签名和应用程序包设置 Unity 应用程序并将其部署到 Google Play?
- javascript - 无法在我的网站上显示 Bootstrap Toast
- python - 如何拆分数据之间的时间步长大于 1 秒的 pandas 数据帧?
- visual-studio-2019 - 在 VS2019 中看不到 IntelliTrace 的导航栏
- powershell - 无法将变量传递给调用命令