Wednesday, November 28, 2018

Create Web API HTTPs Method Tutorial

In this section we will implement Get action and post methods in our Web API controller class that will handle HTTP GET.HTTP Post requests.
As per the Web API naming convention, action method that starts with a work "Get","Post" will handle HTTP request. We can either name it only Get, Post or with any suffix. Let's add our first Get,Post action method and give it a name user_get because it will return all the api_test from the DB. Following an appropriate naming methodology increases readability and anybody can understand the purpose of a method easily.
Video: Create API using Get Method

Video: Create API using Post Method

Example: Get,Post Method in Web API Controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
 

class Api extends CI_Controller
{
 
        public function __construct() {
           
      parent::__construct();
         $this->load->model('apiModel');
         $this->load->database();

 } 
         
 public function user_get(){
    $this->apiModel->getdata();
   
 }
 public function data_post(){

             
           $data = array('fname' => $this->input->post('fname'),
                       'lname' => $this->input->post('lname'),
                        'mobile' =>$this->input->post('mobile'));
                        $this->load->model('apiModel');
                        $this->apiModel->insert($data);
 
  }
  }           
 
    

Example: Get,Post Method in Web API Model

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class apiModel extends CI_Controller {

public function __construct()
     {
     parent::__construct();
     $this->load->database();
 }

     public function getdata(){
 
   $query = $this->db->query("select * from `getdata`");
 
      echo json_encode($query->result());
 
   }
    public function insert($data){
 
       $sql=$this->db->insert('getdata',$data);
if($sql>0){
 echo "insert data from tabel";
}else{
 echo "not insert data from tabel";
}
 
      
}
}

No comments:

Post a Comment