Webservice PHP+JSON - How to read return

Asked

Viewed 526 times

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.

2 answers

1

try this

$retorno = json_decode($jsonRet); $status = $retorno['data']['status'];

  • I tested like this: $return = json_decode($jsonRet); $status = $return['data']['status']; echo $status; * But no display, error occurred in syntax...

  • give a var_dump($return); and post here q was displayed

  • Parse error: syntax error, Unexpected '$return' (T_VARIABLE)

  • it seems that $jsonRet n is returning a value; try a var_dump of it to see q appears

  • 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]",

  • $return = json_decode($jsonRet); turns obj json into array... vc can post the code q generates the return?

  • 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...

  • 1

    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

  • Perfect! = D ... Can you put this excerpt as an answer? So mark as solved to finish

Show 4 more comments

1


$retorno = json_decode($jsonRet); $status = $retorno->data->status;

  • Solved! Thank you very much...

Browser other questions tagged

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