JSON-RPC Library |
[eluser]Nick Husher[/eluser]
After a little poking around, I realized that there's no JSON-RPC library for CodeIgniter. In fact, the state of JSON-RPC libraries for PHP is pretty sad. I decided to rectify that for CI, at least, by writing my own JSON-RPC server/client. It behaves similarly to CI's XML-RPC library, so it should be easy to set up if you're familiar with it. The server mostly adheres to the JSON-RPC 1.1 working draft while the client can be used to request JSON data from any source. CI JSON-RPC 1.0 Readme follows below:
[eluser]Nick Husher[/eluser]
Approximately conforms to the JSON-RPC 1.1 working draft. Because it's a working draft, there are certain aspects to it that are unfinished but, as a whole it is a significant improvement in quality over JSON-RPC 1.0. The server should be backwards-compatible with JSON-RPC 1.0, although this is untested. The Jsonrpc server comes in two parts, a client and a server. The client is used to request JSON information from remote sources, the serve is used to serve (mostly-)valid JSON-RPC content to requesting resources. The client supports requesting data via both GET and POST, while the server only responds to POST requests for the time being. Requirements: * PHP5 or other PHP with a defined json_encode function. * CodeIgniter (tested on v1.7) Installation: 1. Drop the file 'Jsonrpc.php' into your libraries folder 2. That's it! Using the JSON-RPC library: To use the library, load it in CodeIgniter. This can be done with $this->load->library('jsonrpc'). From there, you can access the client or server functionality with $this->jsonrpc->get_client() and $this->jsonrpc->get_server(). Both the client and the server were modeled of of CodeIgniter's included XML-RPC libraries, although there are certain differences. Using the Client: To access the client after loading the jsonrpc library, you can call $this->jsonrpc->get_client(), which returns the client object. You may want to pass this by reference (i.e. $my_client =& $this->jsonrpc->get_client()), although unless you're requesting data from a large number of sources, this shouldn't be a big issue. After you have your client object, it behaves similarly to the CI XML-RPC library. First, you need to set the server with $client->server(). The server function takes three arguments, only the first is required. The first argument is the URI to request the data from, the second argument is the method (either GET or POST, case-sensitive, defaults to POST), and the third is the port number (defaults to 80). You can then specify a method with $client->method(). Method takes a single string representing the JSON-RPC method. This may be empty if you are querying a JSON resource that doesn't adhere to the JSON-RPC spec. You can specify parameters with $client->request(), which takes an array representing the request parameters. Using the Server: To access the server after loading the jsonrpc library, you can call $this->jsonrpc->get_server(). As above, it may be a good idea to pass this by reference rather than by value. After you have your server object, you can specify the functions you would like to serve. This is similar to defining functions with the XML-RPC library, but with a few important differences. First, you need to define a list of functions. This can be done with the $server->define_methods() function, which takes an array of a particular format as its only argument. This array defines all the functions the server knows how to serve. Second, you need to set the object that contains the JSON-RPC functions. This is usually $this, but may be something different if you choose. You can do this with the $server->set_object() function. This is equivalent to the 'object' property of the CI XML-RPC configuration object. Last, to serve the content simply call the $server->serve() function.
[eluser]kerpunk[/eluser]
Hi, This looks like a great library! However, I'm having trouble getting the server response to respond correctly. I'm not sure if I have the right code in the controller, but I've tracked the problem I'm having down to the _execute() function in jsonrpc.php. I was wondering if I'd missed something, or if anyone had come accross the same problem? Code: function index() Again, great idea for a library, and I'd appreciate any help! Thanks Martin
[eluser]Nick Husher[/eluser]
Is the name of your containing class "test"? Here's some sample server code: Code: //json_states.php
[eluser]Nick Husher[/eluser]
I feel kinda bad, I released this library with pretty lousy documentation and then forgot about it. I'm starting a new CI project where I intend to use it for a web service, so I'll probably be refining and documenting things in the months to come.
[eluser]ciscoheat[/eluser]
Hello Nick, thanks for a great work on this library! I'm very interested in the library and documentation, so if you post any progress here, that would be great.
[eluser]kerpunk[/eluser]
Hey Nick, That's awesome, thanks for the quick response! Would you be able to post a little demo of how to handle the parameters sent from the client to the server? I have the code below in the client, but can't figure what to put in the server code to access these parameters. Code: $client->request(array('message' => 'hello world')); Also, I edited line 466 in jsonrpc.php from: Code: echo $response->create_server_response() to: Code: $e = new JSON_RPC_Parser(); I'm not sure if i've misunderstood something here, but this returns the array to the client already in json format, rather than using a print_r to access the array / object. Again, thanks for the awesome library :-)
[eluser]CarNinja[/eluser]
I rehosted the original .php file for this... had to dig it up in my source history: http://xeno.com/hosted/Jsonrpc.txt
[eluser]mjsilva[/eluser]
Hi Nick, tks for the code share, First hit on google searching by json-rpc codeignitor ![]() Well I'm going to need something to make two codeignitor projects talking between them, I look into xml-rpc default library, witch felt pretty strait forward to work with, but I still prefer JSON due to small footprint. I will test this library, do you still plan to further develop it? |
Welcome Guest, Not a member yet? Register Sign In |