Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REST API with CodeIgniter

I am creating a web-service backend for a mobile app I am developing. (I am an experience Obj-C developer, not a web-designer!) Essentially I would like to use Codeigniter and Phil Sturgeon's RESTful API Server https://github.com/philsturgeon/codeigniter-restserver however, I'm having some trouble getting it all setup and working.

I have MySQL database that is setup with data in it. And I need some help writing a CodeIgniter PHP model and controller that returns JSON data of what is inside that database. All of the tutorials and forum post's i have found deal with hardcoded data in the controler, not a MySQL database. I would ideally like to have the URL formatted like this http://api.mysite.com/v1/search?id=1&name=foo&city=bar , I could potentially have 50+ parameters to pass in the url.

Using Phil's code, I have come up with this as my controller:

public function index_get()
{
    if (!$this->get('id'))
    {
        $this->response(NULL, 400);
    }

    $data = $this->grid_m->get_id($this->get('id'));
    if ($data)
    {
        $this->response($data, 200);
    }
    else
    {
        $this->response(NULL, 404);
    }
}

That only gets me one search term: id?=# .. I need to know how to get multiple search terms

Here is my Codeigniter model:

<?php

class Grid_m extends CI_Model
{

function get_all()
{
    $query = $this->db->get('grid');
    if ($query->num_rows() > 0)
    {
        return $query->result();
    }
    return FALSE;;
}

This just returns EVERYTHING in my MySQL database regardless of what id or url term I pass it in the URL.

I'm a big noob when it comes to developing my own custom API so any suggestions on how to fix my controller and database model would be a huge help!

Thanks for the help!

-brian

like image 692
Brian Avatar asked Nov 25 '12 07:11

Brian


People also ask

Can I use REST API in PHP?

There are many ways you can get information from a RESTful API using PHP.

What can I do with CodeIgniter?

CodeIgniter is a PHP MVC framework used for developing web applications rapidly. CodeIgniter provides out of the box libraries for connecting to the database and performing various operations like sending emails, uploading files, managing sessions, etc.

Is CodeIgniter is a framework?

CodeIgniter is a powerful PHP framework with a very small footprint, built for developers who need a simple and elegant toolkit to create full-featured web applications.


1 Answers

This is old question but if somebody go here and still need help, try these code:

In your controller:

public function index_get()  
{  
    $where = '';  

    if ($this->get('id'))  // search by id when id passed by
    {              
        $where .= 'id = '.$this->get('id');  
    }  

    if ($this->get('name'))  // add search by name when name passed by        
    {  
        $where .= (empty($where)? '' : ' or ')."name like '%".$this->get('name')."%'";  
    }  

    if ($this->get('city'))  // add search by city when city passed by  
    {  
        $where .= (empty($where)? '' : ' or ')."city like '%".$this->get('city')."%'";  
    }  

    // you can add as many as search terms

   if ( ! empty($where))  
   {  
       $data = $this->grid_m->get_searched($where);  

       if ($data)  
       {  
           $this->response($data, 200);  
       }  
       else  
       {  
           $this->response(NULL, 404);  
       }  
    }  
    else  
    {  
        $this->response(NULL, 404);  
     }  
}

In your model, create get_searched function:

class Grid_m extends CI_Model  
{  
   function get_searched($where = NULL)  
   {  
      if ( ! is_null($where))  
      {  
          $this->db->where($where);  
          $query = $this->db->get('grid');  

          if ($query->num_rows() > 0)  
          {  
              return $query->result();  
          }  
       }  
       return FALSE;  
    }  
}  
like image 97
Arif RH Avatar answered Oct 05 '22 18:10

Arif RH