我正在尝试学习JSON,并创建了一个非常基本的PHP脚本,它接收来自Python脚本的JSON请求并简单地返回JSON请求。一切正常,只是Python脚本的返回值出于某种原因为“null”。
菲律宾比索:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// read JSON input
$data = json_decode(file_get_contents("php://input"));
// JSON to variable
$message = $data->message;
// Set header
header("Content-type: application/json");
// Create and send JSON response
$response = array('message' => $message);
echo json_encode($response);
}
?>
from urllib.parse import urlencode
from urllib.request import Request, urlopen
# Variables
url = 'http://www.some-website.com/function.php'
post_fields = {"message":"It works!"}
request = Request(url, urlencode(post_fields).encode())
json = urlopen(request).read().decode()
print(json)
Python中的响应:
{"message":null}
有人能解释为什么吗?