从json_decode

时间:2016-09-02 18:01:28

标签: php json ajax vxml

我有一个vxml脚本,用于捕获post参数以向用户发送文本消息以收集他们的电子邮件。

这是脚本

     <?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>

当我输出var_dump($ json)时,它输出这个并发送短信。

<?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>

我对json_decode并不是很熟悉。我如何捕获它在变量中输出的参数,以便我可以将此信息上传到我的数据库?我尝试在$ message_reference中捕获它们(当我的var_dump($ message_reference);它返回null)。

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

$ message_reference返回null,因为您正在尝试访问不存在的属性。输出数组显示密钥是sms_messages,而不是sms_message。

除此之外,sms_messages是一个数组。所以要访问消息,它将是$ json [&#39;结果&#39;] [&#39; sms_messages&#39;] [0] - 这将为您提供一组消息值。

所以我想你可以这样做:

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

等等该数组中的其他值。

如果你希望这个curl请求一次返回多个消息,你会看到这样做,给定响应的结构,你必须把它放在foreach循环中。哪个会更像这样......

$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.
}
相关问题