0

I have a soap request to a server which works successfully and return json data.

{"GetAccountResult":{"Header":{"Rquid":"D560BC95-24F2-F705-0585-7CCB38E37ECE","Status":{"StatusCode":"-4","Message":"Account not found","Details":null}},"Account":null}}

I would like to know how to access that in php and only print the Message? not the entire json results?

I have tried

$account   = $client->GetAccount($arrParams);
            $results = json_encode($account);

            echo $results->Message;

But this still prints the full json object

user1
  • 262
  • 1
  • 2
  • 13

4 Answers4

1

Here is the solution:

$account   = '{"GetAccountResult":{"Header":{"Rquid":"D560BC95-24F2-F705-0585-7CCB38E37ECE","Status":{"StatusCode":"-4","Message":"Account not found","Details":null}},"Account":null}}';
$results = json_decode($account,true);    // 2nd argument `true` returns array from JSON
echo $results['GetAccountResult']['Header']['Status']['Message'];
Himanshu Upadhyay
  • 6,558
  • 1
  • 20
  • 33
0
$result = json_decode($your_json, true);

$message = $result["GetAccountResult"]["Header"]["Status"]["Message"];

I would strongly recommend to use isset() to check if the indices exist. Otherwise you may end up with errors.

miile7
  • 2,547
  • 3
  • 23
  • 38
0

You can retrieve data by json_decode:

$json = '{"GetAccountResult":{"Header":{"Rquid":"D560BC95-24F2-F705-0585-7CCB38E37ECE","Status":{"StatusCode":"-4","Message":"Account not found","Details":null}},"Account":null}}';
$t =  json_decode($json,true);
print_r($t['GetAccountResult']['Header']['Status']['Message']);
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
rowmoin
  • 698
  • 2
  • 8
  • 17
0

Check this solution:

$account   = $client->GetAccount($arrParams);
$results   = json_decode($account);

echo $results->GetAccountResult->Header->Status->Message;