【发布时间】:2021-01-08 06:43:24
【问题描述】:
当远程调用 PHP 脚本时,我没有收到 $_POST 数据。我使用 cURL 和 Postman 进行了尝试。
内容类型:表单数据
curl --location --request POST 'http://example.com/backend.php?gateway=test' --form 'status=AP'
我希望 status 在 POST 中。
回复:
2020-09-22 13:19:27
JSON POST:
REQUEST:
Array
(
[gateway] => test
)
GET:
Array
(
[gateway] => test
)
POST:
Array
(
)
Content-Type: application/x-www-form-urlencoded'
curl --location --request POST 'http://example.com/backend.php?gateway=test' --header 'Content-Type: application/x-www-form-urlencoded' --data-urlencode 'status=AP'
我希望 status 在 POST 中。
回复:
2020-09-22 12:59:00
JSON POST:
REQUEST:
Array
(
[gateway] => test
)
GET:
Array
(
[gateway] => test
)
POST:
Array
(
)
内容类型:application/json'
curl --location --request POST 'http://example.com/backend.php?gateway=test' --header 'Content-Type: application/json' --data-raw '{ "status": "AP" }'
我希望 status 在 JSON POST 中。
回复:
2020-09-22 13:20:43
JSON POST:
REQUEST:
Array
(
[gateway] => test
)
GET:
Array
(
[gateway] => test
)
POST:
Array
(
)
这是来自 backend.php 的代码
<?php
header('Access-Control-Allow-Origin: *');
$json = file_get_contents("php://input");
echo '<pre>';
echo date('Y-m-d H:i:s') . "\n";
echo "\n";
echo "JSON POST:\n";
if ($json) {
print_r(json_decode($json, true));
}
echo "\n";
echo "REQUEST:\n";
print_r($_REQUEST);
echo "\n";
echo "GET:\n";
print_r($_GET);
echo "\n";
echo "POST:\n";
print_r($_POST);
它可以在 localhost 中正常工作,但不能在生产环境中工作。生产中的 PHP 版本是 5.5.38。
【问题讨论】:
-
尝试使用
X而不是request。比如`-X POST` -
是一个rewriterule激活的(也许是把url从http改成https或者把WWW加到域名里)?这会将 POST 更改为 GET
-
@IvoP 啊,是的,是因为 https。更改为 https 确实有效。谢谢。