How to extract the error message from a GCM json decoded response
<p>I am fairly new with PHP and I'm stuck on this big time.</p>

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

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

<p>Any help will be greatly appreciated, thanks!</p>

<hr>

<p><em><strong>Dumped GCM server response:</em></strong></p>

<p>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) "<strong>InvalidRegistration</strong>" } } } </p>

<p><strong>Send message code:</strong></p>

<pre><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-&gt;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?
</code></pre>

<p><strong>Send message helper code:</strong></p>

<pre><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' =&gt; $processed, 'data' =&gt; array( "message" =&gt; $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;
}
</code></pre>

<hr>
