首页 > 解决方案 > 如何在 PHP 中更正 json?

问题描述

在第 1 页,表单由服务器上的数据留下,在第 2 页返回数据,但我无法获取它们,我保存在文件中,有数据,如果我不保存,则无法捕获它们

$postdata = file_put_contents('file.txt', json_decode(file_get_contents('php://input')

并在文件中[{"TourDate": "23.01", "Nights": "7", "City": "\u0428\u0430\u0440\u043c-\u044d\u043b\u044c-\u0428\u0435\u0439\u0445", "Hotel": "ALBATROS AQUA BLU RESORT SHARM EL SHEIKH 4+ *", "Board": "AI", "Accomodation": "1 (18+)", "Price": "836", "Month": "01"}]

我尝试 json_encode,解码,什么也没发生

如何获取数据并使用它们?

标签: php

解决方案


$postdata = file_put_contents('file.txt', json_decode(file_get_contents('php://input');

$tour = json_decode_custom(file_get_contents('file.txt'));

$tour->TourDate; // 23.01
$tour->Nights; // 7
...

function json_decode_custom($json)
{
    return json_decode(trim($json, "[]"));
}

推荐阅读