LoginSignup
2

More than 5 years have passed since last update.

php://inputを使い受け取ったJSONをそのまま返す

Last updated at Posted at 2016-12-14
<?php
echo file_get_contents('php://input');

もうちょっと細かいことやった版

<?php
sleep(1);
$data = json_decode(file_get_contents('php://input'), true);

header('Content-Type: application/json');
echo json_encode($data);

条件で失敗する版

わざと失敗する状況を作りたい場合に。

<?php
sleep(1);
$data = json_decode(file_get_contents('php://input'), true);

if ($data['bad']) {
  http_response_code(400);
  header('Content-Type: application/json');
  echo json_encode($data);
}
else {
  header('Content-Type: application/json');
  echo json_encode($data);
}

参照

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2