phpjsonajaxvxml

Catching Json data variables from json_decode


I have a vxml script that catches a post parameter to send a text message to a user to collect their email.

Here is the script

     <?php
header("Content-type: text/xml");
echo "<?xml version=\"1.0\"?>\n";

$PIN = $_GET['pin'];
$CALLER = 1 . $_GET['callID'];
$params['to'] = $CALLER;
$params['from'] = "16172075679";
$params['body'] = "Please Respond To This Text Message With Your Email Address So We Can Better Serve You.";
$params['result_url'] = "http://hubenterprises.com.mx/ParseSMSresponse.php";

//initialize curl
$ch = curl_init();

// set necessary curl options
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_URL, "http://hosting.plumgroup.com/ws/sms/queue.json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, "alan@inboundprospect.com:Huba5474");

$result = curl_exec($ch);
// process the json result body
$json = (json_decode($result, true) !== null) ? json_decode($result, true) : false;
$success = false;
if ($json['status'] == 'success') {
   $success = true;
   $message_reference = $json['result']['sms_message']['sms_message_id'];//UPLOAD SMS MESSAGE ID
   //INSERT HERE


   // TODO: insert this value into your database with a pin or what have you for the user
}

curl_close($ch);
?>
<vxml version="2.0">
<form>
  <block>
      <log><? print_r($params) ?></log>
      hello  <value expr="'<? echo($CALLER) ?>'"/>
        <?
        var_dump($json);
        var_dump($message_reference);
        ?>
    <return namelist=""/>
  </block>
</form>
</vxml>

When i var_dump($json) it outputs this and the text message gets sent.

<?xml version="1.0"?>
<vxml version="2.0">
<form>
  <block>
      <log>Array
(
    [to] => 18159856396
    [from] => 16172075679
    [body] => Please Respond To This Text Message With Your Email Address So We Can Better Serve You.
    [result_url] => http://hubenterprises.com.mx/ParseSMSresponse.php
)
</log>
      hello  <value expr="'18159856396'"/>
        array(3) {
  ["status"]=>
  string(7) "success"
  ["error"]=>
  string(0) ""
  ["result"]=>
  array(1) {
    ["sms_messages"]=>
    array(1) {
      [0]=>
      array(7) {
        ["sms_message_id"]=>
        string(32) "5a6c5c7114a74679861209c27a083542"
        ["to"]=>
        string(11) "18159856396"
        ["from"]=>
        string(10) "6172075679"
        ["body"]=>
        string(87) "Please Respond To This Text Message With Your Email Address So We Can Better Serve You."
        ["result_url"]=>
        string(49) "http://hubenterprises.com.mx/ParseSMSresponse.php"
        ["request_timestamp"]=>
        int(1472838803)
        ["status"]=>
        string(6) "queued"
      }
    }
  }
}
NULL
    <return namelist=""/>
  </block>
</form>
</vxml>

I'm not really very familiar with json_decode. how do i catch the parameters it outputs in variables so i can upload this information to my database? I tried catching them in $message_reference(when i var_dump($message_reference); it returns null).

Any suggestions?


Solution

  • $message_reference returns null because you're trying to access a property that doesn't exist. The output array shows the key is sms_messages, not sms_message.

    In addition to that, sms_messages is an array. So to access the message it would be $json['result']['sms_messages'][0] - This would give you an array of the message values.

    So I suppose you could do something like this:

    $message_array = $json['result']['sms_messages'][0];
    $message_id = $message_array['sms_message_id'];
    

    And so on for the other values in that array.

    If you expect this curl request to return multiple messages at once, which it appears you do, given the structure of the response, you would have to put that in a foreach loop. Which would go something more like this..

    $messages = $json['result']['sms_messages'];
    foreach($messages as $message)
    {
        $message_id = $message['sms_message_id'];
        // assign additional keys to variables and do something with the data
        // for each message in the array.
    }