关于basicCard响应格式化文本的转义语句未解析

问题描述 投票:0回答:1

我的网站上有一个php脚本,这个动作作为webhook链接到它。因此,一旦将数据提交给服务器,这就是我在json中发送的响应。

`{"payload":
 {"google":
  {"expectUserResponse":true,"
     richResponse":
      {"items":[{ 
          "simpleResponse":{
            "textToSpeech":"This feature is coming soon! Please wait until you receive a notification about its launch! You can verify if the details collected for your entry were right and inform the creator if they weren't. Here are the details."
          }
         },{"basicCard":{"title":"This is the entry I made","subtitle":"But I couldn't submit it","formattedText":"Item: Books  \\nRemarks: McDonalds  \\nDate: 2019-02-17T12:00:00+05:30  \\nAmount: 2098  \\nCategory: Expense  \\nIf the details above aren't right, please inform the creator."}}]}}}}'

但没有一个\ n得到解析。我实际上在php中输入的是一个双空格后跟单反斜杠然后n但是由于某种原因,php添加了一个额外的反斜杠,防止它逃脱,即使我使用json编码json_unescaped_slashes。这是我用来创建json的php代码。

            $response=new \stdClass();
            $response->payload->google->expectUserResponse= true;
            $items=new \stdClass();
            $res->simpleResponse->textToSpeech="This feature is coming soon! Please wait until you receive a notification about its launch! You can verify if the details collected for your entry were right and inform the creator if they weren't. Here are the details.";
            $items->basicCard->title="This is the entry I made";
            $items->basicCard->subtitle="But I couldn't submit it";
            $items->basicCard->formattedText="Item: ".$type."  \nRemarks: ".$item."  \nDate: ".$date."  \nAmount: ".$amount."  \nCategory: ".$category."  \nIf the details above aren't right, please inform the creator.";
            $response->payload->google->richResponse->items[]=json_encode($res, JSON_UNESCAPED_SLASHES).",".json_encode($items, JSON_UNESCAPED_SLASHES);
            $response2=str_replace('\"','"',json_encode($response, JSON_UNESCAPED_SLASHES));
            $response2=str_replace('"{','{',$response2);
            $response2=str_replace('}"','}',$response2);
            echo $response2;

这是它在basicCard响应https://photos.app.goo.gl/RzG5H3VgTJfrgfB59中出现的截图的链接

php httpresponse webhooks actions-on-google
1个回答
0
投票

问题是您将对象编码为JSON作为中间步骤,而不是首先在PHP中构建对象然后对其进行编码。因此,您会遇到一些奇怪的双重编码问题。

做这样的事情

  $msg=new \stdClass();
  $msg->simpleResponse->textToSpeech="Coming soon";

  $card=new \stdClass();
  $card->basicCard->title="This is the entry I made";
  $card->basicCard->subtitle="But I couldn't submit it";
  $card->basicCard->formattedText="Item: ".$type."  \nRemarks: ".$item."  \nDate: ".$date."  \nAmount: ".$amount."  \nCategory: ".$category."  \nIf the details above aren't right, please inform the creator.";

  $response=new \stdClass();
  $response->payload->google->expectUserResponse= true;
  $response->payload->google->richResponse->items = array(
    $msg,
    $card
  );

  $json = json_encode( $response );

更像是你想做的事情。

© www.soinside.com 2019 - 2024. All rights reserved.