Professional Documents
Culture Documents
RPC
RPC
RPC
Miu t
Client or server
process
RPC stubs
Nhng h thng chng trnh con c dng bi client hoc server khi ng yu cu RPC.
Marshalling engine
Cung cp mt giao din chung gia RPC Client v RPC Server v c chia lm 2 loi:
NDR20 v NDR64. NDR20 c dng cho h tng 32 bits. Trong khi NDR64 c ti u
(NDR20 hoc
dng cho h tng 64 bits. Client v Server s thng lng nn chn NDR20 hay NRD64
NDR64)
Runtime application
programming
interface (API)
Connection RPC
protocol engine
Datagram RPC
c s dng khi RPC yu cu giao thc phi kt ni. Lp ny s ch nh s dng giao thc
protocol engine
Registry
RPC
Kernel32.dll l mt file th vin ng 32 bits c trong Windows NT. File ny chu trch nhim
Win32 APIs
(kernel32.dll,
Advapi32.dll l file nng cao ca Windows 32 da trn giao din lp trnh ng dng. File ny
advapi32.dll,
h tr v bo mt v gi cc registry
ntdll.dll)
SSPI
Cung cp giao din bo mt cho RPC. File secur32.dll s thng lng cch dng cho vic
(secur32.dll)
chng thc v m ha nh: Kerberos, NTLM, hoc Secure Sockets Layer (SSL)
Rpcss.dll (Remote procedure call subsystem) ch yu cung cp c s h tng cho cc dch
v COM, nhng mt phn ca Rpcss.dll c dng cho EPM. RPC Server lin lc vi EPM
Endpoint Mapper
(EPM)
Ri sau khi RPC Clients mun kt ni ti RPC Server, n s lin lc vi EPM nh EPM
(rpcss.dll)
Active Directory
Used in the RPC client process only when the security interface specifies Kerberos or
Negotiate as the security provider or when the server uses NTLM as the security provider.
Network stack
Kernel
RPC Type
Connectionoriented
Connectionoriented
Named Pipe
Connectionoriented
HTTP
Connectionoriented
Connectionless
Connectionless
Khi Server nhn c yu cu ca RPC(t pha client trong ni b hoc client t xa), cc hm trong
th vin chy thc RPC ca Server chp nhn cc yu cu v gi hm x l Server Stub. Server stub
s truy vn cc tham s t network buffer v chn 1 trong 2 loi NDR20 hoc NDR64 (trong NDR
Marshalling Engines), sau chuyn i chng t nh dng truyn ti mng sang nh dng theo
yu cu bi my ch. Sau cc th tc t xa s c chy, c kh nng xut ra cc tham s v tr
v gi tr. Khi cc th tc t xa hon tt, mt chui cc bc tng t s tr v d liu cho Client
Cc th tc t xa tr d liu ca n v cho Server Stub, chn 1 trong 2 loi NDR20 hoc NDR64
(trong NDR Marshalling Engines), chuyn i nhng tham s c xut ra thnh nh dng truyn ti
mng n client v tr chng vo th vic chy thc RPC ca Server. Sau th vin chy thc RPC
ca Server s truyn ti d liu n my tnh ca Client bng LRPC hoc qua network.
Client hon tt cc th tc bng cch chp nhn d liu qua mng v tr d liu v gi hm. Th
vin chy thc RPC ca Client nhn c th tc t xa tr v gi tr, chuyn i gi tr t NDR 20
hoc NDR64 v nh dng c dng bi Client, v tr chng v client stub.
i vi Microsoft Windows, th vin chy thc c chia lm 2 phn:
1. Import Library: lin kt vi cc ng dng
2. Th vin chy thc RPC( RPC Runtime Library): c trin khai nh l DLL
IV. Cc Ports dng cho RPC
Cc chng trnh RPC Server thng thng dng nhng port ng ( trnh gy xung t vi cc
chng trnh v cc giao thc c ng k trong dy Well-known TCP Ports). Bng di y s
lit k cc port dng cho RPC
Service Name
UDP
TCP
HTTP
Named Pipes
445
445
135
135
<Dynamically assigned>
<Dynamically assigned>
Phn Client l mt qu trnh ngi dng, ni khi to mt li gi th tc t xa. Mi li gi th tc xa trn phn Client
s kch hot mt th tc cc b tng ng nm trong phn Stub ca Client.
Phn ClientStub cung cp mt b cc hm cc b m phn Client c th gi. Mi mt hm ca ClientStub i din cho
mt hm xa c ci t v thc thi trn Server.
Mi khi mt hm no ca ClientStub c gi bi Client, ClientStub s ng gi mt thng ip m t v th tc
xa tng ng m Client mun thc thi cng vi cc tham s nu c. Sau n s nh h thng RPCRuntime cc b
gi thng ip ny n phn Server Stub ca Server.
Phn RPCRuntime qun l vic truyn thng ip thng qua mng gia my Client v my Server. N m nhn vic
truyn li, bo nhn, chn ng gi tin v m ha thng tin.
RPCRuntime trn my Client nhn thng ip yu cu t ClientStub, gi n cho RPCRuntime trn my Server bng
lnh send(). Sau gi lnh wait() ch kt qu tr v t Server.
Khi nhn c thng tip t RPCRuntime ca Client gi sang, RPCRuntime bn pha server chuyn thng ip ln
phn ServerStub.
ServerStub m thng ip ra xem, xc nh hm xa m Client mun thc hin cng vi cc tham s ca n.
ServerStub gi mt th tc tng ng nm trn phn Server.
Khi nhn c yu cu ca ServerStub, Server cho thc thi th tc c yu cu v gi kt qu thc thi c cho
ServerStub.
ServerStub ng gi kt qu thc trong mt gi tin tr li, chuyn cho phn RPCRuntime cc b n gi sang
RPCRuntime ca Client .
RPCRuntime bn pha Client chuyn gi tin tr li nhn c cho phn ClientStub. ClientStub m thng ip cha kt
qu thc thi v cho Client ti v tr pht ra li gi th tc xa.
Trong cc thnh phn trn, RPCRuntime c cung cp bi h thng. ClientStub v ServerStub c th to ra th cng
(phi lp trnh) hay c th to ra bng cc cng c cung cp bi h thng.
C ch RPC c h tr bi hu ht cc h iu hnh mng cng nh cc ngn ng lp trnh.
dng trong dch v web soap c s dng rng ri trong nhiu ngn ng lp trnh nh
java, android,..php,..
Cc gi/namespace c s dng vi wordpress xml-rpc l: Blogger API, metaWeblog
API, Movable Type API, Pingback API.
XML-RPC l g?
Nu bn khng bit khi nim RPC th c cc bi vit sau y.
Trang ch XML-RPC: http://www.xmlrpc.com
XML-RPC cho ngi mi nhp mn:
http://davenet.scripting.com/1998/07/14/xmlRpcForNewbies
Vy RPC l g?
RPC l li gi th tc (hay gi l hm), chc cc bn lm quen vi vic to v gi
hm. Khi mt on trng trnh c vit lp i lp li chng ta nhm chng vo mt
hm hay th tc v gi hm vi cc tham s thch hp. Cc th tc hoc hm bn c
th:
trong 1 file ngun khc v c include vo file ngun chnh khi cn,
a ch XML-RPC l:
http://yourwordpress/xmlrpc.php?rsd
Test kt ni xmlrpc bng lnh curl. Cch lm vic ny ging giao thc xmpp. Thc thi
cng vic thng qua vic truyn ti on xml ln server bng phng thc POST, v
y l nguyn l khi gi hm. Trong v d sau y gi ni dung xml cha thng tin gi
hm vo xmlrpc wordpress server.
curl -d "<?xml version='1.0' encoding='iso-8859-1'?
><methodCall><methodName>system.listMethods</methodName><params/></methodCall>"
"http://localhost/wordpress/xmlrpc.php"
.
Ch :
Kt qu:
ng k hm xmlrpc mi
Wordpress xmlrpc api c filter xmlrpc_methods gip bn to phng thc ring.
ng k hm rpc vi on code sau, copy vo theme functions.php
method name, and hello_world is the callback, namespace 'frs' for any you want.
return $methods;
}
= $params[1];
Xa hm rpc
Tng t nh cch to hm, s dng xmlrpc_methods xa cc hm khng mong
mun. Thm vo functions.php
function mynamespace_remove_xmlrpc_methods( $methods ) {
unset( $methods['demo.addTwoNumbers'] );
unset( $methods['frs.helloWorld'] );
return $methods;
}
add_filter( 'xmlrpc_methods', 'mynamespace_remove_xmlrpc_methods');
XMLRPC Client
To lp s l gi hm t XMLRPC Server. To
lp XMLRPC_Client vi ni dung sau:
/*create XML-RPC PHP client*/
class XMLRPC_Client {
private $url;
/**
* Call the XML-RPC method named $method and return the results, or die trying!
*
* @param string $method XML-RPC method name
* @param mixed ... optional variable list of parameters to pass to XML-RPC call
*
* @return array result of XML-RPC call
*/
public function call() {
// get arguments
$params = func_get_args();
$method = array_shift( $params );
$ch = curl_init();
$this->url );
true );
$post );
Gi hm rpc
Kt ni ti XMLRPC Server s dng class XMLRPC_Client . To instance ca
class XMLRPC_Client v gi hm rpc trn XMLRPC Server bi phng thc call .
//call method
$client = new XMLRPC_Client( "http://127.0.0.1:81/wordpress/xmlrpc.php" );
$available_methods = $client->call( 'system.listMethods' );
print_r( $available_methods );
Xy dng hm RPC
Ly gi tr tham s truyn vo hm
function hello_world( $params ) {
global $wp_xmlrpc_server;
$blog_id = (int) $params[0]; // tham s u tin khng c s dng, tham s ny tr v id ca blog
wordpress (built in XML-RPC actions)
//tham s 1
$username = $params[1];
$password = $params[2];
//tham s 2
$args
//tham s 3
= $params[3];
S l li
Mt v d v cch s l li trong hm rpc, on code ny kim tra ngi dng nu
khng c quyn chnh sa bi vit, th xut li 403 km theo tin nhn.
function hello_world( $params ) {
...
// check for edit_posts capability (requires contributor role)
// (obviously not required for this simple example, but just for demonstration purposes)
if ( ! current_user_can( 'edit_posts' ) )
return new IXR_Error( 403, __( 'You are not allowed access to details about orders.' ) );
...
}
V d:
$client->call( 'frs.helloWorld', 1, 'xx', 'yy', array( 'age' => '23' ) );
Mc d khng phi chun chnh thc, nhng XML-PRC c s dng kh rng ri. Ti sao vy,
l v n n gin v r rng ngay t khi a ra. Bn d dng nhn ra iu ny khi so snh 1500 t
m t cho chun XML-PRC vi 11000 t m t cho chun SOAP. V iu ng quan tm nht l
XML-PRC c h tr rt tt trong PHP, bn cng c th ci t trn bt c ngn ng no m bn
mun.
Chi tit so snh hai chun ny bn c th c bi bo XML-RPC vs. SOAP. Tm li, SOAP c a
ra vi nh b p thiu st trong chun XML-RPC, a ra c ch mc chuyn nghip thc s
cho vic trao i d liu gia cc h thng. V d phn pht mng nhiu chiu th s dng
chun SOAP s n gin hn rt nhiu so vi dng chun XML-PRC, nhng s khng c g sai nu
bn s dng chun XML-PRC. c bit XML-PRC l chun n nh, trong khi chun SOAP c
th phi tri qua vic nh ngha li trong tng lai.
Bn c bn khon v vn : Bn u t hc chun XML-PRC trong khi h thng m bn kt ni
n li s dng chun SOAP. Bn ng lo lng vn ny, bi v bn c th d dng chuyn
mtrequest theo chun SOAP sang request theo chun XML-PRC bng cch s
dng XSLT (XSL Transformations).
n y bn s t ra cu hi: Vy XML-PRC s hot ng th no? Thc s th bn c th s dng
n m khng cn bit nhiu v chun ny. Nu bn mun tm hiu k hn bn c th cXML-RPC
for Newbies. y chng ti ni nhanh cch m XML-PRC lm vic v sau chuyn sang vn
lm th no s dng c n.
* C ch hot ng ca chun XML-PRC
Hai thnh phn chnh trong thng ip (message) XML-PRC l phng thc (methods)
vtham s (parameters). Phng thc tng ng vi hm m bn nh ngha trong PHP,
trong khi tham s tng ng vi cc bin m bn truyn vo hm ny. Tham s c th c
nhiu kiu khc nhau nh kiu xu, kiu s nguyn, kiu mng, cng tng t nh cc kiu bin
m bn s dng trong PHP. Thm vo , XML-PRC nh ngha cc th khc s dng cho vic
iu khin li nhng bn c th b qua bi trong hu ht cc trng hp bn khng cn lo lng v
chng.
Mt cuc i thoi (conversation) s dng giao thc XML-PRC bt u bng
mt request tXML-PRC Client, request cha mt phng thc v mt s tham s cn thit cho
phng thc.Response tr li vi cc tham s cha d liu c yu cu. Qu trnh x l ging
nh mt hm m bn nh ngha trong PHP, bn gi hm v truyn tham s cho chng, sau
hm tr li mt vi bin yu cu.
Di y l mt v d request ca Client m pha server thy c:
POST
User-Agent:
Host:
Content-type:
Content-length:
<?xml
<methodCall>
<params>
/xmlrpcInterface
Sitepoint
XML-RPC
HTTP/1.0
Client
1.0
xmlrpc.sitepoint.com
text/xml
195
version="1.0"?>
<methodName>forums.getNumTodaysThreads</methodName>
<param><value><string>PHP
Development</string></value></param>
</params>
</methodCall>
Cn y l response t server m client thy c:
HTTP/1.1
200
OK
Connection:
close
Content-Length:
content-Type:
Date:
Wed,
Server:
148
Jul
Sitepoint
<?xml
28
1999
XML-RPC
15
04
text/xml
GMT
Server
1.0
version="1.0"?>
<methodResponse>
<params>
<param>
<value><int>42</int></value>
</param>
</params>
</methodResponse>
y ch l mt v d n gin, gi mt tham s ti mt phng thc, v ly v mt tham s. XMLPRC cung cp nhiu hn th, n cho php bn truyn ln server vi d liu phc tp (nh mng)
v nhn v response chi tit cha tt c d liu c sp xp.
Bn rng, RPC trong XML-RPC l vit tt ca t Remote Procedure Call. Pha client
s gi mt hm (hay phng thc) trn my ch. Nh vy XML-RPC l mt chun XML s dng
trao i d liu bng cch s dng li gi hm t xa Remote Procedure Call.
XML-PRC nm trong lp ng gi (packaging layer) m Kevin m t trong bi Web Services
Demystified.
* Mt s d n m ngun m xy dng theo chun XML-PRC
Chng ta xem mt s d n m ngun m h tr bn lp trnh sinh ra cc request v response
theo chun XML-PRC (Xem chi tit ti http://www.xmlrpc.com/directory/1568/implementations).
y chng ta nh gi trn hai mc tiu chnh: Mt bao lu ci t v s dng v h tr PHP
mc no.
+ XMLRPC-EPI (Xem http://xmlrpc-epi.sourceforge.net)
XMLRPC-EPI ban u c pht trin ring cho trang epinions, v n thnh cng ti mc m
hin ti n cung cp cho PHP nhng hm XML-PRC c bn (hm a ra da trn kinh nghim). Bn
thn XMLRPC-EPI c vit bng C++ v vy vic ci t n khng phi l d dng nu bn
khng c quyn root.
Bn cnh , XMLRPC-EPI ch h tr thao tc vi request v response theo chun XML-PRC m
khng gi v nhn cc request ca chnh n.
+ eZ xmlrpc (Xem http://developer.ez.no/article/static/53/)
c pht trin bi Brd Farstad, y l lp XML-PRC s dng trong ezPublish nhn cc
request t desktop client. Lp c thit k rt tt, ti liu y v r rng
(http://zez.org/article/articleview/47/).
Nhng c vn kh ln l yu cu s dng xml parser phi h tr sn cho PHP. M mc nh
ci PHP chun th th vin ny khng h tr.
+ Fase 4 XML-RPC
Xem http://www.fase4.com/xmlrpc/
+ phpRPC (Xem http://sourceforge.net/projects/phprpc/)
c pht trin bi Useful Inc, h tr y chun XML-PRC v h tr debug rt tt. Ti liu
tt v c nhiu v d (http://www.zend.com/zend/tut/tut-xml-rpc-client.php). Nhng lp ny
khng phi l m hnh l tng cho nhng ngi mi m ch tt vi nhng nh pht trin PHP c
kinh nghim.
+ Keith Devens' XML-RPC Client/Server (Xem http://www.keithdevens.com/software/xmlrpc/)
Bn vo trang ch bn s thy rng rt d bt u. M rt n gin vi cc hm PHP ch
cninclude trc tip vo script ca bn. XML-PRC Client/Server ch lm vic vi kiu d liu m
chng a ra. iu ny c ngha l XML-RPC Client kim tra cn thn d liu nhn v, nhng
bn ca ch lo lng khi s dng cc bin PHP ch khng phi cc tham s XML-RPC.
Ti liu cho th vin ny rt tt. V vy chng ta s s dng th vin ny xy dng Web
Service u tin cho mnh.
* ng dng Web Service u tin vi th vin XML-RPC Client/Server ca Keith Devens
No, chng ta bt u xy dng mt Web Service public cc tin t trang web ca bn theo
dng RSS Feed.
Chng ta nh ngha hai phng thc XML-RPC s dng:
- "news.getNewsList": Cung cp mt danh sch cc tin.
- "news.viewNewsItem": Hin th tng tin n l dng y .
S c mt phng thc mc nh "method_not_found" trong trng hp c nhng rc ri. Mi
phng thc s tng ng mt hm tng ng vi mt hm m chng ta nh ngha trong PHP.
y chng ti s dng PHP 4.1.0 hoc mi hn, v truy cp ti CSDL MySQL (v3.23+).
+ Chun b
- Ti m ngun ca bi ny (Download m ngun) (Trong b m ngun ny c th
vinkd_xmlrpc.php)
- Bn gii nn b m ngun trn ln th mc Web Server, khi ton b m ngun trong th
mc kd_xmlrpc v trong th mc c tp kd_xmlrpc.php chnh l th vin m chng ta
dng. Cc tp khc m chng ta ni sau u c trong th mc ny.
- To CSDL tn l "webservice_demo", to mt bng trong CSDL bng truy vn: CREATE
TABLE kd_xmlrpc_news (news_id mediumint(9) NOT NULL auto_increment, title varchar(255)
default NULL, short_desc text, full_desc text, author varchar(100) default NULL, date datetime NOT
NULL default '0000-00-00 00:00:00', PRIMARY KEY (news_id)) TYPE=MyISAM COMMENT='XML RPC
News';
- Thm vo mt s d liu cho bng.
+ XML-RPC Server
Chng ta to XML-RPC Server, v lu n vo tp server.php:
<?php
/*
server.php
/*
Variables
$dbserver=
$db
for
"localhost";
$dbuser
$dbpassword
"root";
=
/*
Connect
$link
accessing
//
"webservice_demo";
//
"";
Hostname
//
//
to
MySQL
of
Name
MySQL
of
user
your
@mysql_connect
server
MySQL
with
database
access
for
to
MySQL
MySQL
($dbserver,
*/
MySQL
your
Password
the
if
echo
*/
server
$dbuser,
"Unable
to
user
*/
$dbpassword);
(!
(
$db
$link){
connect
to
db"
);
exit();
}
/*
Select
the
database
if
(!mysql_select_db
($db,
*/
$link)
exit
();
}
/*
Include
Keiths
xml-rpc
library
*/
include("kd_xmlrpc.php");
/*
Include
file
that
defines
all
the
xml-rpc
"methods"
*/
include("web_service_api.php");
/*
Now
client
use
the
XMLRPC_parse
connects
and
$xmlrpc_request
/*
From
the
"What
turn
PHP
variables
would
Get
news
the
into
you
like
lets
parameters
Tell
me
the
if
response
from
PHP
what
xml-rpc
variables
method
name
do
for
to
with
which
that
one
method
you
e.g
"So
want.
=
check
data
*/
ie.
server
asks
you?"
*/
XMLRPC_getMethodName($xmlrpc_request);
associated
me
POST
normal
get
item.
Error
take
XMLRPC_parse($GLOBALS['HTTP_RAW_POST_DATA']);
generated,
$params
/*
to
it
$methodName
/*
function
you
want
What's
to
the
view
id#?"
a
*/
XMLRPC_getParams($xmlrpc_request);
method
was
used
to
that
doesn't
the
exist,
return
the
error
client
*/
if(!isset($xmlrpc_methods[$methodName])){
$xmlrpc_methods['method_not_found']($methodName);
/*
Otherwise,
note
the
let's
run
the
functions
response
PHP
function
themselves
return
to
corresponding
the
the
correct
to
that
method
formatted
xml-rpc
client
*/
}else{
/*
Call
the
method
notice
$params[0]
documentation
not
just
$params
as
states.
$xmlrpc_methods[$methodName]($params[0]);
}
?>
Chng ta s ni qua, tp server.php thc hin:
- Kt ni ti MySQL ly d liu
- Include tp kd_xmlrpc.php chng ta c th s dng cc hm ca Keith.
- Include web_service_api.php, tp m chng ta nh ngha cc phng thc XML-PRC.
the
*/
<?php
/*
/*
web_service_api.php
Define
an
array
to
name
the
PHP
xmlrpc
*/
methods
and
their
corresponding
functions
*/
array();
$xmlrpc_methods
$xmlrpc_methods['news.getNewsList']
news_getNewsList;
$xmlrpc_methods['news.viewNewsItem']
news_viewNewsItem;
$xmlrpc_methods['method_not_found']
/*
Now
useful
which
function
can
for
=
converting
then
MySQL
XMLRPC_method_not_found;
datetime
be
to
used
UNIX
timestamp
with
the
XMLRPC_convert_timestamp_to_iso8601($timestamp)
This
is
It
function.
not
comes
method!
from: http://www.zend.com/codex.php?id=176&single=1 */
function
mysql_datetime_to_timestamp($dt)
$yr=strval(substr($dt,0,4));
$mo=strval(substr($dt,5,2));
$da=strval(substr($dt,8,2));
$hr=strval(substr($dt,11,2));
$mi=strval(substr($dt,14,2));
$se=strval(substr($dt,17,2));
return
mktime($hr,$mi,$se,$mo,$da,$yr);
}
/*
Function
Allows
for
ordering
listing
by
news
column
items,
name
corresponding
and
result
to
the
limit
news.getNewsList
of
up
to
20
method
rows
*/
function
/*
news_getNewsList
Define
an
array
$order_fields
/*
check
if
(
=
we'll
accept
$query_info['order']
has
value
to
BY
"
ORDER
"author",
BY
in
variable
*/
&&
value
$order_fields
",
date
DESC
";
else
$order
"ORDER
*/
);
acceptable
$order
$query_info['order']
query
"title"
an
the
our
$query_info['order']
$query_info['order'],
"ORDER
to
correct
ISSET
in_array
$order
see
the
names
$query_info=0
array
to
assign
if
column
Now
and
of
BY
date
DESC
";
}
/*
Now
check
items
we
if
ISSET
$limit
for
want
returned,
$query_info['limit']
and
assign
$query_info['limit']
"LIMIT
to
0,
the
&&
"
specify
the
correct
number
value
$query_info['limit']
of
to
$limit
<
$query_info['limit']
news
20
)
"
else
$limit
*/
{
";
{
"LIMIT
0,
";
}
/*
Now
$query
build
"SELECT
$sql
FROM
the
kd_xmlrpc_news
mysql_query
if
/*
"
*/
$order
$limit;
$query
$sql
$news_items
while
query
);
$result
Extract
the
array();
mysql_fetch_array
variables
we
$news_item['news_id']
want
$sql
from
$news_item['date']
)
the
row
*/
$result['news_id'];
XMLRPC_convert_timestamp_to_iso8601(
mysql_datetime_to_timestamp(
$result['date']
);
$news_item['title']
$result['title'];
$news_item['short_desc']
$result['short_desc'];
$news_item['author']
$result['author'];
/*
Add
to
the
$news_items[]
$news_items
array
*/
$news_item;
}
/*
then
Convert
the
$news_items
respond
array
with
to
set
the
of
XML-RPC
XML.
parameters
*/
XMLRPC_response(XMLRPC_prepare($news_items),
}
/*
else
If
there
was
XMLRPC_error("1",
.
KD_XMLRPC_USERAGENT);
an
error,
respond
"news_getNewsList()
mysql_error()
"\nQuery
{
with
error:
was:
"
an
error
Unable
message
to
$query,
read
*/
news:"
KD_XMLRPC_USERAGENT);
}
}
/*
Function
for
viewing
full
news
news.viewNewsItem
function
/*
$query
the
"SELECT
$sql
/*
FROM
to
$news_id
fetch
kd_xmlrpc_news
the
WHERE
mysql_query
$result
the
variables
Extract
to
the
*/
query
if
corresponding
method
news_viewNewsItem
Define
item
news
news_id
'"
mysql_fetch_array
for
sending
$news_item['news_id']
item
*/
$news_id
"'";
$query
(
$sql
in
our
);
server
response
$news_item['date']
*/
$result['news_id'];
XMLRPC_convert_timestamp_to_iso8601(
mysql_datetime_to_timestamp(
$result['date']
);
$news_item['title']
$result['title'];
$news_item['full_desc']
$result['full_desc'];
$news_item['author']
$result['author'];
/*
Respond
to
the
client
with
the
news
XMLRPC_response(XMLRPC_prepare($news_item),
}
/*
item
*/
KD_XMLRPC_USERAGENT);
else
If
there
was
XMLRPC_error("1",
an
error,
respond
"news_viewNewsItem()
{
with
error:
fault
Unable
code
to
mysql_error(),
instead
read
*/
news:"
KD_XMLRPC_USERAGENT);
}
}
/*
Function
for
when
the
request
function
method
name
doesn't
exist
*/
XMLRPC_method_not_found($methodName){
XMLRPC_error("2",
"The
was
",
method
not
you
requested,
found.",
"
$methodName
KD_XMLRPC_USERAGENT);
}
?>
Tp thc hin:
- u tin chng ta nh ngha mt mng PHP $xmlrpc_methods, mng ny cha danh sch cc
<?php
/*
client.php
/*
Include
the
include
/*
library
(
Define
*/
"kd_xmlrpc.php"
variables
to
find
$site
the
rpc
);
server
script
$location
/*
*/
function
$date
"localhost";
Function
to
"/kd_xmlrpc/server.php";
give
us
convert_date
=
nice
date
$date
XMLRPC_convert_iso8601_to_timestamp
return
back
(
date
*/
"D
$date
$date
*/
H:i:s",
);
);
}
?>
<!doctype
<html>
<head>
html
public
"-//W3C//DTD
HTML
4.0
Transitional//EN">
<title>
KD
XML
<meta
RPC
News
Client
name="Generator"
<meta
content="EditPlus">
name="Author"
<meta
content="HarryF">
name="Keywords"
<meta
name="Description"
</title>
content="XML
content="Gets
news
RPC">
form
server.php">
</head>
<body>
<?php
/*
If
user
if
/*
is
ISSET
$success
the
XML
the
viewing
is
single
(fail)
to
the
POST
correct
item,
$_GET['news_id']
server
succeeded
script,
).
calling
parameters
list($success,
news
the
using
$response)
to
this
XMLPRC_request
method
{
preforms
and
sending
XMLRPC_prepare
=
*/
*/
XMLRPC_request(
$site,
$location,
'news.viewNewsItem',
array(XMLRPC_prepare($_GET['news_id']),
'HarryFsXMLRPCClient')
);
/*
If
all
went
well,
if
show
the
article
*/
($success)
?>
<table
align="center"
width="600">
<tr
<th
valign="top">
colspan="2"><b><?php
echo
$response['title']
);?></b></th>
</tr>
<tr
valign="top">
<th><?php
<th><?php
echo
echo
$response['author']
convert_date
);?></th>
$response['date']
);?></th>
</tr>
<tr
valign="top">
<td
colspan="2">
<?php
echo
nl2br
$response['full_desc']
);?>
</th>
</tr>
</table>
<?php
/*
Else
display
}
echo
the
error
*/
else
(
"<p>Error:
"
nl2br
{
(
$response['faultString']
);
}
}
/*
else
Define
the
parameters
to
pass
to
the
XML-RPC
$query_info['limit']
as
PHP
array
$query_info['order']
/*
method
10;
XMLRPC_prepare
works
on
an
*/
"author";
array
and
converts
it
to
XML-RPC
parameters
*/
list($success,
$response)
XMLRPC_request(
$site,
$location,
'news.getNewsList',
array(XMLRPC_prepare($query_info),
'HarryFsXMLRPCClient')
);
/*
On
success,
display
the
if
as
HTML
table
*/
($success)
echo
"<table
align=\"center\"
$count
while
list
width=\"600\">\n"
);
=
(
list
$key,
$val
0;
each
$response
?>
<tr
valign="top">
<td
colspan="2">
<a
href="<?php
echo
echo
$_SERVER['PHP_SELF']
);?>?news_id=<?php
$response[$count]['news_id']
);
?>">
<?php
echo
$response[$count]['title']
);
?>
</a>
</td>
</tr>
<tr
valign="top">
<td
colspan="2">
<?php
echo
$response[$count]['short_desc']
);
?>
</td>
</tr>
<tr
valign="top">
<td>
<?php
echo
$response[$count]['author']
);
?>
</td>
<td>
<?php
</td>
</tr>
echo
convert_date
$response[$count]['date']
);
?>
<?php
$count++;
}
echo
"</table>\n"
/*
Or
error
}
echo
);
*/
else
(
"<p>Error:
"
nl2br
{
(
$response['faultString']
);
}
}
;?>
</body>
</html>
lit k danh sch tin, chng ta s dng phng thc news.getNewsList. Ch rng chng ta
thit lp $query_info['limit'] ti 10 (hin th ti a 10 tin) v $query_info['order'] c thit lp l
"author" (sp xp theo tn tc gi). y l cc tham s s c gi trong XML-RPC request ca
client. Khi hin th mt tin th $_GET['news_id'] xc nh id ca tin mun ly.
Cc hm XML-RPC c s dng:
- XMLRPC_prepare(): Ging nh s dng trong web_service_api.php.
- MLRPC_request(): Hm ny nhn 5 tham s v tr v mt mng.
$site: Tn min server.
$location: ng dn ti tp cha server script.
$methodName: Tn phng thc XML-RPC c gi
$params: Tham s cho phng thc
$user_agent: L ty chn, c th l bt k v d nh: HarryFsXMLRPCClient.
Mng tr v gm hai bin. Bin u tin l 0 nu li, l 1 nu thnh cng. Bin ny thng c
s dng check li. Bin th hai l mt mng nhiu chiu cha tt c d liu tr v.
- XMLRPC_convert_iso8601_to_timestamp: S dng trong hm convert_date(), chng ta s dng
hm c bit chuyn nh dng ngy t XML-RPC sang timestamp ca PHP.
By gi bn chy th a ch: http://localhost/kd_xmlrpc/client.php
+ nh gi
- S dng on m XML-RPC ca Keith Devens l cch d nht to Web Service cho chnh
bn, l ni tt nht bn tip cn vi Web Service. Vi v d trn bn c th nng cp lm sao
gip ngi dng bit c tin no l mi v c th h tr sp xp theo tc gi, theo tiu hay
theo ngy. Nu bn c k hoch ln lin quan ti Web Service bn c th s dng
lpphpxmlrpc ca Userland.
C ba vn chnh v security m chng ta phi quan tm. l:
+ Lm th no bn c th chng thc c XML-RPC Client: Trong mt vi trng hp bn cn tm
cch chng thc c site t xa. Hin ti bn c th c mt s cch:
- Bn kim tra remote site thng qua s dng a ch IP hoc tn host.
- Vi ngi dng t xa, bn c th s dng kim tra ngi dng da vo CSDL, yu cu username
v password trc khi cho php truy cp ti phng thc
- L tng nht l bn m ha kt ni gia Client/Server s dng SSL. Nhng khi bn phi s
dng cc hm Curl ca PHP gi v nhn d liu.
+ Lm th no iu khin c cc cuc tn cng t chi dch v (denial of service): iu g
s xy ra khi mt client lm lt server bng cc request v ngn cn server phn hi clien khc.
Nguyn Tn Thnh
Dang Quang
Thnh c th tm tt dm mnh "C ch truyn tham s trong RMI" v "RMI Registry trong mt ng dng RMI"
c khng?hix, phn ny kh nhai vi
Mn Thng
Unice Lockon
Nguyn Tn Thnh
Nguyn Tn Thnh
mo hinh rmiregistry.PNG
14 KB Xem Ti xung
Nguyn Tn Thnh
mo hinh rmi.PNG
13 KB Xem Ti xung
Nguyn Tn Thnh
rmi va web.PNG
9 KB Xem Ti xung
Le Van Thuong
1.
Le Van Thuong
1.
InetAddress
Lp ny ng gi a ch ca mt
Socket (a ch IP + port). N c th
l mt cp (hostname + port) trong
trng hp mun kt ni ti
hostname.
N cung cp mt i tng khng
th thay i c s dng cho mt
socket to lin kt, kt ni hoc
tr v gi tr.
N cng c chc nng ca lp
InetAddress l gi gn a ch IP v phn
gii tn ngc (t tn sang IP-t IP
sang tn).
Bao gm cc constructor sau:
InetSocketAddress(InetAddress a
ddr, int port)
InetSocketAddress(int port)
InetSocketAddress(String hostna
me, int port)
Le Van Thuong
Giao tip vi i tng t xa: Thng tin chi tit ca vic lin lc gia cc i tng
t xa c x l bi RMI.
Le Van Thuong
Triu gi hm cc b :
trn
cng
mt
my
Java
(JVM).
Nguyn Tn Thnh
Le Van Thuong
RMI - Difference between RPC and RMI - Jan 30, 2009 at 18:10 pm by Vidya Sagar
Implements object to object implementation among different java objects to implement distributed
communication model.
- RMI passes the objects as parameters to remote methods.
- RMI invokes the remote methods from the objects
RPC:
RMI - Difference between RPC and RMI - Aug 16, 2009 at 13:10 pm by Amit Satpute
Le Van Thuong
Khng cn to b ng k khc, dng k thut Factory Service, 1 i tng c th sn sinh ra nhiu i tng.
Khi th ch cn 1 port, ta c th sn sinh ra v sinh i tng, rt d trong ci t v qun l. Ai cn thng tin
v Factory Service, lin h mnh. Ci ny ch l tm hiu thm, khng c thi.
2011/1/7 ?inh Ch? Th?nh <tanthanh...@gmail.com>
- hin th ni dung trch dn -
------------------------------------------------------------------------L Vn Thng
Trng i hc Cng ngh Thng Tin
Khoa Mng my tnh v truyn thng
Lp MMT03
T: 0979150964
Y!M: ami90s
E-mail: ami...@gmail.com
Le Van Thuong
Trong ti liu ting Anh c on " RPC (Remote Procedure Call) is the
predecessor of RMI (Remote Method Invocation)" dch l RPC l tin thn ca RMI. Mnh s dch thm. Gi mi
cm nhn, ting Anh quan trng n mc no. Hzaiii
Nguyn Tn Thnh
Vi cu hi: trnh by hiu bit v RMI theo mnh cn phi tr li nhiu lm:
1. tng ca RMI
2. C ch hot ng
3. Lp stub and skel
4. B ng k rmi registry
5. Np lp stub t xa
6. Truyn tham tr v tham chiu
7. Cc k thut nng cao: to b ng k ring, to chng trnh sn xut i tng, vn tng la v RMI
Qu nhiu...gn nh ht RMI lun, v ngi ta hi mnh hiu bit v RMI!
Le Van Thuong
Cn theo mnh ngh ch l nhng hiu bit bnh thng v RMI thi. Ch trong khun kh 1 cu, lm ht tt c
nhng hiu bit nh vy, phi lm mt bi bo co mi ht . Nn theo mnh, ch nu s lc thi. Chc vy .
:)
2011/1/7 ?inh Ch? Th?nh <tanthanh...@gmail.com>
Vi cu hi: trnh by hiu bit v RMI theo mnh cn phi tr li nhiu lm:
- hin th ni dung trch dn -
Nguyn Tn Thnh
Gi bun ng qu, i ng y. Mai cc bn ln y n bi tip nhen. Mnh thy hnh thc n bi ny rt hiu
qu. Gi thay v nh spam by b nh Thm Hi th mnh chuyn sang spam c tri thc i, xem ai post c
nhiu kin thc ln y nht. Nh l va post mi v phi va tr li bi post ca bn mnh na !
Anh em tp trung t k lc 500 bi post t y n ti mai coi.
Cc bn trong nhm qun tr qun l ph nhen, ai post bi by, ni dung khng r rng, khng c du ting vit
th delete ht!
3 bi vit b xa.
Le Van Thuong
, qun 1 chuyn. Thy ni l phi vit xc tch, khng qu 2 trang giy. Mnh vit di qu, cc bn lc bt
chnh i nha. Mnh cng lc y, v t ci hnh vo cho n "xc tch". Mi thy mi , liu cm m gp mm thi. :
(
2011/1/7 ?inh Ch? Th?nh <tanthanh...@gmail.com>
Th ng ri, ti khng c sch v no i cht c, trong lc lm vic thy th no ni th y !
------------------------------------------------------------------------L Vn Thng
Trng i hc Cng ngh Thng Tin
Khoa Mng my tnh v truyn thng
Lp MMT03
T: 0979150964
Y!M: ami90s
E-mail: ami...@gmail.com
Thang Nguyen