$ _POST在PHP中为空 [英] $_POST is empty in php
问题描述
我已经阅读了这篇文章中的所有回应< br>
print_r($ _ POST); ===>返回空数组
print_r($ _ SERVER);
Array([HTTP_HOST] => localhost
[HTTP_ACCEPT] => * / *
** [HTTP__CONTENT_TYPE] ** => application / json; charset = UTF-8
[CONTENT_LENGTH] => 942
** [CONTENT_TYPE] ** => application / x-www -form-urlencoded
[PATH] => / usr / local / bin:/ usr / bin:/ bin
[SERVER_SIGNATURE] => Apache / 2.2.14 80
[SERVER_SOFTWARE] => Apache / 2.2.14(Ubuntu)
[SERVER_NAME] => localhost
[SERVER_ADDR] => 127.0.0.1
[SERVER_PORT] => 80
[REMOTE_ADDR] => 127.0.0.1
[DOCUMENT_ROOT] => / var / www
[SERVER_ADMIN] => webmaster @ localhost
[SCRIPT_FILENAME ] => /var/www/slocation.php
[REMOTE_PORT] => 50657
[GATEWAY_INTERFACE] => CGI / 1.1
[SERVER_PROTOCOL] => HTTP / 1.1
[REQUEST_METHOD] => POST
[QUERY_STRING] =>
[REQUEST_URI] => /slocation.php
[SCRIPT_NAME] => /slocation.php
[PHP_SELF] => /slocation.php
[REQUEST_TIME] => 1288466907
)
HTTP__CONTENT_TYPE和CONTENT_TYPE有什么区别?
print_r($ HTTP_RAW_POST_DATA); ==>返回正确的数据
file_get_contents('php:// input'); ======>返回正确的数据。
只有$ _POST失败。
$ url =http://localhost/slocation.php;
$ ch = curl_init($ url);
curl_setopt($ ch,CURLOPT_HEADER,1);
curl_setopt($ ch,CURLOPT_HTTPHEADER,array('Content-type:application / json; charset = UTF-8'));
curl_setopt($ ch,CURLOPT_POST,1);
curl_setopt($ ch,CURLOPT_POSTFIELDS,$ payload);
$ result = curl_exec($ ch);
UPDATE ::::::::::::::::::::: ::::::::::::::::::::::::::::::::::::::
我找到一个主人此处
如果你看到HTTP__CONTENT_TYPE,它很可能意味着你犯了错误在设置CONTENT-TYPE头字段。很可能,当curl无法识别有效的CONTENT_TYPE值时,错误的值设置为HTTP__CONTENT_TYPE,CONTENT_TYPE采用默认值。
I have gone through all the responses in this post
print_r($_POST); ===> returns empty array
print_r($_SERVER);
Array ( [HTTP_HOST] => localhost
[HTTP_ACCEPT] => */*
**[HTTP__CONTENT_TYPE]** => application/json; charset=UTF-8"
[CONTENT_LENGTH] => 942
**[CONTENT_TYPE]** => application/x-www-form-urlencoded
[PATH] => /usr/local/bin:/usr/bin:/bin
[SERVER_SIGNATURE] => Apache/2.2.14 (Ubuntu) Server at localhost Port 80
[SERVER_SOFTWARE] => Apache/2.2.14 (Ubuntu)
[SERVER_NAME] => localhost
[SERVER_ADDR] => 127.0.0.1
[SERVER_PORT] => 80
[REMOTE_ADDR] => 127.0.0.1
[DOCUMENT_ROOT] => /var/www
[SERVER_ADMIN] => webmaster@localhost
[SCRIPT_FILENAME] => /var/www/slocation.php
[REMOTE_PORT] => 50657
[GATEWAY_INTERFACE] => CGI/1.1
[SERVER_PROTOCOL] => HTTP/1.1
[REQUEST_METHOD] => POST
[QUERY_STRING] =>
[REQUEST_URI] => /slocation.php
[SCRIPT_NAME] => /slocation.php
[PHP_SELF] => /slocation.php
[REQUEST_TIME] => 1288466907
)
What's the difference between HTTP__CONTENT_TYPE and CONTENT_TYPE?
print_r($HTTP_RAW_POST_DATA); ==> returns correct data posted
file_get_contents('php://input'); ======> returns correct data.
Only $_POST fails.
This is my curl command
$url = "http://localhost/slocation.php";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt ($ch, CURLOPT_HTTPHEADER, array('"Content-type: application/json; charset=UTF-8"'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
$result = curl_exec($ch);
UPDATE::::::::::::::::::::::::::::::::::::::::::::::::::
I found a master here
To answer my own question, "What's the difference between HTTP__CONTENT_TYPE and CONTENT_TYPE?"
If you see "HTTP__CONTENT_TYPE", it most probably means you have made mistake in setting the CONTENT-TYPE header field. Probably, when curl doesn't recognize the valid CONTENT_TYPE value, then the wrong value is set to HTTP__CONTENT_TYPE and CONTENT_TYPE takes a default value.
这篇关于$ _POST在PHP中为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!