开发者

Using PHP curl how does one get the response body for a 400 response

The api I'm calling returns a json object of validation errors w开发者_如何学运维ith 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);


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.


I was able to get content from a 400 response by setting FAILONERROR to false, WITHOUT having to also alias 400 as a normal 200 response.


Add this:

curl_setopt($ch, CURLOPT_HTTP200ALIASES, array(400));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜