Yoo Yoo - 25 days ago 6
PHP Question

How to write PHP to receive JSON from Android

How to write PHP (server side) to receive JSON then insert into DB using Android.

I wrote Java to send data to the PHP server :

HttpPost httpPost = new HttpPost("http://sit-edu4.sit.kmutt.ac.th/csc498/51270372/[My Folder].[My filename].php");


But I do not know how to write the PHP file to receive JSON then insert to DB.

Could you please help me ? Thank you.

Answer

You have your JSON data somewhere in $_POST array.

You can decode the JSON like this:

$json = $_POST['data']; // replace 'data' with your key
$decoded = json_decode($json, TRUE);
if ($decoded === FALSE) {
  throw new Exception('Bad JSON format.');
}
// save to your database via PDO