简体   繁体   中英

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

Works fine for:

  • Sending from Android platform to PHP (web service)
  • Request headers sent with this Content-Type: application/x-www-form-urlencoded

Not working for:

  • Request headers sent with Content-Type: application/json . no data is received

API is working on same platform but not in cross platform:

  • Web to web WORKING
  • Android to web NOT WORKING

In PHP added both header on top:

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.

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).

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.

after log search i found my answer.. we need this function to get $_POST response from cross platform (android to web)

file_get_contents('php://input')

OR we can also use this function to get $_POST response

$HTTP_RAW_POST_DATA

here is complete function to get the response.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM