Main Menu




browser lang:en

Users Area

Client Login

today cache size is:222615

 

Holyguard rss
rss 2.0 for all sections

 

Portfolio

Some of my projects:
CTIBA
MA-NO Web Agency
BluPool
L'Pratone
Travel in Hotel
Yacht Elements
AbruzzoWeb
Il Mastino
CSI Teramo
Innovazione S.p.a.




Comments

John
24/07/2010 by John
better
16/04/2010 by bob
attached files not working !
05/11/2009 by Adnan
Tuvok
14/02/2009 by
Nice script
07/02/2009 by desaj
Other solutions
07/02/2009 by Mike
well
06/02/2009 by holy
Theif
06/02/2009 by

Read More »


RAPIDQ

Rapid-Q:The lost files
My old library where i have collected all the rapidq scripts and italian help


Today my pagerank is: Free Page Rank Tool

JSON-RPC PHP

The original document is at http://jsonrpcphp.org/

JSON-RPC PHP is a couple of classes written in PHP implementing respectively client and server functionalities of the JSON-RPC protocol.The goal of the project is the full encapsulation of the JSON-RPC approach inside a PHP application.
Via these classes, it become possible to offer the network the methods of each one's own classes as RPC services and/or enjoy RPC services exacltly as if they were local objects.

Example

This exercise makes both a server-side system and a client-side system to realize a complete JSON-RPC case-study. The two sides, as you're going to see, are totally indipendent and they complement themselves for the pratice pourpose only.

Server

Suppose to have got an hosted class example [code] doing some operations in the local system. The operations could be an informations request:

public function giveMeSomeData($param);


the supply of an information to be stored in the system:

public function changeYourState($state);


or a very important information storage in a database:

public function writeSomething($something);


moreover, there are some protected or private methods and some proprieties.
It's easy to conceive a localUsage example of the objects of the example class:

require_once 'example.php';
$myExample = new example();



Suppose, also, that the same functionalities must be offered to some partners attending on the network, acting on different hosts. A good way to aim the goal is JSON-RPC PHP. The RPC server could have the form:


require_once 'jsonRPCServer.php';
require 'example.php';

$myExample = new example();
jsonRPCServer::handle($myExample)
    or print 'no request';

That's all.
Once called with a weel formed JSON-RPC request, via HTTP-POST, the server will offer the same public methods of the example class, accept the same parameters and returns the same values.
The JSON-RPC language is detailed in the specification.
Obviously, offering all public methods could be more than desired. It's possible that some operations are wanted unavailable to remote consumers, but restricted to the local users of the class.
To be simple, JSON-RPC PHP doesn't offer a direct solution to this problem. This problem, in fact, can be easly resolved by the service developer with simple and ordinary techniques.

Suppose, e.g., that the method:
public function writeSomething($something);


should be unavailable to the remote users. Simply, an extension restrictedExample is required, overriding the writeSomething() in a dummy way as:

public function writeSomething($something) {
        throw new Exception('writeSomething method

                is not available for RPC');
    }


Once done, the writeSomething() method become unavailable from the network if the RPC has been builded via the new class. So, the new server [code], has the form:

require_once 'jsonRPCServer.php';
require 'example.php';
require 'restrictedExample.php';

$myExample = new restrictedExample();
jsonRPCServer::handle($myExample)
    or print 'no request';

Folks wanting test their clients, both server and localUsage of this example are available and working.

Client

The client usage is very simple.

suppose to know the existence of a JSON-RPC service, to know its methods and their features.

To simplify the example, suppose that the server could be the server of the above paragraph. As you'll see, it's unnecessary that both client and server are builded via JSON-RPC PHP classes.

Once the service features are known, a client example can be builded as follow:




require_once 'jsonRPCClient.php';
$myExample = new jsonRPCClient('http://localhost/server.php');

The objects of the jsonRPCClient class so builded have the same methods of the RPC service available.

That's all.

Folks wanting veerify the class behaviour, the client output is available and working.

Concurrent usage of the JSON-RPC PHP client and server

Concurrently using both classes gives an interesting example to explane the logic.

Observing the localUsage example (on the server side) of the example class and a client example on a remote host, it shows clearly that the resulting code is prefectly identical, except, obviously, the creation instruction.

The only difference could be that if on the server side the classe usage has been restricted (in the restrictedExample way), the modified or erased methods would not have the same effect.



localUsage [execute]


require_once 'example.php';
$myExample = new example();

// performs some basic operation
echo 'Attempt to perform basic operations
'."\n"; try { echo 'Your name is '.$myExample->giveMeSomeData('name').'
'."\n"; $myExample->changeYourState('I am using this function from the local environement'); echo 'Your status request has been accepted
'."\n"; } catch (Exception $e) { echo nl2br($e->getMessage()).'
'."\n"; } // performs some strategic operation, locally allowed echo '
Attempt to store strategic data
'."\n"; try { $myExample->writeSomething('Strategic string!'); echo 'Strategic data succefully stored'; } catch (Exception $e) { echo nl2br($e->getMessage()); }


client [execute]


require_once 'jsonRPCClient.php';
$myExample = new jsonRPCClient('http://jsonrpcphp.org/server.php');

// performs some basic operation
echo 'Attempt to perform basic operations
'."\n"; try { echo 'Your name is '.$myExample->giveMeSomeData('name').'
'."\n"; $myExample->changeYourState('I am using this function from the network'); echo 'Your status request has been accepted
'."\n"; } catch (Exception $e) { echo nl2br($e->getMessage()).'
'."\n"; } // performs some strategic operation, locally allowed echo '
Attempt to store strategic data
'."\n"; try { $myExample->writeSomething('Strategic string!'); echo 'Strategic data succefully stored'; } catch (Exception $e) { echo nl2br($e->getMessage()); }

This comparison explain definitively the JSON-RPC PHP spirit.

Rating:
60.0
4 votes
1 2 3 4 5

Comments

Tuvok by

14/02/2009 
Here’s a PHP jQuery JSON-RPC Server

http://www.tanabi.com/projects/jsonrpc


Insert your comment

Titolo
Messaggio
Nome Utente
e-mail (se vuoi ricevere le risposte a questo post anche via mail)

Videos


Contents

Selective Tweets Application for Facebook

21/07/2010 

Selective Tweets is an application that allows you to control what tweets are added to your Facebook…

in:Social Networking (0 comments)

FBJS Quick Jump Menu for a FBML Facebook Platform App

15/07/2010 

In a FBML Facebook App, your quick jump menu will require a little tweak to work in FBJS…

in:Scripts and tutorials (0 comments)

How to manage your online reputation, free tools forcommunity managers

02/07/2010 

Every single day, someone, somewhere is discussing something important to your business; your brand, your executives, your…

in:Scripts and tutorials (0 comments)

Genuitec MobiOne Enables Developers to Create Web Apps for the iPhone

28/05/2010 

Although some IT managers are coming around to the idea of supporting iPhones, most aren’t convinced…

in:Free Software (0 comments)

How to configure MX records for incoming SMTP e-mail traffic

27/03/2010 

When you want to run your own mail server, and it does not matter what version and…

in:Apache - .htaccess (0 comments)

Complete guide for Jquery Developers

27/02/2010 

Have you ever had to develop something yourself only to find out that there had already been…

in:JQuery (0 comments)

How can i insert HTML code in my posts?

14/02/2010 

The situation begins with your blog or website and you need to post some code on a particular…

in:The Holy Faq's (0 comments)

12 undocumented tricks for Google Buzz

14/02/2010 

So. Google just recently announced Google Buzz. I’m not sure about you, but I…

in:Blog (0 comments)

Read more »


Tag Clouds


Selective Tweets Application FacebookFBJS Quick Jump Menu FBML Facebook Platform AppHow manage your online reputation free tools forcommunity managersGenuitec MobiOne Enables Developers Create Apps iPhoneHow configure records incoming SMTP email


Add to Technorati Favorites