从json获取数据并传递给url

时间:2017-12-10 10:49:10

标签: php arrays json parsing

想要将json数据发送到我的url,当收到json时,我会收到url上的所有json数据,而不是'resolvedQuery'中的数据

$update_response = file_get_contents("php://input");
$update = json_decode($update_response, true);

$results = $update['result']['resolvedQuery'];

$url = "https://autoremotejoaomgcd.appspot.com/sendmessage?key=APA91bEq&message=Data" . urlencode(json_encode($update));

Json Data

    {
  "id": "4ed272a3-b9a4-4f18-a67e-10e12e4f4149",
  "timestamp": "2017-12-10T10:07:54.282Z",
  "lang": "en",
  "result": {
    "source": "agent",
    "resolvedQuery": "turn livingroom light on",
    "action": "",
    "actionIncomplete": false,
    "parameters": {
      "power-toggle": "on",
      "room": "livingroom"
    },
    "contexts": [],
    "metadata": {
      "intentId": "19764ecc-715c-4b25-960f-9845f2aef1f9",
      "webhookUsed": "true",
      "webhookForSlotFillingUsed": "false",
      "webhookResponseTime": 503,
      "intentName": "lights"
    },
    "fulfillment": {
      "speech": "Ok, Turning livingroom light on",
      "messages": [
        {
          "type": 0,
          "speech": "Ok, Turning livingroom light on"
        }
      ]
    },
    "score": 1
  },
  "status": {
    "code": 206,
    "errorType": "partial_content",
    "errorDetails": "Webhook call failed. Error: Webhook response was empty.",
    "webhookTimedOut": false
  },
  "sessionId": "395d3517-05da-42ac-9037-dadac519ab0b"
}

如何从'resolvedQuery'获取数据并将其发送到我的网址

1 个答案:

答案 0 :(得分:0)

在您提供的代码中,您有以下代码行

$url = "https://autoremotejoaomgcd.appspot.com/sendmessage?key=APA91bEq&message=Data"
        . urlencode(json_encode($update));

您要将message的结果附加到网址参数json_decode($update_response, true)$update_response包含HTTP请求发布的所有已解码的JSON数据,这不是您想要的。

我相信你想要的是一个名为resolved-query的新参数,设置为json_decode($result, true)的结果。

尝试将您的$url变量修改为以下内容:

$url = "https://autoremotejoaomgcd.appspot.com/sendmessage?key=APA91bEq&message=Data&resolved-query="
        . urlencode(json_encode($result));

URL将设置为以下字符串:

https://autoremotejoaomgcd.appspot.com/sendmessage?
    key=APA91bEq&
    message=Data&
    resolved-query=turn+livingroom+light+on