首页 > PHP > 转XML-RPC范例
2015
07-24

转XML-RPC范例

xmlrpc_client.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<html>
<head>
<title>XML-RPC PHP Demo</title>
</head>
<body>
<h1>XML-RPC PHP Demo</h1>
<?php
include './xmlrpc.inc';
// Make an object to represent our server.
$server = new xmlrpc_client('/xmlrpc_server.php', 'www.test.com', 80);
 
// Send a message to the server.
$message = new xmlrpcmsg('sample.sumAndDifference',
array(new xmlrpcval(5, 'int'),
new xmlrpcval(3, 'int')));
$result = $server->send($message);
 
// Process the response.
if (!$result) {
print "<p>Could not connect to HTTP server.</p>";
} elseif ($result->faultCode()) {
print "<p>XML-RPC Fault #" . $result->faultCode() . ": " .
$result->faultString();
} else {
$struct = $result->value();
$sumval = $struct->structmem('sum');
$sum = $sumval->scalarval();
$differenceval = $struct->structmem('difference');
$difference = $differenceval->scalarval();
print "<p>Sum: " . htmlentities($sum) .
", Difference: " . htmlentities($difference) . "</p>";
}
?>
</body></html>

xmlrpc_server.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?php
include './xmlrpc.inc';
include './xmlrpcs.inc';
 
function sumAndDifference ($params) {
// Parse our parameters.
$xval = $params->getParam(0);
$x = $xval->scalarval();
$yval = $params->getParam(1);
$y = $yval->scalarval();
 
// Build our response.
$struct = array('sum' => new xmlrpcval($x + $y, 'int'),
'difference' => new xmlrpcval($x - $y, 'int'));
return new xmlrpcresp(new xmlrpcval($struct, 'struct'));
}
 
// Declare our signature and provide some documentation.
// (The PHP server supports remote introspection. Nifty!)
$sumAndDifference_sig = array(array('struct', 'int', 'int'));
$sumAndDifference_doc = 'Add and subtract two numbers';
 
new xmlrpc_server(array('sample.sumAndDifference' =>
array('function' => 'sumAndDifference',
'signature' => $sumAndDifference_sig,
'docstring' => $sumAndDifference_doc)));
?>
最后编辑:
作者:saunix
大型互联网公司linux系统运维攻城狮,专门担当消防员

留下一个回复