Reading the error body in PHP curl_exec [duplicate]
Asked Answered
R

1

6

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

When using PHP's curl_exec to call a RESTful API the documentation says http://php.net/manual/en/function.curl-exec.php

Returns TRUE on success or FALSE on failure. However, if the CURLOPT_RETURNTRANSFER option is set, it will return the result on success, FALSE on failure.

When curl_exec fails it returns false. However the remote server might have tried to return a helpful message to go with that error. How can we access this message using curl_exec, when it just returns false?

For example the remote API might be returning this message and a HTTP 400 status code:

{"Items":[{"Field":"FirstName","Description":"FirstName is required and cannot be null."}]}

Currently I am unable to read this error message that has been returned, instead all I get is false. I have looked at curl_info too. I do have returntransfer set on.

In C# I would use the exception.Response.GetResponseStream method and write:

    private static string Post(WebClient client, string uri, string postData)
    {
        string response = string.Empty;
        try
        {
            response = client.UploadString(uri, "POST", postData);
        }
        catch (WebException ex)
        {
            var stream = ex.Response.GetResponseStream(); // here
            if (stream != null)
            {
                using (var reader = new StreamReader(stream))
                {
                    response = reader.ReadToEnd();
                }
            }
        }
        return response;
    }

How can this be done in PHP? Is curl_exec the right method or should I use something else?

Rybinsk answered 3/10, 2012 at 12:20 Comment(7)
looook php.net/manual/en/function.curl-error.phpJemima
That just returns the string for whatever status code was returned, eg "Bad Request" for the http 400 I am getting. It doesn't return the message set by the remote API. Actually, no - I just checked again, curl_error is returning an empty stringRybinsk
@Rybinsk I cant see any way of doing that except... sockets,Jemima
With curl_setopt($curl, CURLOPT_FAILONERROR, false); and curl_setopt($curl, CURLOPT_HTTP200ALIASES, array(400)); set, $curl_result is only returning "Bad Request" (a small improvement - before it was returning false)Rybinsk
@Rybinsk Sorry, what is $curl_result? The return value of curl_exec()?Phosphorous
@Rybinsk Definitely working for me: codepad.viper-7.com/S6ptk0Phosphorous
keep "CURLOPT_FAILONERROR: false" to get an error response body and then check HTTP status from "curl_getinfo($crl, CURLINFO_HTTP_CODE)" based on the HTTP status you can return error or success.Goldston
F
-1

You should look at curl_error

http://www.php.net/manual/en/function.curl-error.php

Frederiksberg answered 3/10, 2012 at 12:32 Comment(1)
if you look at the comments you see he dont means thatJemima

© 2022 - 2024 — McMap. All rights reserved.