[英]PHP API Content-Type: application/json. Response empty data
Works fine for: 适用于:
Content-Type: application/x-www-form-urlencoded
与此Content-Type: application/x-www-form-urlencoded
一起发送的请求标头Content-Type: application/x-www-form-urlencoded
Not working for: 不适用于:
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在同一平台上工作,但不在跨平台上工作:
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.