xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

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

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
  }
}

(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!

refs

https://davidwalsh.name/php-json



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2022-12-14 21:47  xgqfrms  阅读(1017)  评论(4编辑  收藏  举报