0

I'm implementing GCM using php server. I'm getting the response in JSON as shown:

{"multicast_id":5500424824887517718,"success":1,"failure":1,"canonical_ids":1,"results":[{"message_id":"0:1401303281549959%31e4cc17f9fd7ecd","registration_id":"APA91bGiXGz2KgfS1kELa7YM4VEEnnAk1H2_isOnWzJ0lNCJEmICQhjWaxQXJ0riZatG1LvZSRUYMNHF"},{"error":"NotRegistered"}]}  

Now from the JSON, I have to check that is there any error in the response. I'm not getting how to check the same.

EDIT: I have understood how I can reach to the "error" field. But now the error is I'm not able to reach to "registration_id" field. I just have to check that does this "registration_id" field exist at any index of "results"?

Please help me regarding the same.

Thanks in advance.

Akshay Sethi
  • 803
  • 3
  • 13
  • 22

1 Answers1

2

Check out PHP's json_decode function. This will turn the JSON into a PHP array that you can query for the error.

http://www.php.net/manual/en/function.json-decode.php

Edit

$php_array = json_decode('{...}');  // Turn JSON into PHP array
$results = $php_array['results'];
foreach($results as $result) {  // Loop through results
    if (array_key_exists('registration_id', $result)) {  // Check if entry has a registration_id
        $registration_id = $result['registration_id'];  // Record registration_id
    }
}
Dylan
  • 1,372
  • 11
  • 13
  • I have done that part. The problem I'm facing is that I'm not able to check that the field named "registration_id" exists at any index of "results" array. if yes, i have to save that position. – Akshay Sethi May 28 '14 at 21:14
  • @AkshaySethi you still use json_decode, I updated my response – Dylan May 29 '14 at 00:52