1JSON::RPC(3) User Contributed Perl Documentation JSON::RPC(3)
2
3
4
6 JSON::RPC - Perl implementation of JSON-RPC 1.1 protocol
7
9 JSON-RPC is a stateless and light-weight remote procedure call (RPC)
10 protocol for inter-networking applications over HTTP. It uses JSON
11 as the data format for of all facets of a remote procedure call,
12 including all application data carried in parameters.
13
14 quoted from http://json-rpc.org/wd/JSON-RPC-1-1-WD-20060807.html
15 <http://json-rpc.org/wd/JSON-RPC-1-1-WD-20060807.html>.
16
17 This module was in JSON package on CPAN before. Now its interfaces was
18 completely changed.
19
20 The old modules - JSONRPC::Transport::HTTP and Apache::JSONRPC are
21 deprecated. Please try to use JSON::RPC::Server and JSON::RPC::Client
22 which support both JSON-RPC protocol version 1.1 and 1.0.
23
25 CGI version.
26
27 #--------------------------
28 # In your application class
29 package MyApp;
30
31 use base qw(JSON::RPC::Procedure); # Perl 5.6 or more than
32
33 sub echo : Public { # new version style. called by clients
34 # first argument is JSON::RPC::Server object.
35 return $_[1];
36 }
37
38
39 sub sum : Public(a:num, b:num) { # sets value into object member a, b.
40 my ($s, $obj) = @_;
41 # return a scalar value or a hashref or an arryaref.
42 return $obj->{a} + $obj->{b};
43 }
44
45
46 sub a_private_method : Private {
47 # ... can't be called by client
48 }
49
50
51 sub sum_old_style { # old version style. taken as Public
52 my ($s, @arg) = @_;
53 return $arg[0] + $arg[1];
54 }
55
56
57 #--------------------------
58 # In your triger script.
59 use JSON::RPC::Server::CGI;
60 use MyApp;
61
62 # simple
63 JSON::RPC::Server::CGI->dispatch('MyApp')->handle();
64
65 # or
66 JSON::RPC::Server::CGI->dispatch([qw/MyApp FooBar/])->handle();
67
68 # or INFO_PATH version
69 JSON::RPC::Server::CGI->dispatch({'/Test' => 'MyApp'})->handle();
70
71 #--------------------------
72 # Client
73 use JSON::RPC::Client;
74
75 my $client = new JSON::RPC::Client;
76
77 my $uri = 'http://www.example.com/jsonrpc/Test';
78 my $obj = {
79 method => 'sum', # or 'MyApp.sum'
80 params => [10, 20],
81 };
82
83 my $res = $client->call( $uri, $obj )
84
85 if($res){
86 if ($res->is_error) {
87 print "Error : ", $res->error_message;
88 }
89 else {
90 print $res->result;
91 }
92 }
93 else {
94 print $client->status_line;
95 }
96
97 # or
98
99 $client->prepare($uri, ['sum', 'echo']);
100 print $client->sum(10, 23);
101
102 See to JSON::RPC::Server::CGI, JSON::RPC::Server::Daemon,
103 JSON::RPC::Server::Apache JSON::RPC::Client and JSON::RPC::Procedure.
104
106 supports JSON-RPC protocol v1.1
107
109 Document
110 Examples
111 More Tests
112
114 Makamaka Hannyaharamitu, <makamaka[at]cpan.org>
115
117 Copyright 2007-2008 by Makamaka Hannyaharamitu
118
119 This library is free software; you can redistribute it and/or modify it
120 under the same terms as Perl itself.
121
122
123
124perl v5.12.0 2008-02-25 JSON::RPC(3)