简体   繁体   English

PHP API内容类型:application / json。 响应为空数据

[英]PHP API Content-Type: application/json. Response empty data

Works fine for: 适用于:

  • Sending from Android platform to PHP (web service) 从Android平台发送到PHP(Web服务)
  • Request headers sent with this Content-Type: application/x-www-form-urlencoded 与此Content-Type: application/x-www-form-urlencoded一起发送的请求标头Content-Type: application/x-www-form-urlencoded

Not working for: 不适用于:

  • Request headers sent with Content-Type: application/json . 使用Content-Type: application/json发送的请求标头Content-Type: application/json no data is received 没有收到数据

API is working on same platform but not in cross platform: API在同一平台上工作,但不在跨平台上工作:

  • Web to web WORKING 网络到网络
  • Android to web NOT WORKING Android到网络无法正常工作

In PHP added both header on top: 在PHP中,两个标头都放在顶部:

header("Access-Control-Allow-Origin: *");
header('Content-Type: application/json');

If you compare these two content types application/x-www-form-urlencoded and application/json in a "The first works with PHP, the second doesn't", then you probably expect the data to appear magically inside $_POST in both cases. 如果您在“第一个可与PHP一起使用,第二个不与PHP一起使用”中比较这两种内容类型application / x-www-form-urlencoded和application / json,则您可能希望数据在两者中都神奇地出现在$ _POST内案件。

This will not happen. 这不会发生。 PHP only fills $_POST if the first content type is given (alternatively, application/multipart-form-data can be used for everything, especially file uploads). 如果只指定了第一种内容类型,PHP只会填充$ _POST(或者,application / multipart-form-data可以用于所有内容,尤其是文件上传)。

If you want to use application/json, then you have to implement a parser on the PHP side yourself that reads the HTTP request body and parses it to your liking. 如果要使用application / json,则必须在PHP端自己实现一个解析器,该解析器读取HTTP请求正文并将其解析为您喜欢的内容。

after log search i found my answer.. we need this function to get $_POST response from cross platform (android to web) 在日志搜索后,我找到了我的答案..我们需要此功能才能从跨平台(Android到Web)获取$ _POST响应

file_get_contents('php://input')

OR we can also use this function to get $_POST response 或者我们也可以使用此函数来获得$ _POST响应

$HTTP_RAW_POST_DATA

here is complete function to get the response. 这是获取响应的完整功能。

$data = urldecode(file_get_contents('php://input'));
echo $data;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Nginx,fastcgi PHP 请求体在内容类型为 application/json 时为空 - Nginx, fastcgi PHP request body empty when content-type application/json Slim - 如何使用“Content-Type:application / json”标题发送响应? - Slim - How to send response with “Content-Type: application/json” header? 未记录的PHP自动将content-type:application / json解码为$ _REQUEST - PHP undocumented automatically decode content-type:application/json to $_REQUEST 何时在PHP中使用标题('Content-Type:application / json') - When to use header('Content-Type: application/json') in PHP 如何将'content-type application / json'的数据发布到苗条的php rest服务 - How to post data of 'content-type application/json' to slim php rest service PHP header('Content-Type: application/json'); 不管用 - PHP header('Content-Type: application/json'); is not working 应该什么时候Content-Type是application / json - When should Content-Type be application/json 无法将其转换为content-type:application / json - can not convert it into content-type:application/json `header(“Content-type:application / json”)的用法;` - The usage of `header(“Content-type:application/json”);` 如果标题中包含“ Content-Type:application / json”,则不会创建PHP 5.4.9 $ _POST超全局变量 - PHP 5.4.9 $_POST superglobal not created if 'Content-Type: application/json' is in header
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM