I have this bit of php that retrieves a JSON string:
$login = "http://$server/auth/login/$username/$password";
$json = file_get_contents($login);
The JSON received is this:
{
"Return":{
"Type":"auth.login",
"ResponseSummary":{
"StatusCode":0,
"ErrorMessage":""
},
"Results":{
"SessionID":"fejqciel23dcjqvsumjdplq6s6",
"User":{
"Id":"*****",
"AccountId":"*****",
"FirstName":"*****",
"LastName":"******",
"Email":"*****@*****.***",
"UserName":"*****@*****.***",
"Group":"3",
"Language":"English",
"Measurement":"Imperial",
"Timezone":"Etc\/GMT+8",
"MsgFlag":"0",
"Pincode":"",
"Phone":"",
"TerminationDate":null,
"DaylightSavings":"0",
"AcceptedWebsiteHardwareTc":"0",
"APIContractAccepted":null,
"DisplayNotice":"1",
"NoticeInterval":"1 day",
"TemporaryPassword":false,
"Roles":[3, 11],
"UserCode":******,
"AccountName":"*****@*****.***",
"CustomAttributes":[]
},
"AccountLastChangeDate":"2015-05-27 06:13:28"
}
}
}
I am trying to parse out the SessionID. It's the only bit I need. I've followed several other articles here on SO but either get nothing or errors.
A couple of things I've tried:
$jsondata = json_decode($json, true);
echo $jsondata['Return'][0]['Results'][0]['User'][0]['SessionID'];
and
$jsondata = json_decode($json);
echo $jsondata->Return[0]->Results[0]->User[0]->SessionID;
and numerous variations on these. I either get nothing at all or various errors.
What's the correct way to extract the SessionID?
I've hacked it to work using a couple string.explode statements but I'd prefer to do it correctly.
Thanks!