Teguh Arief
Published on: September 24, 2020
Modern application development frequently involves integrating various APIs, and JSON (JavaScript Object Notation) has become the go-to format for friendly API communication. In this tutorial, we will focus on how to send retrieve post JSON to databases in Codeigniter. We'll cover the entire workflow: preparing data on the frontend, sending it via cURL, validating it on the backend, and finally inserting it into your database.
Frontends
Data of JSON
First, let's look at the structure of the JSON data we'll be sending. This example represents typical user information to send retrieve post JSON to databases in Codeigniter:
"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
To send retrieve post JSON to databases in Codeigniter from the frontend, we'll use PHP cURL to make a POST request to our backend. This snippet prepares the data and sends it as a JSON payload:
$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
On the backend, our CodeIgniter controller handles the incoming JSON data, validates it, and then proceeds to send retrieve post JSON to databases in Codeigniter. This controller demonstrates how to process the JSON input and perform validation before insertion:
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";
}
}
}
This comprehensive guide should help you understand and implement the process to send retrieve post JSON to databases in Codeigniter. Happy coding!
Related Posts
NestJS with PostgreSQL, TypeORM, JWT auth, and Docker
Discover the complete steps to set up and run a NestJS application with PostgreSQL, TypeORM, JWT authentication, and Docker, all managed via Darwin Terminal.
Read More
NestJS CRUD with MongoDB using Mongoose
Learn to build a robust backend with NestJS, covering CRUD operations using MongoDB and Mongoose, from setup to creating, reading, and updating.
Read More
Send Multiple Email Blasts to a Single Email Recipient
Learn to send multiple email blasts to a single recipient for promotions. This guide covers effective system design and implementation for your business.
Read More