【问题标题】:Getting file content 'php://input' not working获取文件内容“php://输入”不起作用
【发布时间】:2012-10-24 07:01:38
【问题描述】:

我有 PHP 应用程序尝试使用 'file_get_contents' 函数在 'php://input' 中获取 post JSON。只返回空字符串。下面是完整的代码行。

$_PUT = json_decode(file_get_contents("php://input"), true);

或者我需要更改任何 apache/php 设置?

【问题讨论】:

  • 你是如何构建请求的?
  • 来自文档:“php://input 不适用于 enctype="multipart/form-data"。”
  • 请求使用 Angular $resource 和 JSON 格式的数据。所以@TobiasSpringer,我需要在发送帖子时修改标题?我正在使用这个标题'Content-Type': 'application/x-www-form-urlencoded'
  • 如果你要 PUTing JSON,你应该使用'Content-Type': 'application/json'。确保您跟踪请求,例如在 google chrome 网络选项卡中,以确保您实际发送请求正文

标签: php json rest file-get-contents


【解决方案1】:

我正在使用这个标题'Content-Type': 'application/x-www-form-urlencoded'

如果您的数据是 application/x-www-form-urlencoded,那么您应该通过 $_POST 访问它,而不是接触原始 HTTP 请求正文。仅当您使用非典型内容类型(例如 application/json)时才这样做。

如果您的数据不是该格式,那么您应该使用正确的 Content-Type 标头。

【讨论】:

  • 仍在战斗,建议的解决方案不起作用。彻底检查了标题,并使用完全按照代码发送。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-25
  • 1970-01-01
  • 2017-04-18
  • 1970-01-01
相关资源
最近更新 更多