0

This php code is creating a json file and posting data into it. i wanted to post data in mysql instead of json. How can i achieve this ? I'm calling add_device from an android app and wanted its data to be posted in mysql server.

<?php
header('Content-Type: application/json; charset=UTF-8');
$FILE_PATH = "../private/storage/device_list.json";
$response_data = [
'status' => false];
function createJson($json){
return json_encode($json, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);}
function add_new_device(){
global $response_data;
global $FILE_PATH;

$device_path = $FILE_PATH;

if(!file_exists($device_path)){
    $fp = fopen($device_path, 'w');
    fclose($fp);
}

$post_data = [
    'UNIQUE_ID' => $_POST['unique_id'],
    'COUNTRY' => $_POST['country'],
    'SOFTWARE_VERSION' => $_POST['software_version'],
    'DEVICE_MODEL' => $_POST['device_model'],
    'DEVICE_LANGUAGE' => $_POST['device_language'],
];

$strJsonFileContents = file_get_contents($device_path);
$device_array = json_decode($strJsonFileContents, true);
$device_array['device_list'][ $_POST['unique_id']] = $post_data;
file_put_contents($device_path, createJson($device_array));
$response_data['status'] = true;
echo createJson($response_data);
}
if (isset($_POST['add_device'])){
add_new_device();}

0 Answers0