Pick up JSON after sending a POST

2

I'm having trouble getting the response I get after submitting a request via POST

function httpPost($url, $data) {
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($curl);
        curl_close($curl);
        return $response;
    }

After that I wanted to get the answer I have in JSON and "separate" the fields I have as a return, for example:

$resposta = httpPost($url, $data);

$status = $resposta->{'status'};

Response I get when I run:

{"errors":[{"type":"internal_error","parameter_name":null,"message":"Ocorreu um erro ao cancelar a transferência."}],"url":"URL","method":"post"}

I wanted to print the "message" for example.

    
asked by anonymous 19.05.2016 / 23:25

2 answers

1

I was able to do the following:

$executar_transf = httpPost($url, $data);
$resposta_fin = json_decode($executar_transf, true);

echo $resposta_fin['errors']['0']['message'];
    
20.05.2016 / 17:20
0

One tip is to use the $.get() function of JQuery:

$(function () {
    $.get('exemplo.php', {
        //parametros da solicitacao
        param: 'pamametro'
    },function (data) {
        // resposta da solicitacao
        var response = data['errors'];
        console.log(response[0].message);
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>

One detail is that if you want to send more than one parameter, you can only separate them by commas: { param_1: 'param_1', param_2: 'param_2' }

    
20.05.2016 / 02:50