Problem with Curl, catch message body even with http != 200

Asked

Viewed 834 times

5

I have a request on my API:

$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, 'link da api');
curl_setopt( $ch, CURLOPT_FAILONERROR, true );
curl_setopt( $ch, CURLOPT_TIMEOUT, 10 );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $params);
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'POST' );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, true );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 2 );

$db_save_user = json_decode(curl_exec($ch)); //recupera o json quando o httpstatus = 200
$http_status = curl_getinfo( $ch, CURLINFO_HTTP_CODE );

Using the postman, I get the expected error, and the json reply I would like to use this information in case the user accesses the page or updates the page, instead of creating a new user, it proceed with the registration setting the information that is already saved in the bank, but I’m unable to find the method to retrieve this information when httpstatus != 200 no way!

inserir a descrição da imagem aqui

For example, in js/jquery, in a request ajax, when there is server failure, even then there is the possibility to recover the json reply:

jQuery.ajax({
    url: "link da api", 
    type: "POST",
    data: data,
    success: function(returnjson){
        console.log(returnjson.id_customer);
        return true;
    },
    error: function(returnjson) {
        if(returnjson.status == 409){
            console.log(returnjson.responseJSON.id_customer);
            return true;
        }else{
            alert("Ocorreu algum erro, tente novamente ou entre em contato com o suporte técnico.");
        }
        return false;
    }
});
  • Have you checked if the "curl_exec($ch)" command returns data about the request? I believe that some of Curl’s methods cause this behavior to occur. Using another HTTP client would solve your problem as well. That’s not an option?

  • He is returning bool(false).... It is an option, but if possible wanted to do with Curl

  • The command Curl -I http://www.example.org returns the http status code of the request. Can you verify which method matches your client? Maybe this helps.

  • Sorry @andreybleme, I don’t think I understand your question

  • Also, use Uri with the end bar to avoid WCF responding with the 307.

1 answer

5


The problem is exactly in the third line.

curl_setopt( $ch, CURLOPT_FAILONERROR, true );

According to the documentation of CURL:

A long Parameter set to 1 Tells the library to fail the request if the HTTP CODE returned is Equal to or Larger than 400. The default action would be to Return the page normally, ignoring that code.

Translating (edited for better understanding):

The parameter set to "1" tells the library to "kill" the request if the HTTP CODE returned is equal to or greater than 400. The action default would return the page normally, ignoring this code.

So what’s happening is this:

Using the true in the CURLOPT_FAILONERROR you are wrapping up the CURL when the HTTP_CODE not 200, as pointed out. As a result you cannot get the result of the page, nor will you be able to do the json_decode().

Solution:

Remove the line (curl_setopt( $ch, CURLOPT_FAILONERROR, true );) or make it as false. This will cause CURL to return the page even if the HTTP CODE is greater than or equal to 400!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.