Krunkmaster Krunkmaster - 1 year ago 67
Ajax Question

Catching Json data variables from json_decode

Greetings stack exchange.

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

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'] = "";

//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, "");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, "");

$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

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

<vxml version="2.0">
<log><? print_r($params) ?></log>
hello <value expr="'<? echo($CALLER) ?>'"/>
<return namelist=""/>

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

<?xml version="1.0"?>
<vxml version="2.0">
[to] => 18159856396
[from] => 16172075679
[body] => Please Respond To This Text Message With Your Email Address So We Can Better Serve You.
[result_url] =>
hello <value expr="'18159856396'"/>
array(3) {
string(7) "success"
string(0) ""
array(1) {
array(1) {
array(7) {
string(32) "5a6c5c7114a74679861209c27a083542"
string(11) "18159856396"
string(10) "6172075679"
string(87) "Please Respond To This Text Message With Your Email Address So We Can Better Serve You."
string(49) ""
string(6) "queued"
<return namelist=""/>

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?

Answer Source

$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.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download