Send Retrieve Post JSON to Databases in Codeigniter

Teguh Arief

Sep 24, 2020

Share article to

Illustration of send JSON to database.

Most application developers mix and match the APIs (Application Programming Interface) to produce their application, the friendly APIs language programming can use JSON. In this tutorial, we will learn send retrieve Post JSON to CodeIgniter and also to insert them in the Database. The datas from Frontends, then send and validate it via Backends, and last step insert its datas to Database.

Frontends



Data of JSON


"data":[{
"NamaLengkap": "Nama Lengkap",
"NomorHp": "08123456789",
"TempatLahir": "Jakarta",
"TanggalLahir": "12-12-2012",
"AlamatEmail": "teguh@gmail.com",
"WargaNegara": "WNI"
}]


I used PHP cURL to send a post request to my RESTserver


$data = array(
"NamaLengkap" => $this->input->post('NamaLengkap'),
"NomorHp" => $this->input->post('NomorHp'),
"TempatLahir" => $this->input->post('TempatLahir'),
"TanggalLahir" => $this->input->post('TanggalLahir'),
"AlamatEmail" => $this->input->post('AlamatEmail'),
"WargaNegara" => $this->input->post('WargaNegara'),
);
log_message("error", print_r(json_encode($data),true));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://yoursite/backends');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/json'));
$result = curl_exec($ch);
var_dump($result);
log_message("error", print_r($result,true));


Backends



In Controller


class Backends extends CI_Controller {

public function __construct() {
parent::__construct();
$this->load->library('form_validation');
}

public function index() {
if($_SERVER['REQUEST_METHOD'] == "POST"){
$data = file_get_contents("php://input");
$row = json_decode($data,true);
$_POST['NamaLengkap'] = $row['NamaLengkap'];
$_POST['NomorHp'] = $row['NomorHp'];
$_POST['TempatLahir'] = $row['TempatLahir'];
$_POST['TanggalLahir'] = $row['TanggalLahir'];
$_POST['AlamatEmail'] = $row['AlamatEmail'];
$_POST['WargaNegara'] = $row['WargaNegara'];

$NamaLengkap = $_POST['NamaLengkap'];
$NomorHp = $_POST['NomorHp'];
$TempatLahir = $_POST['TempatLahir'];
$TanggalLahir = $_POST['TanggalLahir'];
$AlamatEmail = $_POST['AlamatEmail'];
$WargaNegara = $_POST['WargaNegara'];

$this->form_validation->set_message('required', '%s harus diisi');
$this->form_validation->set_message('min_length', '%s Min %s chars');
$this->form_validation->set_message('max_length', '%s Max %s chars');
$this->form_validation->set_message('is_unique', '%s sudah terdaftar');
$this->form_validation->set_message('matches', '%s not same with %s');
$this->form_validation->set_message('numeric', '%s harus berupa angka');
$this->form_validation->set_message('valid_email', 'Penulisan %s tidak benar');

$this->form_validation->set_rules('NamaLengkap', 'Nama Lengkap', 'required');
$this->form_validation->set_rules('NomorHp', 'Nomor Hp', 'required|numeric');
$this->form_validation->set_rules('TempatLahir', 'Tempat Lahir', 'required');
$this->form_validation->set_rules('TanggalLahir', 'Tanggal Lahir', 'required');
$this->form_validation->set_rules('AlamatEmail', 'Alamat Email', 'required|valid_email');
$this->form_validation->set_rules('WargaNegara', 'Warga Negara', 'required');

if ( ($this->form_validation->run() == TRUE) ) {
$data=array(
'Nama_lengkap'=>$NamaLengkap,
'No_hp'=>$NoHp,
'Tmp_lahir'=>$TempatLahir,
'Tgl_lahir'=>$TanggalLahir,
'Email'=>$AlamatEmail,
'Kewarganegaraan'=>$WargaNegara
);
$this->db->insert('db_data', $data);
echo json_encode(array('Status' => 1, 'Message' => "Success"));
}else{
echo json_encode(array('Status' => 0, 'Message' => 'Error: ' . strip_tags(validation_errors())));
}

}else{
echo "Access Denied";
}

}

}


Happy birthday to my niece Hana, and me too in October ! "Talent wins games, but teamwork and intelligence wins championships." - Michael Jordan