Mike
Mike

Reputation: 415

How to extract the error message from a GCM json decoded response

I am fairly new with PHP and I'm stuck on this big time.

I am trying to extract the error message "InvalidRegistration" in this instance, which appears to be in an array within an array and then filter and act upon it in my PHP server.

Note that the error array may have a greater depth than 1 on multicast messages to multiple regId's.

Any help will be greatly appreciated, thanks!


Dumped GCM server response:

object(stdClass)#3 (5) { ["multicast_id"]=> int(6225919148914620552) ["success"]=> int(0) ["failure"]=> int(1) ["canonical_ids"]=> int(0) ["results"]=> array(1) { [0]=> object(stdClass)#4 (1) { ["error"]=> string(19) "InvalidRegistration" } } }

Send message code:

    $dbf = new push_db_functions();  

    // a row id which contains an intentionally bad regId
    // to trigger the error from 'test' mySql database
    $id = '19'; 

    $result = $dbf->sendMessage($id);

    // dump GCM server response  
    $obj2 = json_decode($result);  
    var_dump($obj2);

   // TODO: (Question Subject)
   // how to extract and test for if('failure' == 1) { ...?
   // and then how to extract 'error' message so I can act upon it appropriately?

Send message helper code:

public function sendMessage($id) {
    $sqlquery = "SELECT regid FROM test WHERE Id = '$id'";
    $results = mysql_query($sqlquery);
    $processed = mysql_fetch_row($results);
    $url = 'https://android.googleapis.com/gcm/send';
    $apiKey = "my api key";
    $message = "Hello World";
    $fields = array('registration_ids' => $processed, 'data' => array( "message" => $message),);
    $headers = array('Authorization: key=' . $apiKey, 'Content-Type: application/json');
    // open connection
    $ch = curl_init();
    // set the url, number of POST vars, POST data
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
    // execute post
    $response = curl_exec($ch); 
    curl_close($ch);
    return $response;
}

Upvotes: 1

Views: 1302

Answers (2)

Nishchit
Nishchit

Reputation: 19074

Simple decode response in JSON

 $data = json_decode($response); 

The output of this if

 
{
    "multicast_id": 5020929399500020011,
    "success": 0,
    "failure": 1,
    "canonical_ids": 0,
    "results": [{
        "error": "NotRegistered"
    }]
}

And now you can easily parse error and result in json.

Hope this will help you guys.

Upvotes: 1

adomnom
adomnom

Reputation: 574

To just grab the error code given, you should be able to use

$obj2->results[0]->error;

But if you want to do it flexibly, you may wish to do something more along the lines of...

$errors = array();

if( !empty($obj2->results) ) {
    foreach( $obj2->results as $result ) {
        $error = $result->error;
        // Do whatever you want with the error here. 
        // In this instance, I'm just putting it into a fancy array
        $errors[] = $error;
    }
}

// $errors = array( [0] => 'InvalidRegistration' );

Upvotes: 0

Related Questions