簡體   English   中英

從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中捕獲它們(當我var_dump($ message_reference)時,它返回null)。

有什么建議么?

$ message_reference返回null,因為您嘗試訪問的屬性不存在。 輸出數組顯示鍵為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請求立即返回多個消息,那么在給定響應的結構的情況下,您似乎會這樣做,則必須將其放入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.
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM