Welcome Guest, Not a member yet? Register   Sign In
Introducing...REST!
#1

[eluser]Lovecannon[/eluser]
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:
Code:
$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:
Code:
$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:
Code:
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_o...ction_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
#2

[eluser]ezafy[/eluser]
thank you so much for sharing it Smile))))))))))
#3

[eluser]Lovecannon[/eluser]
No problem. Right now, its pretty basic, but I will try to develop some more functions, and I hope you find it useful!
#4

[eluser]Tony Nash[/eluser]
Excellent!
Keep up your good work Smile, I just can't wait to see the next version
#5

[eluser]ecarsted[/eluser]
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
#6

[eluser]Lovecannon[/eluser]
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"
#7

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

Eric
#8

[eluser]James Pax[/eluser]
nice Big Grin
#9

[eluser]Lovecannon[/eluser]
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.
#10

[eluser]garymardell[/eluser]
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.




Theme © iAndrew 2016 - Forum software by © MyBB