Welcome Guest, Not a member yet? Register   Sign In
RESTful API - Search & CRUD
#1

[eluser]Peng Kong[/eluser]
Here's my attempt at a REST Controller, your feedback is most appreciated. (see attached file)

Features
1) Search and CRUD API
2) Query string for search (*query string must be enabled in config)
3) Decoupled from authentication library

URLs
1) Search (GET Request) - http://api.mysite.com/users?one=1&two=2&...output=xml
2) [C]reate (POST Request) - http://api.mysite.com/users
3) [R]ead (GET Request) - http://api.mysite.com/users/1
4) [U]pdate (PUT Request) - http://api.mysite.com/users/1
5) [D]elete (DELETE Request) - http://api.mysite.com/users/1

Controller Template
Code:
<?php

class Places extends REST_Controller
{
    // GET Request - Search
    // http://api.mysite.com/users?one=1&two=2&three=3&four=4&output=xml
    function search()
    {
        //$this->get('one');
        //$this->get('two');
        //$this->get('three');
        //$this->get('four');
        
        //$this->response($results, 200);
    }

    // POST Request - [C]RUD
    // http://api.mysite.com/users
    function create()
    {
        /*
        // Do your basic auth stuff
        $this->input->server('PHP_AUTH_USER')
        $this->input->server('PHP_AUTH_PW')
        
        // Do your digest auth stuff
        $this->input->server('PHP_AUTH_DIGEST'))
        
        // Do your oauth stuff
        // something...
        
        // If authentication fails
        $this->response('Authentication required.', 401);
        */
        
        /*
        $this->post('param1');
        $this->post('param2');
        $this->post('param3');
        
        $this->response('Resource created.', 201);
        */
    }
    
    // GET Request - C[R]UD
    function read($place_id)
    {
        $this->response('Resource read.', 200);
        //$this->response($place, 200);
    }

    // PUT Request - CR[U]D
    function update($place_id)
    {
        /*
        $this->put('param1');
        $this->put('param2');
        $this->put('param3');
        */
        $this->response('Resource updated.', 200);
    }

    // DELETE Request - CRU[D]
    function delete($place_id)
    {
        $this->response('Resource deleted.', 200);
    }
    
}


/* End of file places.php */
/* Location: ./system/application/controllers/places.php */

Credit goes to Phil Sturgeon
http://philsturgeon.co.uk/news/2009/06/REST-implementation-for-CodeIgniter
#2

[eluser]ortenheim[/eluser]
Hi i am testing your controller, while being new to REST services im not sure i set it up right...
the places.php file i put in "application/controllers" and the REST_controller.php in "applciation/libraries" and rest.php. in the config.php i have set

Code:
$config['uri_protocol']    = "QUERY_STRING";

i get the error
Quote:An Error Was Encountered
The URI you submitted has disallowed characters.
probably since i dont put the right returning get functions:

Code:
$this->get('one');
$this->get('two');
$this->get('tree');
$this->get('four');

im sorry i am really new to this

My goal is to create a simple REST service that:
1. can POSTS a user to the database
2. can return the total amount of users in the database

perhaps you can also make an example with a database with a model? or point me to where i can find such examples Smile

Also , how does one use authentication with the REST? ( i see you have included it in the create() function).

Thank you in advance Smile
#3

[eluser]Peng Kong[/eluser]
Ok just do two things...

1) Change config

Code:
$config['uri_protocol']    = "PATH_INFO";

yep like you already did... enable query strings

Code:
$config['enable_query_strings'] = TRUE;

2) Sorry I missed this out... add the require line above into your controller

Code:
<?php

require(APPPATH.'/libraries/REST_Controller.php');

class Places extends REST_Controller

check back tomorrow i'll try to come up with a simple working example

also you should check out Phil Sturgeon's example...
http://philsturgeon.co.uk/news/2009/06/R...odeIgniter
#4

[eluser]Phil Sturgeon[/eluser]
The NetTuts tutorial on this finally got published.

http://net.tutsplus.com/tutorials/php/wo...igniter-2/
#5

[eluser]ortenheim[/eluser]
Been using your tutorial and creating my own get, works well with regular english characters but I have unicode characters like:
Quote:äåö åæø
in the database and when running "visitors" I get the error:

Code:
XML Parsing Error: undefined entity
Location: http://localhost:8888/restserver1/index.php/payments_api/visitors
Line Number 2, Column 108:&lt;xml&gt;&lt;item><id>1</id><city>Oslo</city><session>Session1</session><firstname>Mikael</firstname><lastname>&Ouml;ttenheimer</lastname><companyname>cpp334</companyname><regdate>2010-02-05 11:58:54</regdate></item>&lt;/xml&gt;

When displaying in regular pages the characters are showed correctly from the database.

Model:
Code:
class Visitor_model extends Model
{
    function get_all()
    {
        $query = $this->db->get('visitor');
        if ($query->num_rows() > 0)
        {
            return $query->result();
        }
        return FALSE;    
    }

Rest controller:
Code:
&lt;?php

require(APPPATH.'/libraries/REST_Controller.php');

class Payments_api extends REST_Controller
{
    
    function visitors_get()
    {
        $this->load->model('Visitor_model');
        $visitors = $this->Visitor_model->get_all();

                if($visitors)
                {
                    $this->response($visitors, 200); // 200 being the HTTP response code
                }

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

How do i fix this unicode problem?
#6

[eluser]Peng Kong[/eluser]
utf8_encode and urlencode your stuff. else they get messed up in url transit.

look at my attached file you'll get the idea.

yahoo twitter (and im sure other) apis all require you to do that before sending the data over url
#7

[eluser]Peng Kong[/eluser]
oh crap sorry im not answering your question. sorry i read it wrong.

what's inside your database? "& Ouml ;" (without spaceing) or Ö

only the Ö is accepted in the url.

urlencode it and get "% C3 % 96" (without spacing) before putting it in the url
#8

[eluser]ortenheim[/eluser]
The letter Ö is in the database, not getting what you mean with url encoding? i am only opening the http://localhost:8888/restserver1/index....i/visitors that returns my database entries as an xml (included as one of the examples on nettuts)
#9

[eluser]ortenheim[/eluser]
Also, what is also wierd is that when i remove the Ö and have O and the XML renders fine....when using http://localhost:8888/restserver1/index....ormat/html this errors is recieved.... Sad

Quote:A PHP Error was encountered

Severity: Warning

Message: array_keys() [function.array-keys]: The first argument should be an array

Filename: libraries/REST_Controller.php

Line Number: 486
A PHP Error was encountered

Severity: 4096

Message: Object of class stdClass could not be converted to string

Filename: libraries/Table.php

Line Number: 269
A PHP Error was encountered

Severity: 4096

Message: Object of class stdClass could not be converted to string

Filename: libraries/Table.php

Line Number: 269
#10

[eluser]Peng Kong[/eluser]
just to be clear you're using Phil's REST controller right (NOT the one downloaded from this page)? so im not lookin at the wrong file while helping you debug.

try htmlentities($param) before pushing it to xml




Theme © iAndrew 2016 - Forum software by © MyBB