Welcome Guest, Not a member yet? Register   Sign In
CI and Phil Strugeons REST API
#11

[eluser]Phil Sturgeon[/eluser]
Look at the example_api.php. It does everything you need to get a REST controller working.

You cannot mix REST and normal Controllers, they are different things.
#12

[eluser]moos3[/eluser]
ok thanks phil.
#13

[eluser]ortenheim[/eluser]
Ok phil, i have installed and tried your RESTimplementation and it looks very promising, i am new to RESTful webservices and get the
Code:
http://localhost/codeigniter/index.php/example_api/user/id/1
and

Code:
http://localhost/codeigniter/index.php/example_api/users/users/format/html

to work Smile

but how do i add a user? how do i use rest to start a function (for example a counter of users in the database? and return the result?)

i am trying to make a simple restful server that
1. you can insert single users from a aspx application and 2. return the amount of users in the database

perhaps some more documentation on how to make restful services? I could help you do this if you help me understand how it works Smile
or perhaps a tutorial with models and a database for practical applications?

would love your help. thank you for your library and efforts.
#14

[eluser]Phil Sturgeon[/eluser]
I have no idea how to do it in ASP.NET as I am masochistic enough to work with that language, but essentially you need to post to:

Quote:http://localhost/codeigniter/index.php/example_api/user

And create a function user_post() in the controller. Then just use $this->post('first_name') and interact with your CodeIgniter models however you normally would.

There should be a NetTuts tutorial posted about all this today but they are slacking. They said it would be out already. :-(
#15

[eluser]ortenheim[/eluser]
ok thanks you dont have to explain the .net side, other peeps will solve that Smile perhaps i have missed out on how to post to the REST service? do you recommend an application or specific method to actually test the rest service?

thanks for the heads up on the nettuts i will check it out Big Grin will probably spam you here and on nettuts nuts until i get a hang of it Wink
#16

[eluser]Phil Sturgeon[/eluser]
Use cURL or something similar. That is what my CodeIgniter REST Client uses.
#17

[eluser]ortenheim[/eluser]
Thanks!

Think i might have found a bug, cannot recieve unicode(Öåä) letters from the database as XML when getting all users, se more information here:

http://ellislab.com/forums/viewthread/143126/
#18

[eluser]bugboy[/eluser]
I've been looking at this library for a while now and think its great.

I have a few questions regarding this and the other library restclient.

Rest Server
Rest Client


On Phil's presentation slide slide from Igniting your web service - EECI2009

It was mentioned using the api you create to power your own web app and allow other people to access the data using the api.
I can see the real benefits of this as it means you build your web app once using the same api as your users.

If i have one api for my web app and users i would like it to be secure so i use digest as indicated.
Does this mean that my web app which is on the same server has to have a username and password like a user to use the rest server?

I was thinking that if the same server(the actual web app) asks for the api then no auth is needed as its calling itself in effect and if a user on another server a auth is needed as its outside the web apps server?

Does the libraries already take this into account?
If not what would be the best suggestion for this?

Instead of using the config:
Code:
/*
|--------------------------------------------------------------------------
| REST Login usernames
|--------------------------------------------------------------------------
|
| Array of usernames and passwords for login
|
|    array('admin' => '1234')
|
*/
$config['rest_valid_logins'] = array('admin' => '1234');

I'm thinking of using a database to store the users details so that would mean that web app itself would require a user entry?


Does this make sense?

Cheers
#19

[eluser]bugboy[/eluser]
I've modified the restserver to allow me to get the id of the logged in user so i can perform user specific task and also block off tasks so user can't edit/destroy another users data (eg profile).

I'm not sure if this is the safest or the right way to do this. Below is the modifed bits of the rest_controller. Is there a better safer way of doing this?


Code:
class REST_Controller extends Controller
{
    // Set this in a controller to use a default format
    protected $rest_format = NULL;
    
    private $_method;
    private $_format;
    
    private $_get_args = array();
    private $_put_args = array();
    private $_delete_args = array();
    private $_args = array();
    
    /**
|added in for user store
**/
    private $_user_id;


Create a function to get the id of the logged in user

Code:
// INPUT FUNCTION --------------------------------------------------------------
    
      public function user_id()
    {
        return $this->_user_id;
    }

user login in function. Please note that I'm just running a quick test thats why i'm getting a direct user

Code:
// SECURITY FUNCTIONS ---------------------------------------------------------
    
    private function _check_login($secret = '', $api_key = NULL)
    {
        if(empty($secret))
        {
            return FALSE;
        }
        
        $valid_logins =& $this->config->item('rest_valid_logins');
        
                // using a new array function, this will be replaced with a database call
        if($valid_logins[0]['username'] !== $secret)
        {
            return FALSE;
        }
        
        // If actually NULL (not empty string) then do not check it
        if($api_key !== NULL)
        {
            if($valid_logins[0]['password'] != $api_key)
            {
                return FALSE;
            }
        }
        
        // set the user key
        $this->_user_id = $valid_logins[0]['id'];

        return TRUE;
    }

unset if not vaild

Code:
if ($digest['response'] != $valid_response)
        {
            $this->_user_id = NULL;
        
            header('HTTP/1.0 401 Unauthorized');
            header('HTTP/1.1 401 Unauthorized');
            exit;
        }


config array amended to take more data. Please not that this is just temporary and that all these will be stored in a database

Code:
/*
|--------------------------------------------------------------------------
| REST Login usernames
|--------------------------------------------------------------------------
|
| Array of usernames and passwords for login
|
|    array('admin' => '1234')
|
*/
//$config['rest_valid_logins'] = array('admin' => '1234');

$config['rest_valid_logins'] = array(
    array(
    'id' => 1,
    'username' => 'admin',
    'password' => '1234'
    )
);


This is just a test call and would be open to the public but gives me a indication it works.

Code:
function check_get()
    {
        // user check maybe unstable and not great
        $id = $this->user_id();
        $data = array('id' => $id);
        $this->response($data, 200); // 200 being the HTTP response code
    }

Now this works but i'm completely unsure if its the best way to go about it.

I hope this makes sense.

Any suggestions, advice or crit's welcome as i'm completely new and willing to learn more on this subject.

Thanks in advance.
#20

[eluser]Unknown[/eluser]
Phil,

I have been using your REST API for a few weeks now and it has worked GREAT! Thank you for publishing it! I have successfully configured multiple HTTP methods in my code (GET, POST, PUT and DELETE). I typically test with the browser (GET) and cURL (other methods). All works great for my 'first' API class, but I can't get my second class to work. Please note that my service files are in system/application/controllers/ws. Here is the code that does not work (I have the curl commented out in the code).

<?php

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

class Company extends REST_Controller
{
// curl -d "db_query=(company_name) values ('Simone Pringle LTD')" http://localhost:8082/ws/company/add_new_company

function add_new_company_post()
{
log_message('debug', "************* add_new_company_post ws");

$db_query = $this->post('db_query');

$query = $this->db->query("insert into company $db_query");
$company_id=$this->db->insert_id();

$this->response(
$company_id, 200
);
}

}

?>

Any ideas?

Your help would be tremendously appreciated!

Thanks,
Simone




Theme © iAndrew 2016 - Forum software by © MyBB