PHP 接收 JSON 参数:完整指南及示例代码
在 PHP 中,可以通过 $_POST 来接收 JSON 参数。首先需要将传递的 JSON 参数解码为 PHP 数组,然后就可以像操作普通数组一样操作它。
示例代码如下:
$json = file_get_contents('php://input');
$data = json_decode($json, true);
// 现在可以像操作普通数组一样操作 $data
echo $data['name'];
echo $data['age'];
在上面的代码中,file_get_contents('php://input') 获取了传递的 JSON 参数,json_decode 将其解码为 PHP 数组。最后,可以通过 $data 数组来访问其中的参数。
原文地址: https://www.cveoy.top/t/topic/mkTT 著作权归作者所有。请勿转载和采集!