0

When I make a request to my webservice one time I get:

funcion login returns => {
    "token":"fsuehfisubfibiefasasdapmwineq","client":"Admin","permission":"ADMIN"
}

And I can just save something like the client or the token in a variable, doing for example:

$json = login($email,$password); - saving the JSON in the variable $json.
$values = json_decode($json); - decoding the $json and saving it on $values.

And know if I want to save the name or the token for future requests to the webservice I make a variable:

$token = $values->{'token'};
$client = $values->{'client'};

But my question is what i do when i get this from the webservice, because I did the same and tried somethings and I'm not getting anywhere.

funcion member returns = {
    "Table":[
       {
        "client":"Admin",
        "name":"Martin Lupin",
        "age":"25",
        "city":"Lisbon"
       }
    ]
}

How do I access to "client", "name", "age", "city"?
I tried to do:

$name = $valuesclient->{'client'}; - and nothing
$name = $valuesclient->{'table'}->{'client'}; - and nothing
$name = $valuesclient->{'table'->{'client'}}; - and nothing

Can someone help me?

Dmitry S.
  • 1,544
  • 2
  • 13
  • 22
  • _Side note:_ `$values->{'token'}` is the same as `$values->token`. Some thing with `{'client'}`. You only need to use `{'the-property'}` if the name contains invalid characters. – M. Eriksson Apr 09 '21 at 09:13
  • If the json object has `Table`, then you need to use the same casing when fetching the prop: `$valuesclient->Table` so try `$valuesclient->Table->client` and so on. Variables and property names are case sensitive. – M. Eriksson Apr 09 '21 at 09:16
  • thank you for the information, i already tried like $finalvalues = $valuesclient->Table and is not working – David Rodrigues Apr 09 '21 at 09:16
  • it says in the browser (Attempt to read property "Table") – David Rodrigues Apr 09 '21 at 09:19
  • Do a `var_dump($valuesclient);` and check what it actually contains. You should also check your web servers error log to see if you get any messages there. If you get "nothing", it should also throw at least a warning about trying to access undefined properties. If you do see some error/warning on screen, then you _must_ add those to the question as well (the full messages). They are clues. – M. Eriksson Apr 09 '21 at 09:19
  • Does this answer your question? [How do I extract data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php) – El_Vanja Apr 09 '21 at 09:22
  • in the `var_dump` apperars - > `string(80) "{"Table":[{"client":"Admin","name":"Martin Lupin","age":"25","city":"Lisbon"}]}"` – David Rodrigues Apr 09 '21 at 09:23
  • What you have in your response is an array with a single object in it. So you just need to get the first element of the array and then access the properties you wish. If this response can contain multiple objects, then you'll need a loop. – El_Vanja Apr 09 '21 at 09:24
  • my response to the webservice is a funcion giving it the token that the webservice send me when i loged in like this. `function customer($token, $client){ $method = "GET"; $url = "http://Something...Something..."; $data_query = array( 'token' => $token, 'parameter' => $client );` – David Rodrigues Apr 09 '21 at 09:29
  • `$curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json;charset=utf-8')); curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data_query)); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($curl) if ($result == FALSE){ return "Erro: " . curl_error($curl); } else { return $result; } curl_close($curl); };` – David Rodrigues Apr 09 '21 at 09:29
  • i just dont know how i can make that "loop" – David Rodrigues Apr 09 '21 at 09:31
  • Read the link I proposed as duplicate. It explains nearly every conceivable way of reading JSON. – El_Vanja Apr 09 '21 at 09:33
  • If `$valuesclient` returns a string, you also need to json decode it before using at an object, just like you did with `$json`. – M. Eriksson Apr 09 '21 at 09:44
  • i tried all the link options and still not working but thanks – David Rodrigues Apr 09 '21 at 10:44
  • this is the code '$jsonclient = customer($values->{'token'},$values->{'cliente'}); $valuesclient = json_decode($jsonclient); echo $jsonclient; => puts in the browser {"Table":[{"client":"Admin","name":"Martin Lupin","age":"25","city":"Lisbon"}]}" echo $valuesclient; => puts in the browser Fatal error: Uncaught Error: Object of class stdClass could not be converted to string in C:\xampp\htdocs\Evolution\actions.php:19 Stack trace: #0 {main} thrown in C:\xampp\htdocs\Evolution\actions.php on line 19` – David Rodrigues Apr 09 '21 at 10:50

0 Answers0