从 json_decode 捕获 Json 数据变量

Catching Json data variables from json_decode

我有一个 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(when i var_dump($message_reference); 它 returns null).

中捕获它们

有什么建议吗?

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

除此之外,sms_messages是一个数组。因此,要访问该消息,它将是 $json['result']['sms_messages'][0] - 这将为您提供一个消息值数组。

所以我想你可以这样做:

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

对于该数组中的其他值,依此类推。

如果您希望此 curl 请求一次发送 return 多条消息(看起来确实如此),则考虑到响应的结构,您必须将其放入 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.
}