Converting a json object to an array


Using restful web services relies on json results. You need your json object to be decoded and then converted to a array. Using the set library from CakePHP we can accomplish this. In this example I send a message to Android push gateway, then attempt to read the result.

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, '');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Authorization: key= ENTER_YOUR_KEY',
    'Content-Type: application/json'
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array(
   'registration_ids' => $registation_ids,
   'data' => array("m" => $message),

$result = curl_exec($ch);
if ($result === FALSE) {
   // do error handling
   debug('curl request failed');

// The json decoding / converting line
$data = Set::reverse(json_decode($jsonresult));

The data variable now contains an array of information ready to parse.

Leave a Reply

Your email address will not be published.