browser lang:en
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);
public function changeYourState($state);
public function writeSomething($something);
require_once 'example.php';
$myExample = new example();
require_once 'jsonRPCServer.php';
require 'example.php';
$myExample = new example();
jsonRPCServer::handle($myExample)
or print 'no request';
public function writeSomething($something);
public function writeSomething($something) {
throw new Exception('writeSomething method
is not available for RPC');
}
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.
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());
}
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());
}
Selective Tweets is an application that allows you to control what tweets are added to your Facebook…
in:Social Networking (0 comments)In a FBML Facebook App, your quick jump menu will require a little tweak to work in FBJS…
in:Scripts and tutorials (0 comments)Every single day, someone, somewhere is discussing something important to your business; your brand, your executives, your…
in:Scripts and tutorials (0 comments)Although some IT managers are coming around to the idea of supporting iPhones, most aren’t convinced…
in:Free Software (0 comments)When you want to run your own mail server, and it does not matter what version and…
in:Apache - .htaccess (0 comments)Have you ever had to develop something yourself only to find out that there had already been…
in:JQuery (0 comments)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)So. Google just recently announced Google Buzz. I’m not sure about you, but I…
in:Blog (0 comments)
