REST API HTML Javascript jQuery Ajax PHP Example Java MORE

How to save data using Rest API Codeigniter

How to save data using Rest API Codeigniter

In this example I am going to show you how to create a Rest API Codeigniter which will save the data in the database.

Api.php(Controller)

  <?php
require APPPATH . '/libraries/TokenHandler.php';
require APPPATH . 'libraries/REST_Controller.php';
class Api extends REST_Controller {
public function __construct()
{
parent::__construct();
$this- >load- >database();
$this- >tokenHandler = new TokenHandler();
header('Content-Type: application/json');
}
// Signup Api
public function user_post() {
       $this- >load- >model('api_model');
       $user=$this- >request- >body;
$data['Phone']=$user['Phone'];
$data['UserType']=4;
$data['Status']=1;
$data['CreatedDate']=date("Y-m-d");
$UserID = $this- >api_model- >signup_post($data);
if($UserID >0){
   $userdata['status'] = 200;
$userdata['message'] = "User Created Successfully !";
}
       else{
$userdata['status'] = 201;
$userdata['message'] = "Error !";
            }
   return $this- >set_response($userdata, REST_Controller::HTTP_OK);
}

Run it yourself

Api_model.php(Model)

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Api_model extends CI_Model {
public function signup_post($data) {
$this- >db- >insert('UserMst',$data);
return $this- >db- >insert_id();
}
}
?>

Run it yourself