Issue
The api I'm calling returns a json object of validation errors with the HTTP code 400. I implemented the client using PHP's curl library, but on error curl_exec
returns false. How can I get the response body on error?
Note that I am setting curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
Solution
You can unset CURLOPT_FAILONERROR
for once. And adding your error status code to CURLOPT_HTTP200ALIASES
as expected might also help.
curl_setopt($conn, CURLOPT_FAILONERROR, false);
curl_setopt($conn, CURLOPT_HTTP200ALIASES, (array)400);
(libcurl also has a CURLOPT_ERRORBUFFER
, but you cannot use that option from PHP.)
Btw, curl behaves correctly by not returning the response body in case of 4xx errors. Not sure if that can be overriden. So you might have to migrate to PEAR HTTP_Request2
or a similar HTTP request class where you can deviate from the standard.
Answered By - mario Answer Checked By - Gilberto Lyons (WPSolving Admin)