Return JSON PHP POST query

Asked

Viewed 772 times

0

I have this script in PHP, but I can’t see the result of the POST [Body]. It sends a CPF query and should return a json with the data. But nothing appears in the query.

<?php 
$array = '{ 
    "cpf_cnpj": "83899526000182"
}'; 

$json = json_encode($array);
$ch = curl_init('http://mobi.ieptb.org.br/consulta');

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");

curl_setopt($ch, CURLOPT_POSTFIELDS, $json);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_HTTPHEADER, array(

    'Content-Type: application/json',

    'Content-Length: ' . strlen($json))

);


$jsonRet = json_decode(curl_exec($ch));

?> 

It should return the following JSON (DATA I WANT TO GET):

[
{
"uf": "SC",
"codigo_cartorio": 1,
"codigo_cidade": 4205407,
"descricao": "2º TABELIONATO DE NOTAS E 1º PROTESTO DE TÍTULOS",
"endereco": "Rua Tenente Silveira, 221 , Centro - Florianópolis - SC - Telefone: 4830391991",
"telefone": "4830391991",
"data_atualizacao": "16/10/2019",
"quant_protestos": 1,
"protestos":[
{
"data_protesto": "15/05/2018",
"valor_protestado": "R$88.545,00",
"valor_protestado_o": 88545
}
]
},

IMAGEM DO TALEND API TESTER COM O RETORNO

  • 1

    What is the value of $jsonRet in the end?

  • I edited the question with the return. I send a request via POST with json containing cpf_cnpj and would like you to return the above data to me.

  • 1

    And what he actually returns?

  • I do not understand the question. I confess that I am a beginner in the area. But explaining in general: I need to send a CPF via POST to http://mobi.ieptb.org.br/consulta. The answer to the request is the Json above in the question. I wanted to read this data (Uf, code_cartorio, data_protesto and etc.)

  • var_dump($jsonRet); exit add this to your code at the end and put what output was generated.

  • Dude, did you test the URL? Ta returning server error 500. You’ll never get anything.

  • The return was NULL. However, in Talend which is an API tester it is returning normal; I put the print in the question

  • William the problem is what you passed in json_encode cc @Andersoncarloswoss

Show 3 more comments

2 answers

3


You created this which is a string:

$array = '{ 
    "cpf_cnpj": "83899526000182"
}';

And tried applying json_encode:

$json = json_encode($array);

That is to say nay goes to Curl like this:

{ 
    "cpf_cnpj": "83899526000182"
}

Go like this:

"{ \n    \"cpf_cnpj\": \"83899526000182\"\n}"

this because simply you want a string to turn json, which has no sense, just pass the string straight, since it already has the json format:

<?php 
$json = '{ 
    "cpf_cnpj": "83899526000182"
}'; 

$ch = curl_init('http://mobi.ieptb.org.br/consulta');

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");

curl_setopt($ch, CURLOPT_POSTFIELDS, $json);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_HTTPHEADER, array(

    'Content-Type: application/json',

    'Content-Length: ' . strlen($json))

);


$jsonRet = json_decode(curl_exec($ch));

Or if you want to use an array to manipulate the value of cpf_cnpj before sending, just use an array even, example if it came from a POST:

<?php
if (empty($_POST['cpfCnpj'])) die('informe o cpf');

$array = array( 
    "cpf_cnpj" => $_POST['cpfCnpj']
);

$json = json_encode($array);

$ch = curl_init('http://mobi.ieptb.org.br/consulta');

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");

curl_setopt($ch, CURLOPT_POSTFIELDS, $json);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_HTTPHEADER, array(

    'Content-Type: application/json',

    'Content-Length: ' . strlen($json))

);

$jsonRet = json_decode(curl_exec($ch));

Another possibility is that the target server is targeting HTTPS, meaning your PHP is not configured in php.ini for secure connections, so to do this follow the tips of these answers:

Which is basically enabling openssl in php.ini and setting up the certificate in php.ini more or less like this:

curl.cainfo = /caminho/cacert.pem

You can download the certificate on: https://curl.haxx.se/ca/cacert.pem

If you can’t or are developing something fast is to turn off the security check like this:

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

$jsonRet = json_decode(curl_exec($ch));

Of course, this will leave the data exposed to netizers halfway through, so as soon as possible set up SSL on Curl, if your hosting is having this problem then ask Helpdesk for it to enable security (but I think it is unlikely a stay without this)

  • I understand. But the return is still NULL. I performed the test in Talend (API tester) and it returned the query normally (I put the print in the question)

  • @Guilhermeduarte tested here on my PC and worked perfectly see the test: https://repl.it/@Guilhermenasci5/PHP-Curl

  • @Guilhermeduarte this site mobi.ieptb.org.br is unstable, there are times that do not open either by Talend, Postman, etc. Here are times that go and there are times that do not work.

  • He’s been through some instability today, but apparently he’s back to normal

  • @Guilhermeduarte blz, tested now, see worked normally: https://i.stack.Imgur.com/3X99E.png, something else you need? It’s working there?

  • I put your code in my htdocs. The return is always bool(false). By Postman and Talend the requests are ok

  • @Guillermoduarte after $jsonRet = json_decode(curl_exec($ch)); add this var_dump(curl_error($ch)); and take the result send me here.

  • string(34) "Recv Failure: Connection was reset"

  • Using the link code https://repl.it/@Guilhermenasci5/PHP-Curl, the result is bool(false)

  • @Guilhermeduarte may be firewall, but do the following test, add this after curl_init: curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);&#xA;curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);, if it works is because the site is redirecting to HTTPS, and your php is not configured with OPENSSL

  • It worked. Thanks @Guilhermenascimento.

  • @Guillermoduarte please mark the answer as correct, grateful!

Show 7 more comments

0

I have a simpler method with file_get_contents. Do a test there.

<?php

    $url = 'http://mobi.ieptb.org.br/consulta';

    $body = '{"cpf_cnpj": "83899526000182"}';

    $opts = [
        'http' =>
            [
            'method'  => 'POST',
            'header'  => "Content-Type: application/json\r\n". 'Content-Length: ' . strlen($body),
            'content' => $body,
            'timeout' => 60
        ]
    ];
    $context  = stream_context_create($opts);
    $a = file_get_contents($url, false, $context, -1, 40000);
    echo '<pre>' ;
    print_r(json_decode($a));

Expected exit

[0] => stdClass Object
        (
            [uf] => SC
            [codigo_cartorio] => 1
            [codigo_cidade] => 4205407
            [descricao] => 2º TABELIONATO DE NOTAS E 1º PROTESTO DE TÍTULOS
            [endereco] => Rua Tenente Silveira, 221 , Centro - Florian├│polis - SC - Telefone: 4830391991
            [telefone] => 4830391991
            [data_atualizacao] => 16/10/2019
            [quant_protestos] => 1
            [protestos] => Array
                (
                    [0] => stdClass Object
                        (
                            [data_protesto] => 15/05/2018
                            [valor_protestado] => R$88.545,00
                            [valor_protestado_o] => 88545
                        )

                )

        )
......

Browser other questions tagged

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