无法使用php读取post请求的正文
我正在尝试使用一个在线服务,它需要在回调url(webhook)上读取post请求的主体 但是,我不熟悉http请求,无法读取接收到的数据 我使用检索正文并将结果存储在文本文件中:无法使用php读取post请求的正文,php,post,Php,Post,我正在尝试使用一个在线服务,它需要在回调url(webhook)上读取post请求的主体 但是,我不熟悉http请求,无法读取接收到的数据 我使用检索正文并将结果存储在文本文件中: file_put_contents("log/test.txt", file_get_contents('php://input')); 我在记事本++中收到的数据如下所示(奇怪的字符): 知道这里发生了什么吗 编辑:完整代码 <head> <meta charset="UTF-8"&
file_put_contents("log/test.txt", file_get_contents('php://input'));
我在记事本++中收到的数据如下所示(奇怪的字符):知道这里发生了什么吗 编辑:完整代码
<head>
<meta charset="UTF-8">
</head>
<body>
<?php
require_once dirname(__FILE__).'/db_functions.php';
file_put_contents("log/test.txt", file_get_contents('php://input'));
?>
</body>
</html>
您正在发布二进制数据。
您正在发布图像/文件/文档。
您可以将其转换回原来的状态并保存到目录中。
$target_dir = "files/";
$target_file = $target_dir . basename($_FILES["file"]["name"]); // file = your input name
$target_file = preg_replace('/\s+/', '_', $target_file);
$uploadOk = 1;
$FileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if file already exists
if (file_exists($target_file)) {
$uploadOk = 0;
}
// Check file size
if ($_FILES["file"]["size"] > 5000000) {
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
echo "Your file ". preg_replace('/\s+/', '_', basename( $_FILES["file"]["name"])). " is uploaded.";
} else {
echo "Sorry, an error occured";
}
}
您正在发布二进制数据。
您正在发布图像/文件/文档。
您可以将其转换回原来的状态并保存到目录中。
$target_dir = "files/";
$target_file = $target_dir . basename($_FILES["file"]["name"]); // file = your input name
$target_file = preg_replace('/\s+/', '_', $target_file);
$uploadOk = 1;
$FileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if file already exists
if (file_exists($target_file)) {
$uploadOk = 0;
}
// Check file size
if ($_FILES["file"]["size"] > 5000000) {
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
echo "Your file ". preg_replace('/\s+/', '_', basename( $_FILES["file"]["name"])). " is uploaded.";
} else {
echo "Sorry, an error occured";
}
}
看来,数据被压缩了 因此,简单的解决方案是使用GZDECODE:
file_put_contents("log/test.txt", gzdecode(file_get_contents('php://input')));
看来,数据被压缩了 因此,简单的解决方案是使用GZDECODE:
file_put_contents("log/test.txt", gzdecode(file_get_contents('php://input')));
你能发布一个完整的代码示例吗?我猜接收到的数据是经过编码/二进制流的,通常需要查看HTTP请求头(内容编码)。您确定curl安装在您的系统中吗???@Isitar:我添加了代码above@mario:你能更具体一点吗?你能发布一个完整的代码示例吗?我猜接收到的数据是经过编码/二进制流的,通常需要查看HTTP请求头(内容编码)。您确定curl安装在您的系统中吗???@Isitar:我添加了代码above@mario:你能说得更具体些吗?非常感谢:当你提到“文档”时,我意识到数据可能已经被压缩了。这真的很有帮助,可惜没有。我没有足够的分数。我当然会这么做的。。。sorry@Bruno把我的答案记为最好,现在问题还没有解决。非常感谢:当你提到“文档”时,我意识到数据可能已经被压缩了。这真的很有帮助,可惜没有。我没有足够的分数。我当然会这么做的。。。sorry@Bruno把我的答案记为最好,现在问题还没有解决