`

XML-RPC for PHP简介及使用

    博客分类:
  • PHP
阅读更多

 

一.XML-RPC是什么?
XML-RPCUserland Software公司设计的一种格式:是一种使用HTTP协议传输XML格式文件来获取远程程序调用(Remote Procedure Call)的传输方式。官方网站是www.xmlrpc.com

http://phpxmlrpc.sourceforge.net/上面有个PHP XML-RPC的框架(类集合)用于使用PHP语言来写XML-RPC客户端和服务端。现在的稳定发行版本是2.2,下载地址是http://sourceforge.net/projects/phpxmlrpc/files/phpxmlrpc/2.2.2/xmlrpc-2.2.2.tar.gz/download

另外你在www.xmlrpc.com上面也可以找到其他s语言的XML-RPC列表,例如PerlPython

这篇文章将介绍XML-RPC for PHP的类库使用。

二.系统运行要求

该类库的设计目标是可扩展性和向后兼容性。因此,它支持大部分的现有PHP版本。最低需要的PHP版本是4.2。该类库提供一个兼容层来支持PHP 4.0.54.1。当然官方建议是使用PHP 5.0或者更高版本。

注意,如果你要用SSL或者HTTP 1.1跟远程服务器通信,必须把“CURL”扩展编译进PHP中。

另外,PHP自带的原生“xmlrpc”扩展跟这里介绍的XML-RPC类库不是同一个东西,所以并不需要将其编译进PHP中。

三.相关类介绍

1.xmlrpcval

XML-RPC中有六种基本类型和两种复合类型,基本类型是:intbooleanstringdoubledateTime.iso8601base6,复合类型是:arraystructXmlrpcval的作用就是把PHP中类型转换成这几种类型放入XML-RPC中,以便传输。

new xmlrpcval(123,"int");   //将123作为XML-RPC int类型
new xmlrpcval(123,"string"); //将123作为XML-RPC string类型
new xmlrpcval(123);//如果没有第二个参数,系统默认将其作为string类型

new xmlrpcval( //XML-RPC array类型
array(
new xmlrpcval("string1"),
new xmlrpcval("string2"),
new xmlrpcval("string3"),),"array");

new xmlrpcval( //XML-RPC struct类型
array( "name"=>new xmlrpcval("codebean","string"),
"age"=>new xmlrpcval(34,"int"),
"address"=>new xmlrpcval(
array( "street"=>new xmlrpcva("xiaoyinxilu","string"), "city"=>new xmlrpcval("beijing","string") ),
"struct")),
"struct");

 

相关方法:
kindOf():返回该对象的基本类型:"struct","array","scalar"

scalarVal():如果$val->kindOf()=="scalar",则直接返回改对象的PHP对应的值。
arrayMen(int $n):如果$val->kindOf()=="array",返回$val中第n个的值。
arraySize():如果$val->kindOf()=="array",返回$val的元素个数。

2.xmlrpcmsg
这个类提供一个向XML-RPC服务器发送请求的对象,客户端发送xmlrpcmsg到服务器,服务器返回一个xmlrpcresp

//请求服务器的examples.getStateName方法,参数是123
$message = new xmlrpcmsg("examples.getStateName",array(new xmlrpcval(123,"int")));

 

相关函数:
getNumParams():获取xmlrpcmsg对象的参数的总数。
getParam(int $n):获取xmlrpcmsg对象的第n个数的值。
3.xmlrpc_client
客户端的基本类。

 

 

//在服务端www.test.com上面的interface.php路径建立相关链接。    
$client = nw xmlrpc_client("/interface.php","ww.test.com",80);

 

相关方法:
send($message,30):向服务端发送$message,超时时间是30秒。
setDebug(int $level):设置是否输出调试信息,默认是0即不输出调试信息。$level1,打印服务端的HTTP头信息及XML信息。$level2,同时打印服务端和客户端的HTTP头信息及XML信息。
4.xmlrpcresp
该类主要包含XML-RPC请求返回的结果。Xmlrpc_clientsend方法返回该类型。

new  xmlrpcresp(xmlrpcval $val);//在服务端生成一个xmlrpcresp对象
new xmlrpcresp(0,int $errcode,string $err_string);//服务端出错时,可以返回该类型。

相关方法:
faultCode():客户端获取服务端返回的出错代码。
faultString():客户端获取服务端返回的出错信息。
Value():客户端获取服务端返回的值。
5.xmlrpc_server
服务端的基本类。

function test($xmlrpcval){
return new xmlrpcresp($val);
}

//客户端可以访问examples.myTest来实际访问test()函数
new xmlrpc_server(
array(
"examples.myTest"=>array("function"=>"test")
));

四,一个实际例子
假设服务度是my.rpcserver.com,提供服务的路径是interface.php,客户端是my.test.com.

在服务度的interface.php中:

<?php
include './lib/xmlrpc.inc';
include './lib/xmlrpcs.inc';

function foo($xmlrpcmsg){
$par1 = $xmlrpcmsg->getParam(0); //获取第一个参数
$val1 = $par1->scalarval(); //转换成PHP对应的值

$par2 = $xmlrpcmsg->getParam(1); //获取第二个参数
$val2 = $par2->scalarval(); //转换成PHP对应的值

$par3 = $xmlrpcmsg->getParam(2); //获取第二个参数

//转换成PHP对应的值

for($i=0; $i<$par3->arraySize(); $i++){
$v = $par3->arrayMem($i);
$val[] = $v->scalarVal()."<br>";
}

$msg1 = new xmlrpcval(strrev($val1),"string");
$msg2 = new xmlrpcval(strrev($val2),"int");

$msg = new xmlrpcval(array($msg1,$msg2),"array"); //返回一个array

return new xmlrpcresp($msg);
}

new xmlrpc_server(
array(
"example.test"=>array("function"=>"foo"),
)
);
?>

 

在客户端的client.php中:

<?php 
include './lib/xmlrpc.inc';
$params = array(
new xmlrpcval("hello rpc","string"),
new xmlrpcval(123,"int"),
new xmlrpcval(
array(
new xmlrpcval("test","string"),
new xmlrpcval(456,"int")
)
,
"array"),
);

$message = new xmlrpcmsg("example.test",$params);
$client = new xmlrpc_client("/interface.php", "my.rpcserver.com" ,'80');
//$client->setDebug(2);
$res = $client->send($message,30);

if(!$res->faultCode()){
$v= $res->value();
for($i=0; $i<$v->arraySize(); $i++){
$vv = $v->arrayMem($i);
echo $vv->scalarVal()."<br>";
}
}
else{
echo $res->faultcode().":".$res->faultString()."<br>";
}
?>

 


分享到:
评论

相关推荐

    xml-rpc 1.5.1

    The automatic base64 encoding can be turned on via the new XML_RPC_Client::setAutoBase64() method. The auto-encoding is a workaround for systems that don&apos;t have PHP&apos;s mbstring extension ...

    XML-RPC for PHP-开源

    XML-RPC Web RPC协议PHP实现。 额外的模块提供对JSON和JSONRPC协议的支持。 该库的javascript版本也可用。

    整合PHP和XML

    Learn how to use SAX, XSLT, and XPath to manipulate XML documents, as well as use of XML-RPC protocol for accessing procedures on a remote computer, and much more

    phpxmlrpc-3.0.0 程序库

    It is a library implementing the XML-RPC protocol, written in PHP. It is also known as PHPXMLRPC. It is designed for ease of use, flexibility and completeness. High speed and reduced memory footprint ...

    PHP Web 2.0 Mashup Projects.pdf

    we create an extensible object-oriented parser for XML. The mashup covered in this chapter integrates information taken from Amazon's E-commerce Service (ECS) with the Internet UPC database. In ...

    PHPweb服务器YACS.zip

    YACS 是一个强大的 PHP 脚本,可以让你维护一个动态的 Web 服务器。... RSS syndication- Easy installation- XML-RPC interface (implementing the Blogger API and metaWeblog API) 标签:YACS

    XML Processing with Perl, Python, and PHP (2002).pdf

    the way, I address some of the important protocols, such as SOAP and XML-RPC, that make seamless data transfer possible. Throughout this book you’ll find sample scripts. You can download the complete...

    php手册.chm,php手册

    XML-RPC 函数 CLVIII. XMLReader functions CLIX. XSL functions CLX. XSLT Functions CLXI. YAZ Functions CLXII. YP/NIS Functions CLXIII. Zip File Functions (Read Only Access) CLXIV. Zlib Compression ...

    php帮助文档,php。chm,php必备的中文手册

    XML-RPC 函数 CLXIV. XMLReader functions CLXV. XSL functions CLXVI. XSLT Functions CLXVII. YAZ Functions CLXVIII. YP/NIS Functions CLXIX. Zip File Functions (Read Only Access) CLXX. Zlib Compression ...

    PHP5 完整官方 中文教程

    XML-RPC — XML-RPC 函数 XMLReader — XMLReader functions XMLWriter — XMLWriter Functions XSL — XSL functions XSLT — XSLT Functions YAZ — YAZ Functions YP/NIS — YP/NIS Functions Zip — Zip File ...

    php.ini-development

    should be disabled, as enabling it may result in issues when generating XML ; documents, however this remains supported for backward compatibility reasons. ; Note that this directive does not control...

    PHP.Web.Services.APIs.for.the.Modern.Web.2nd.Edition

    this practical book provides everything you need to build web service APIs with PHP. Author Lorna Jane Mitchell uses code samples, real-world examples, and advice based on her extensive experience to...

    PHP5中文参考手册

    XML-RPC — XML-RPC 函数 XMLReader — XMLReader functions XMLWriter — XMLWriter Functions XSL — XSL functions XSLT — XSLT Functions YAZ — YAZ Functions YP/NIS — YP/NIS Functions Zip — Zip File ...

    中文版PHP使用手册

    XML-RPC 函数 CLVIII. XMLReader functions CLIX. XSL functions CLX. XSLT Functions CLXI. YAZ Functions CLXII. YP/NIS Functions CLXIII. Zip File Functions (Read Only Access) CLXIV. Zlib Compression ...

    php中文完全开发手册

    XML-RPC 函数库 CXVIII. xdiff Functions CXIX. XSLT Functions CXX. YAZ Functions CXXI. YP/NIS Functions CXXII. Zip File Functions (Read Only Access) CXXIII. Zlib Compression Functions VI. Zend API 24. ...

    PHP手册(带评论版-2008-03-14).part2.rar

    XML-RPC 函数 CLVIII. XMLReader functions CLIX. XSL functions CLX. XSLT Functions CLXI. YAZ Functions CLXII. YP/NIS Functions CLXIII. Zip File Functions (Read Only Access) CLXIV. Zlib Compression ...

    PHP手册(带评论版-2008-03-14).part1.rar

    XML-RPC 函数 CLVIII. XMLReader functions CLIX. XSL functions CLX. XSLT Functions CLXI. YAZ Functions CLXII. YP/NIS Functions CLXIII. Zip File Functions (Read Only Access) CLXIV. Zlib Compression ...

    PHP函数参考手册大全

    XML-RPC 函数 CLVIII. XMLReader functions CLIX. XSL functions CLX. XSLT Functions CLXI. YAZ Functions CLXII. YP/NIS Functions CLXIII. Zip File Functions (Read Only Access) CLXIV. Zlib Compression ...

    php手册PHP5研究室编无乱码版本chm

    XML-RPC 函数 CLVIII. XMLReader functions CLIX. XSL functions CLX. XSLT Functions CLXI. YAZ Functions CLXII. YP/NIS Functions CLXIII. Zip File Functions (Read Only Access) CLXIV. Zlib ...

    PHP手册2007整合中文版

    XML-RPC 函数 CLXXXII. XMLReader functions CLXXXIII. XMLWriter Functions CLXXXIV. XSL functions CLXXXV. XSLT Functions CLXXXVI. YAZ Functions CLXXXVII. YP/NIS Functions CLXXXVIII. Zip File Functions ...

Global site tag (gtag.js) - Google Analytics