0

From the service request I get the following response and I have tried json_decode to parse it but it does not work. I need to check if ["body"]->enquiry->status is “OPEN”. Could anyone tell me how to parse this response?

array(5) {
    ["headers"] => array(5) {
        ["server"] => string(17)
        "Apache-Coyote/1.1" ["content-type"] => string(16)
        "application/json" ["content-length"] => string(3)
        "313" ["date"] => string(29)
        "Fri, 08 Jul 2016 00:22:29 GMT" ["connection"] => string(5)
        "close"
    }
    ["body"] => string(313){
        "version ":{
            "major ":1,
            "minor ":6,
            "revision ":0
        },
        "enquiry ":{
            "id ":"21a2a688-c09b-48bc-8cb0-0ad596c18447",
            "creationTime ":1467937344745,
            "lastUpdateTime ":1467937344753,
            "status ":"OPEN ",
            "from ":"test ",
            "email ":"[email protected] ",
            "message ":"test ",
        },
        "enquiries":null
    } 
    ["response"] => array(2) {
        ["code"] => int(202)
        ["message"] => string(8)
        "Accepted"
    }
    ["cookies"] => array(0) {}
    ["filename"] => NULL
}
3
  • 1
    just access the appropriate index, decode the json string with an array flag, then treat it like any normal array Commented Jul 8, 2016 at 1:41
  • The code above is the response or the response decode with json_decode ? I want to see the raw response. Commented Jul 8, 2016 at 1:42
  • This is the raw response that I get from a service. I am actually new to PHP, any help is much appreciated. Commented Jul 8, 2016 at 1:44

1 Answer 1

1

Here's how to access it. I used the $response as the response you gave us.

//We decode the 'body' from the response to json and we convert it to an array
$body = json_decode($response['body'],true);

//We access the status
$status = $body['enquiry']['status'];

Also, be careful if you want to check if the status is equal to 'OPEN'. In you response, the status is actual OPEN. Notice the trailing space. You can use trim($status) to remove the spaces.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much, I am able to get the status now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.