本文介绍了file_get_contents('php://input') 返回带有 PATCH 请求的空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的环境是PHP 5.5.9,Nginx 1.4.6-1ubuntu3.2,本地主机.

My environment is PHP 5.5.9, Nginx 1.4.6-1ubuntu3.2, localhost.

我正在尝试从 PATCH 方法中获取数据,但这只是返回一个空字符串...使用 POST 方法可以正常工作,这是我的脚本的一部分:

I'm trying to get data from PATCH method, but that just return an empty string...With POST method that's work fine, this is a piece of my script :

case 'POST':
case 'PATCH':
     $this->data = file_get_contents("php://input");

$this->data 在使用 PATCH 方法时为空,在 POST 时完成,我使用 POSTMAN chrome 扩展并推送 RAW 数据(不是 multipart/form-data)

$this->data is empty when PATCH method is used and complete when POST, i use the POSTMAN chrome extension and i push RAW data (not multipart/form-data)

我认为 Nginx 有问题...但日志文件中没有任何内容...

I think Nginx was in fault...but nothing into the log file...

任何帮助将不胜感激!

推荐答案

我最近在尝试访问 php://input 以获取 PUT 请求时遇到了类似的问题.问题很简单,我之前已经访问过它一次(在相关代码之前运行的日志记录函数中).

I recently ran into a similar issue trying to access php://input for a PUT request. The issue was simply that I had already accessed it once previously (in a logging function that ran prior to the code in question).

POST 的行为与其他方法不同,这解释了差异:

POST acts differently than the other methods, which explains the discrepancy:

注意:在 PHP 5.6 之前,使用 php://input 打开的流只能被读取一次;流不支持搜索操作.但是,根据 SAPI 实现,可能可以打开另一个 php://input 流并重新开始读取.这仅在请求正文数据已保存的情况下才有可能.通常,这是 POST 请求的情况,而不是其他请求方法,例如 PUT 或 PROPFIND.

来源:http://php.net/manual/en/wrappers.php.php

解决方案很简单:将 php://input 从初始抓取的值存储到 PHP 变量中,并且不要尝试运行 file_get_contents("php://input") 对于非 POST 请求不止一次.

The solution was simple: Store the value of php://input from the initial grab to a PHP variable, and don't try to run file_get_contents("php://input") more than once for a non-POST request.

这篇关于file_get_contents('php://input') 返回带有 PATCH 请求的空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 15:32
查看更多