首页 > 编程语言 >PHP 如何获取 POST 请求发送的 JSON 对象参数 All In One

PHP 如何获取 POST 请求发送的 JSON 对象参数 All In One

时间:2022-12-14 21:57:31浏览次数:52  
标签:obj name json value echo JSON post PHP POST

PHP 如何获取 POST 请求发送的 JSON 对象参数 All In One

php get post json data

# Get JSON as a string
$json_str = file_get_contents('php://input');

# Get as an object
$json_obj = json_decode($json_str);


if($json_obj !== null) {
  // metric = {delta,entries,name,id,value,}
  echo "<h1 style=\"color: #0f0; background: #000;\">post json string = \n$json_str</h1>";
  echo "<h1 style=\"color: #0f0; background: #000;\">post json obj = \n $json_obj</h1>";
}

demos


# Get JSON as a string
$json_str = file_get_contents('php://input');

# Get as an object
$json_obj = json_decode($json_str);


if($json_str !== null) {
  echo "<h1 style=\"color: #0f0; background: #000;\">post json string = \n$json_str</h1>";
}

if($json_obj !== null) {
  // metric = {delta,entries,name,id,value,}
  // $name = $json_obj["name"];
  // $id = $json_obj["id"];
  // $value = $json_obj["value"];
  // // echo "<h1 style=\"color: #0f0; background: #000;\">post json obj = \n $json_obj</h1>";
  // echo "<h1 style=\"color: #0f0; background: #000;\">post json obj.name = \n $name</h1>";
  // echo "<h1 style=\"color: #0f0; background: #000;\">post json obj.id = \n $id</h1>";
  // echo "<h1 style=\"color: #0f0; background: #000;\">post json obj.value = \n $value</h1>";
  foreach ($json_obj as $key => $value) {
    echo "<h1 style=\"color: #0f0; background: #000;\">post json obj.value = \n $value</h1>";
    // Warning: Array to string conversion in /usr/local/var/php8/analytics.php on line 56
  }
}

(

标签:obj,name,json,value,echo,JSON,post,PHP,POST
From: https://www.cnblogs.com/xgqfrms/p/16983703.html

相关文章