Main Menu




browser lang:en

Users Area

Client Login

today cache size is:82392

 

Holyguard rss
rss 2.0 for all sections

 

Portfolio

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




Comments

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
really good
02/11/2008 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:
0.0
0 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

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)

Display an image from a MySQL database in a web page via PHP

14/02/2010 

There's lots of clever scripts around to tell you how to get images in and out of…

in:PHP scripts (0 comments)

MySQL - Find Locations Nearest Known Coordinates

03/02/2010 

Spherical Law of Cosines Suppose that we want to find the five nearest places to (47.470779, -87.890699) using Spherical…

in:MySql (0 comments)

The 7 free best CSS editors

01/02/2010 

I have always used Dreamweaver, and love it. But, I have been thinking, what are the FREE CSS…

in:Free Software (0 comments)

Upgrade to Alsa 1.0.20 on Ubuntu Jaunty 9.04

09/01/2010 

Advanced Linux Sound Architecture (known by the acronym ALSA) is a Linux kernel component intended to replace the…

in:Blog (0 comments)

PHP - The Singleton Pattern

26/11/2009 

The Singleton Pattern is one of the GoF (Gang of Four) Patterns. This particular pattern provides a…

in:Scripts and tutorials (0 comments)

Read more »


Tag Clouds


Complete guide Jquery DevelopersHow insert HTML code posts12 undocumented tricks Google BuzzDisplay image from MySQL database page PHPMySQL Find Locations Nearest Known CoordinatesThe free best editorsUpgrade Alsa 1020 Ubuntu Jaunty


Add to Technorati Favorites