【问题标题】:POST data are not received when a PHP script is called remotely远程调用 PHP 脚本时未收到 POST 数据
【发布时间】: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 确实有效。谢谢。

标签: php php-5.5


【解决方案1】:

奇怪。它应该按照您尝试的方式工作。你可以试试这个:

curl -d "status=AP" -H "Content-Type: application/x-www-form-urlencoded" -X POST http://example.com/backend.php?gateway=test

【讨论】:

    猜你喜欢
    • 2016-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-20
    • 1970-01-01
    • 2011-04-15
    • 1970-01-01
    相关资源
    最近更新 更多