编写项目时需要将数据转换成json格式的字符串,并通过post传参传给后台,但在后台接收数据时发现$_POST参数为空 

头部为:

curl_setopt($ci, CURLOPT_HEADER, 0);
curl_setopt($ci, CURLOPT_HTTPHEADER,array('Content-Type: application/json; charset=utf-8','Content-Length:' . strlen($tmpdatastr)));

针对这个问题在网上查询资料发现,仅在 Coentent-Type的值为application/x-www-data-urlencode和multipart/form-data时,php才会将http请求数据包中的数据填进$_POST中,

其他类型的Coentent-Type,php不能自动识别,会将相应的数据填入变量$HTTP_RAW_POST_DATA中.

所以可以使用两种方法在这种情况下获取数据

1.通过$HTTP_RAW_POST_DATA获取

$post_data = $GLOBALS['HTTP_RAW_POST_DATA'];

但需要修改相应的php.ini指令

2.通过php://input获取(建议使用这种方法 php://input数据总是和$HTTP_RAW_POST_DATA相同,但php://input更凑效且不需要修改php.ini)

$post_data = file_get_contents("php://input"); 

 

By Hgq

相关文章:

  • 2021-08-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-16
  • 2021-10-20
  • 2022-12-23
  • 2022-12-23
  • 2021-09-02
猜你喜欢
  • 2021-05-29
  • 2022-12-23
  • 2021-11-11
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案