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)
What is the value of
$jsonRet
in the end?– Woss
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.
– Guilherme Duarte
And what he actually returns?
– Woss
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.)
– Guilherme Duarte
var_dump($jsonRet); exit
add this to your code at the end and put what output was generated.– Woss
Dude, did you test the URL? Ta returning server error 500. You’ll never get anything.
– Lucas de Carvalho
The return was NULL. However, in Talend which is an API tester it is returning normal; I put the print in the question
– Guilherme Duarte
William the problem is what you passed in json_encode cc @Andersoncarloswoss
– Guilherme Nascimento