php - Why I can't parse JSON decode response
Get the solution ↓↓↓I would like some help me before asking I've tried in several ways and it don't works
Here is the JSON response I get
{
"header": {
"api_version": "3.0.0",
"type_connection": 1,
"user": "",
"request_time": "2021-07-11 02:16:28",
"access_ip": null,
"error": null,
},
"result": [
{
"pessoa": ""{
"cadastral": ""{
"number": "41259288231",
"tipoLogradouro": "AVENIDA",
]
}}}]}
Here is the CURL
if (validNB($number)) {
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => 'localhost/cpf_json.php?token=235424343==&number='.$number.'',
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_ENCODING => 'gzip',
CURLOPT_HTTPHEADER => array(
'Accept-Encoding: gzip, deflate',
'Accept-Language: pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7',
'Cache-Control: no-cache',
'Connection: keep-alive',
'Pragma: no-cache',
'User-Agent: Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Mobile Safari/537.36'
),
));
echo $Procurar = curl_exec($ch);
$json = json_decode($Procurar, true);
sleep($SleepTime);
$gettime = $json[0]['header']['request_time'];
$checkerror = $json[0]['header']['error'];
$logra = $json[0]['pessoa']['cadastral']['tipoLogradouro'];
if($checkerror == null){
echo json_encode(array("status" => 1, "cartao" => 'Number: '.$number.' ', "separador" => '|',"retorno" => ' Test Get Time: '.$gettime.' | Test get error value: '.$checkerror.' test A error is null '));
} else {
echo json_encode(array("status" => 0, "cartao" => 'Number: '.$number.' ', "separador" => ' ', "retorno" => ' Test Get Time: '.$gettime.' | Test get error value: '.$checkerror.' test B Error is not null | test logra: '.$logra.' '));
}
Something is wrong. If I access manually URL I can see the response correctly, but the value of my string is not being shown on echo. Any idea how to solve it?
My if echo currently is:
{"status":1,"cartao":"Number: ","separador":"|","retorno":" Test Get Time: | Test get error value: test A "}
No strings value has printted
Thanks in advance
Edit:
I can't print string values on echo encode =( Before that, this code has already worked. Even without close curl, I just took what already worked and modified it to parse this JSON response Don't know why it's not working, the fact of it do not show string values make me believe that is something when I'm trying get values.
According to the structure of the obtained JSON response, would this syntax be wrong?
$gettime = $json[0]['header']['request_time'];
$checkerror = $json[0]['header']['error'];
$logra = $json[0]['pessoa']['cadastral']['tipoLogradouro'];
This would explain why I can't print the value
Answer
Solution:
Here is the solution:
Decode the json response correctly
$json = json_decode($string, true);
Then, pretty print your output
print("<pre>".print_r($json,true)."</pre>");
follow the sequence of arrays and objects to find the right sintax in this case:
"result": [
{
"pessoa": ""{
"cadastral": ""{
"number": "41259288231",
"tipoLogradouro": "AVENIDA",
]
}}}]}
You can access this response values with:
$getnumber = $json['result'][0]['pessoa']['cadastral']['number'];
$gettipoLogradouro = $json['result'][0]['pessoa']['cadastral']['tipoLogradouro'];
I thank you for all the help you tried to give me, despite not solving the problem itself, it is necessary to recognize that the community is receptive and willing to help, as much as possible.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: php undefined array key
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.