POST的JSON从Python到PHP-收到“空”

时间:2018-09-25 11:38:02

标签: php python json

我正在尝试学习JSON,并创建了一个非常基本的PHP脚本,该脚本可以接收来自Python脚本的JSON请求,并仅返回JSON请求。一切工作正常,但由于某种原因,返回给Python脚本的值是“ null”。

PHP:

<?php

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

?>

Python:

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}

有人可以解释为什么吗?

0 个答案:

没有答案
相关问题