鍍金池/ 問(wèn)答/Java  PHP  Linux  網(wǎng)絡(luò)安全/ php CURL 如何取得對(duì)方回傳的500 錯(cuò)誤?

php CURL 如何取得對(duì)方回傳的500 錯(cuò)誤?

clipboard.png

我是用curl去進(jìn)行請(qǐng)求
對(duì)方回錯(cuò)誤的時(shí)候會(huì)是500,如圖
我要如何去抓到他吐出來(lái)的信息在我這?
因?yàn)槿绻麤](méi)有錯(cuò)誤,對(duì)方回傳json給我我收得到,可以顯示
但對(duì)方錯(cuò)誤時(shí)會(huì)回傳500錯(cuò)誤,那我要如何抓到這段文字?
我已經(jīng)做老半天還是沒(méi)有抓到

$ch = curl_init($api);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
  curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
  $result = curl_exec($ch);
回答
編輯回答
獨(dú)白

$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

2018年6月7日 19:57
編輯回答
哎呦喂

在頭信息里面可以獲取的

// 獲取響應(yīng)頭大小
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);

// 根據(jù)響應(yīng)頭大小去獲取頭信息內(nèi)容
$header = substr($result, 0, $headerSize);
2017年10月17日 17:32