php 中的 $_POST 变量为空
我在这里看到了一些有同样问题的帖子,但没有一个像我的那么简单,因此这篇文章。
我是 php 新手,我发现 $_POST
和 $_REQUEST
变量都是空的。我可以使用 apache_request_headers() 获取标头,并且能够正确查看标头。只是 $_POST
变量为空。
我的客户端是一个 Android 应用程序,并且正在正确生成帖子消息。我用tcpdump来测试一下。 apache 日志还显示调用了预期的 PHP。我使用Apache 2.2.14和PHP 5.2.12标准安装,没有什么特别的。
谁能想到 $_POST
变量为空的任何原因吗?
另外,我没有在代码中使用类。它只是简单的代码,根据需要调用其他类的函数。我提到这一点是因为我看到另一篇文章建议使用 $this->input->post()
但在我的情况下这是不可能的。
编辑:感谢下面的肖恩,发现了问题。 “Content-Type”设置不正确,导致实际值未到达 php 代码。奇怪的是,空请求的标头正是我设置的。为什么数据会被剥离?
I saw a few posts here that have the same problem but none of them are a simple as mine and hence the post.
I am new to php and I see that the $_POST
and $_REQUEST
variable are both empty. I can get the headers using the apache_request_headers()
and am able to see the headers properly. It's just the $_POST
variable is empty.
My client is an Android app and is generating the post message properly. I used tcpdump to test it. Also apache logs show that the intended PHP is invoked. I use Apache 2.2.14 and PHP 5.2.12 the standard installation, nothing special.
Can anyone think of any reason why the $_POST
variable is empty?
Also I am not using a class in my code. Its just plain code which calls functions from other classes as needed. I mention this because I saw another post that suggests to use $this->input->post()
but that is not possible in my case.
EDIT: Found the problem thanks to Sean below. The "Content-Type" was not set properly and that caused the actual values not reaching the php code. Oddly enough the headers of the empty request are what I set them to. Why would the data be stripped?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
确保您的 Android 客户端在 POST 上发送正确的内容类型。它应该设置为application/x-www-form-urlencoded。
Make sure that your Android client is sending the correct content type on POST. It should be set to application/x-www-form-urlencoded.
首先,我会检查您的服务器是否没有使用 301“永久移动”代码重定向您的 POST 数据。打开您的日志文件来检查这一点。如果运行良好,您应该会看到 200 代码(最坏的情况下会看到 201)。您会注意到,每次调用服务器都会生成 POST 和 GET 代码。确保两者都是 200。如果只有 GET 是 200 而 POST 是 301,那么您的服务器由于某种原因正在重定向(例如您正在调用 http: //mysite.com,但服务器重定向到 http://www.mysite.com) 。
如果您需要测试没有向 android 返回值的 GET,请尝试使用 android 中的 HttpPost 进行类似的操作:
然后从服务器端尝试执行诸如创建文件之类的操作,或者如果存在 GET 变量则执行其他操作。
但是,如果您的日志具有 GET 和 POST 的重定向代码,那么您需要使用 .htaccess 覆盖重定向或配置主机以停止任何重定向。
祝你好运!
First I would check that your server isn't redirecting your POST data with a 301 'moved permanently' code. Open you're log file to check this. If its working well you should see a 200 code (or 201 at worst). You'll notice that for each call to the server a POST and GET code will be generated. Make sure both are 200. If only GET is 200 but POST is 301 then your server is redirecting for some reason (e.g. you're calling http://mysite.com, but the server redirects to http://www.mysite.com).
If you need to test GET without a return value to android try something like this using HttpPost in android:
Then from the server side try doing something like creating a file, or something if GET vars are present.
But if your log has a redirect code for both GET and POST then you need to override the redirect with a .htaccess or configure your host to stop any redirect.
Good luck!