0
Hello!
Guys, I’d like to understand how to read the return, with the following structure::
$ws = array(
'categoria' => array(
array(
'cat1' => array(
'dado1' => $dado1,
'dado2'=>$dado2
),
'subcategoria'=>array(
'dado3'=>$dado3,
'dado4'=>$dado4,
)
),
),
);
$json = json_encode($ws, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK);
$url = 'https://link.com';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'Content-Length: ' . strlen($json),
"Content-Type: application/json",
));
$jsonRet = curl_exec($ch);
curl_close($ch);
With the above code I do the sending. Now how to extract the return data individually?
It comes in this format:
{"data":{"nome1":João,"status":["ativo"]}}
I want to be able to interpret only the status, or the nome1
, separately with php.
I tested like this: $return = json_decode($jsonRet); $status = $return['data']['status']; echo $status; * But no display, error occurred in syntax...
– Neo
give a var_dump($return); and post here q was displayed
– Alisson Almeida
Parse error: syntax error, Unexpected '$return' (T_VARIABLE)
– Neo
it seems that $jsonRet n is returning a value; try a var_dump of it to see q appears
– Alisson Almeida
I had forgotten to put the parentheses in the var_dump(); with the $return it is all blank, nothing appears on the screen. and with $jsonRet appears: string(208) "{"date":{"name1":John,"status":"[active]",
– Neo
$return = json_decode($jsonRet); turns obj json into array... vc can post the code q generates the return?
– Alisson Almeida
Yes, I put it in this format as you indicated at the beginning, json_decode($jsonRet); but in var_dump() nothing happens, the screen is empty...
– Neo
kkk espera ai I q erred here the correct would be $status = $return->data->status; try ai ai vc da um echo to see if prints the value
– Alisson Almeida
Perfect! = D ... Can you put this excerpt as an answer? So mark as solved to finish
– Neo