$_POST变量在发送POST请求时为空。

12 浏览
0 Comments

$_POST变量在发送POST请求时为空。

我正在尝试使用RestSharp将JSON发送到我的rest-api。基本上,我为json创建了一个模型类:

public class LogPostData

{

public string LogMessage { get; set; }

public string LogStackTrace { get; set; }

public string LogUserId { get; set; }

public string LogUserIp { get; set; }

}

所以我以这种方式进行请求:

var logPost = new LogPostData();

logPost.LogMessage = "日志消息"

logPost.LogStackTrace = "一些内容";

var post = JsonConvert.SerializeObject(logPost);

var client = new RestClient("rest api的URL");

var request = new RestRequest("methodApi", Method.PUT);

request.RequestFormat = DataFormat.Json;

request.AddParameter("application/json; charset=utf-8", post, ParameterType.RequestBody);

request.RequestFormat = DataFormat.Json;

var response = client.Execute(request);

如你所见,我创建了LogPostData对象,然后使用JsonConvert.SerializeObject进行了序列化。

我调用了methodApi,并将json作为参数传递。

现在,在我的rest api中,我做了以下操作:

file_put_contents('debug.txt',serialize($_POST));

内容应该是我使用RestSharp在post变量中发送的变量,但我得到的是:a:0:{}。

为什么我的$_POST变量是空的?

0
0 Comments

问题出现的原因是因为发送的请求中的Content-Type是application/json,而不是application/x-www-form-urlencoded。因此,$_POST变量为空。

解决方法是通过使用php://input来获取请求主体中的原始JSON数据,并使用json_decode将JSON数据反序列化为对象或关联数组。如果想要将数据转换为类似$_POST的关联数组形式,可以将json_decode的第二个参数设置为true。

文章内容如下:

根据PHP手册的说明,$_POST变量适用于application/x-www-form-urlencoded和multipart/form-data类型的内容。然而,在发送JSON(application/json)的情况下,$_POST变量为空并不奇怪,因为$_POST是根据表单数据创建的关联数组,而你并没有提交表单数据。

要从请求主体获取原始的JSON数据,你可以使用php://input:

$json = file_get_contents('php://input');

要将JSON数据反序列化为对象,你可以使用json_decode:

$logPostData = json_decode($json);

如果你希望将数据转换为像$_POST一样的关联数组形式,可以将json_decode的第二个参数设置为true:

$logPostData = json_decode($json, true);

如果你将请求的方法改为POST,但仍然得到空的$_POST变量,那是因为你发送的是application/json而不是application/x-www-form-urlencoded。

你可以在PHP脚本中修改头部,将Content-Type设置为application/x-www-form-urlencoded:

header('Content-Type: application/x-www-form-urlencoded');

同时,你还需要在C#中将请求设置为POST方式,并将参数设置为"application/x-www-form-urlencoded"类型的请求主体:

new RestRequest("methodApi", Method.POST);
request.AddParameter("application/x-www-form-urlencoded", post, ParameterType.RequestBody);

如果你检查$_POST的内容,发现它是a:0:{},说明你修改了内容类型为application/x-www-form-urlencoded,但实际上发送的是JSON类型的内容。这两种类型是不同的。你可以参考"Differences in application/json and application/x-www-form-urlencoded"了解更多信息。

如果你将内容类型更改为application/json,并且在PHP脚本中使用以下代码:

$rawData = file_get_contents("php://input");
$json = json_decode(rawData, true);
file_put_contents('debug.txt', $rawData);

但在文件中什么都没有,那是因为你没有正确获取到请求主体中的原始JSON数据。请将`$json = json_decode(rawData, true);`修改为`$json = json_decode($rawData, true);`。

0
0 Comments

当使用POST方法发送POST请求时,$_POST变量为空的原因是请求的header中的encoding-type不匹配。具体来说,发送的请求的header中的encoding-type为application/json,而PHP需要的是application/x-www-form-urlencoded。因此,解决方法是将发送的请求的header中的encoding-type更改为application/x-www-form-urlencoded,并将请求body中的参数以param1=I%20like%20horses¶m2=they%20are%20cool的形式发送。

如果希望继续发送JSON格式的请求,需要正确接收处理。可以创建一个处理器,用于接收JSON格式的请求。根据这个回答,可以使用以下代码来处理PUT请求:

$method = $_SERVER['REQUEST_METHOD'];
if ('PUT' === $method) {
    $data = json_decode(file_get_contents('php://input'), true);
    var_dump($data); //$data包含PUT字段
}

0