4.1 在PHP中应用XML_RPC |
4.2 客户端 |
<?php $f=new xmlrpcmsg(@#examples.getStateName@#,array(new xmlrpcval(14, "int"))); $c=new xmlrpc_client("/RPC2", "betty.userland.com", 80); $r=$c->send($f); $v=$r->value(); if (!$r->faultCode()) { print "状态代码". $HTTP_POST_VARS["stateno"] . "是" . $v->scalarval() . "<BR>"; print "<HR>这是服务器的应答<BR><PRE>" . htmlentities($r->serialize()). "</PRE><HR>\n"; } else { print "错误: "; print "代码: " . $r->faultCode() . " 原因: @#" .$r->faultString()."@#<BR>"; } ?> |
$client=new xmlrpc_client($server_path, $server_hostname, $server_port); |
$response=$client->send($xmlrpc_message); |
$msg=new xmlrpcmsg($methodName, $parameterArray); |
$msg=new xmlrpcmsg("examples.getStateName", array(new xmlrpcval(23, "int"))); |
<?php $myVal=new xmlrpcval($stringVal); $myVal=new xmlrpcval($scalarVal, "int" | "boolean" | "string" | "double" | "dateTime.iso8601" | "base64"); $myVal=new xmlrpcval($arrayVal, "array" | "struct"); ?> |
<?php $myArray=new xmlrpcval(array(new xmlrpcval("Tom"), new xmlrpcval("Dick"),new xmlrpcval("Harry")), "array"); $myStruct=new xmlrpcval(array( "name" => new xmlrpcval("Tom"), "age" => new xmlrpcval(34, "int"), "geek" => new xmlrpcval(1, "boolean")),"struct"); ?> |
$resp=new xmlrpcresp($xmlrpcval); |
$xmlrpcVal=$resp->value(); |
$scalarVal=$val->scalarval(); |
$arr=xmlrpc_decode($xmlrpc_val); |
$xmlrpc_val=xmlrpc_encode($phpval); |
4.3 服务器端 |
<?php $s=new xmlrpc_server( array("examples.myFunc" => array("function" => "foo"))); ?> |
<?php function foo ($params) { global $xmlrpcerruser; // 引入用户错误代码值 // $params是一个xmlrpcval对象的数组 if ($err) { // 错误条件 return new xmlrpcresp(0, $xmlrpcerruser+1, // 用户错误1 "Error!"); } else { // 成功 return new xmlrpcresp(new xmlrpcval("Fine!", "string")); } } ?> |
<?php include("xmlrpc.inc"); include("xmlrpcs.inc"); function foo ($params) { global $xmlrpcerruser; // 引入用户错误代码值 // $params是xmlrpcval对象的一个数组 $vala=$params->params[0]; $sval=$vala->scalarval(); $ret=$sval*2; return new xmlrpcresp(new xmlrpcval($ret, "int")); } $s=new xmlrpc_server( array("product" => array("function" => "foo"))); ?> |
<?php include("xmlrpc.inc"); if ($HTTP_POST_VARS["number"]!="") { $f=new xmlrpcmsg(@#product@#,array(new xmlrpcval($HTTP_POST_VARS["number"], "int"))); $c=new xmlrpc_client("/xmlrpc/servfoo.php", "luigi.melpomenia.com.ar", 80); $c->setDebug(0); $r=$c->send($f); $v=$r->value(); if (!$r->faultCode()) { print "Number ". $HTTP_POST_VARS["number"] . " is " . $v->scalarval() . "<BR>"; print "<HR>来自服务器的结果!<BR><PRE>" . htmlentities($r->serialize()). "</PRE><HR>\n"; } else { print "操作失败: "; print "代码: " . $r->faultCode() . " 原因: @#" .$r->faultString()."@#<BR>"; } } print "<FORM METHOD=\"POST\"> <INPUT NAME=\"number\" VALUE=\"${number}\"> <input type=\"submit\" value=\"go\" name=\"submit\"></FORM><P> 输入一个数值"; ?> |