I have this nested object generated from accessing the Login of the API. The "Token" will be different for each user login. To access other parts of the API, user have to login first. To do that, the "Token" value is required.
So now I am trying to extract the "Token" value from the nested object and place it in the header part of the POST request. Any idea how I can access the nested object?
Many thanks.
{
"Data": {
"Id": 2,
"Token": "2:BxeFG9E28lnG5gB5GiB1NgxXBPGjTl",
"Name": "Administrator",
"ClinicID": 0,
"Admin": true,
"UserAccessRight": {
"id": 1,
"dateCreated": "0001-01-01T00:00:00",
"lastUpdated": "0001-01-01T00:00:00",
"userid": 2,
"waitingQueue": 2,
"patient": 2,
"appointment": 2,
"consultation": 2,
"dispensary": 2,
"inventory": 2,
"report": 2,
"setting": 2
}
},
"Success": true
}
I have tried this but fail
<?php require 'vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\Request;
$client = new Client([
'headers' => [ 'Content-Type' => 'application/json' ]
]);
$response = $client->post('http://localhost:81/Login',
['body' => json_encode(
[
"username" => "admin",
"Password" => "123456"
]
)]
);
echo '<pre>' . var_export($response->getStatusCode(), true).'</pre>';
echo '<pre>' . var_export($response->getBody()->getContents(),
true) . '</pre>';
$object = '<pre>' . var_export($response->getBody()->getContents()
, true) . '</pre>';
echo $object;
$yummy = json_decode("$object");
echo $yummy['Data'][1]->Token;