显示来自Request Body POST的JSON数据

时间:2014-06-15 11:08:46

标签: php json

我找到了以下测试代码(sendJSON.php):

<?php
$article = new stdClass();
$article->UserName = "JohnDoe";
$article->userPassword = "MyPassword";

$json_data = json_encode($article);

$post = file_get_contents('readJSON.php',null,stream_context_create(array(
'http' => array(
    'protocol_version' => 1.1,
    'user_agent'       => 'PHPExample',
    'method'           => 'POST',
    'header'           => "Content-type: application/json\r\n".
                          "Connection: close\r\n" .
                          "Content-length: " . strlen($json_data) . "\r\n",
    'content'          => $json_data,
),
)));

if ($post) {
    echo $post;
} else {
    echo "POST failed";
}
?>

尝试从服务器端readJSON.php中的Request Body获取$ json_data。这是我简单的readJSON.php文件:

<?php
  echo file_get_content("PHP://input");
?>

问题是服务器端页面没有回显?

0 个答案:

没有答案