Part of the EllisLab Network
   
1 of 3
1
Introducing…REST!
Posted: 02 March 2008 01:30 PM   [ Ignore ]  
Grad Student
Rank
Total Posts:  62
Joined  06-11-2007

Hmm, if your anything like me, the fact that CI only supports XML-RPC is not enough. And unless you’ve been in a hole the last few years..then you’ve probably heard of REST, which stands for Representational State Transfer, which is a simple way of providing web services, using HTTP as a transport, and using URLs to identify services. I personally, am not a big fan of XML, so I’m not too big on protocols like XML-RPC and SOAP(mostly SOAP, because SOAP is such a hassle to screw with), and so, I looked around the CI forums for a REST library, and apparently there isn’t one. So, I wanted to make an easy way to implement REST in CodeIgniter(and also just PHP), which is just as simple as CI’s XML-RPC interface. So thus, the Lovable REST library was born. Now onwards, to API docs.

First, copy Rest.php into application/libraries.

To use it, your going to have to load it, example:

$this->load->library('rest');

After it is loaded, the library will be available at $this->rest, and the methods are as follows:

You can add methods via the addFunction function, example:

$this->rest->addFunction('name of service method to be used as a url identifier', 'name_of_method', 'data type'); /*Btw, the name of the method is a method of whatever object is passed via the serve function. The data type parameters can be: get, post, raw, and none. In order to use get as a service, you have to set your URI protocol to PATH_INFO or ORIG_PATH_INFO in your config.php. If you want to use PUT and DELETE, use raw as your data type.*/

Other functions in the API are jsonEncode, jsonDecode(which are just wrappers to decode/encode JSON), and serve, which propagates the request (You have to pass a reference of the object in which the API methods will reside, if they are in your controller, your $this), getRawInput, which returns the raw http post data, and enableGet, which you can use to enable $_GET manually.

Now to put it all together, here is an example of all of it in action:

class Api extends Controller
{
function _test($data)
    
{
        print_r
($data); /* $data almost always is an object, but if you specify raw as a data type, it will pass a string containing the RAW_HTTP_POST data. If you specify none, $data will be null */
    
}
function rest()
{
    $this
->load->library('rest');
    
$this->rest->addFunction('test', '_test', 'get');           
    
$this->rest->addFunction('testpost', '__test', 'post');
    
$this->rest->serve($this); //You have to pass a reference to the object that has the methods. In this example, it would be the controller. */
}
}

Now, to access the services,  you would use http://domain-name.com/controller/name_of_function_that_serve_is_called/function/function_name
so say, if the above controller was located at http://ilikecheese.com, you could access the testpost service at http://ilikecheese.com/api/rest/function/testpost

So…sorry for the long-winded rant, if anyone gets confused by my attempt at API docs, please post your question, and I will clarify.

Download:

http://www.mediafire.com/?t4g753ydmtp

Profile
 
 
Posted: 08 March 2008 10:42 PM   [ Ignore ]   [ # 1 ]  
Summer Student
Total Posts:  4
Joined  03-06-2008

thank you so much for sharing it smile))))))))))

Profile
 
 
Posted: 09 March 2008 09:29 AM   [ Ignore ]   [ # 2 ]  
Grad Student
Rank
Total Posts:  62
Joined  06-11-2007

No problem. Right now, its pretty basic, but I will try to develop some more functions, and I hope you find it useful!

Profile
 
 
Posted: 11 March 2008 11:00 AM   [ Ignore ]   [ # 3 ]  
Grad Student
Rank
Total Posts:  48
Joined  11-25-2007

Excellent!
Keep up your good work smile, I just can’t wait to see the next version

Profile
 
 
Posted: 04 April 2008 08:42 AM   [ Ignore ]   [ # 4 ]  
Summer Student
Total Posts:  19
Joined  04-01-2008

Hi,

I am getting

A PHP Error was encountered
Severity: Notice
Message: Undefined variable: array
Filename: libraries/Rest.php
Line Number: 25

I am sure that it has to do with the fact that I am using PHP 5.2 and Apache 2.2.6

$_SERVER[‘PATH_INFO’] = (isset($_SERVER[‘ORIG_PATH_INFO’])) ? $_SERVER[‘ORIG_PATH_INFO’] : ‘’;

The variables are not set.  Not sure what I should be setting this to.

Eric

Profile
 
 
Posted: 04 April 2008 08:32 PM   [ Ignore ]   [ # 5 ]  
Grad Student
Rank
Total Posts:  62
Joined  06-11-2007

Your problem is you do not have your URI protocol set to PATH_INFO, in your config/config.cfg, change $config[‘uri_protocol’] to “PATH_INFO”

Profile
 
 
Posted: 05 April 2008 04:26 AM   [ Ignore ]   [ # 6 ]  
Summer Student
Total Posts:  19
Joined  04-01-2008

Fixed it, just commented out
// $_SERVER[‘PATH_INFO’] = (isset($_SERVER[‘ORIG_PATH_INFO’])) ? $_SERVER[‘ORIG_PATH_INFO’] : ‘’;

Eric

Profile
 
 
Posted: 05 April 2008 05:48 AM   [ Ignore ]   [ # 7 ]  
Grad Student
Avatar
Rank
Total Posts:  67
Joined  11-15-2007

nice :D

Profile
 
 
Posted: 05 April 2008 06:38 AM   [ Ignore ]   [ # 8 ]  
Grad Student
Rank
Total Posts:  62
Joined  06-11-2007

Hmm…Im gonna have to fix that..but Im going to have an update of my REST library soon, that will try and fix that error, and add some extra features.

Profile
 
 
Posted: 05 April 2008 09:55 AM   [ Ignore ]   [ # 9 ]  
Research Assistant
RankRankRank
Total Posts:  362
Joined  04-05-2007

mmm I am not sure how i would go about using this to provide an API. Any chance someone could assemble a sample controller showing a request being made to get an array of values and return as json? Would be much appreciated.

Profile
 
 
Posted: 05 April 2008 10:12 PM   [ Ignore ]   [ # 10 ]  
Grad Student
Rank
Total Posts:  62
Joined  06-11-2007

well, I have functions that do JSON decoding/encoding,
$this->rest->jsonEncode and $this->rest->jsonDecode, just pass a variable to jsonEncode to encode it into JSON, it can be an array or object. and to decode use jsonDecode

class Api extends Controller
{
function _test($data)
    
{
        $this
->load->library('rest');
        echo
$this->rest->jsonEncode($obj_to_encode_into_json);
    
}
function rest()
{
    $this
->load->library('rest');
    
$this->rest->addFunction('test', '_test', 'get');           
    
$this->rest->addFunction('testpost', '__test', 'post');
    
$this->rest->serve($this); //You have to pass a reference to the object that has the methods. In this example, it would be the controller. */
}
}
Profile
 
 
   
1 of 3
1
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 721, on January 06, 2010 09:38 AM
Total Registered Members: 114993 Total Logged-in Users: 65
Total Topics: 122434 Total Anonymous Users: 3
Total Replies: 647290 Total Guests: 493
Total Posts: 769724    
Members ( View Memberlist )