首頁 >php教程 >php手册 >XML-RPC for PHP简介及使用

XML-RPC for PHP简介及使用

不言
不言原創
2018-05-26 14:15:063220瀏覽

一.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中,以便传输。


<span style="color: #0000ff;">new</span><span style="color: #000000;"> xmlrpcval(</span><span style="color: #000000;">123</span><span style="color: #000000;">,</span><span style="color: #000000;">"</span><span style="color: #000000;">int</span><span style="color: #000000;">"</span><span style="color: #000000;">);   </span><span style="color: #008000;">//</span><span style="color: #008000;">将123作为XML-RPC int类型</span><span style="color: #008000;"><br/></span><span style="color: #0000ff;">new</span><span style="color: #000000;"> xmlrpcval(</span><span style="color: #000000;">123</span><span style="color: #000000;">,</span><span style="color: #000000;">"</span><span style="color: #000000;">string</span><span style="color: #000000;">"</span><span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;">将123作为XML-RPC string类型</span><span style="color: #008000;"><br/></span><span style="color: #0000ff;">new</span><span style="color: #000000;"> xmlrpcval(</span><span style="color: #000000;">123</span><span style="color: #000000;">);</span><span style="color: #008000;">//</span><span style="color: #008000;">如果没有第二个参数,系统默认将其作为string类型</span><span style="color: #008000;"><br/></span><span style="color: #000000;"><br/></span><span style="color: #0000ff;">new</span><span style="color: #000000;"> xmlrpcval(    </span><span style="color: #008000;">//</span><span style="color: #008000;">XML-RPC array类型   </span><span style="color: #008000;"><br/></span><span style="color: #0000ff;">array</span><span style="color: #000000;">(<br/>       </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> xmlrpcval(</span><span style="color: #000000;">"</span><span style="color: #000000;">string1</span><span style="color: #000000;">"</span><span style="color: #000000;">)</span><span style="color: #000000;">,</span><span style="color: #000000;">    <br/>       </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> xmlrpcval(</span><span style="color: #000000;">"</span><span style="color: #000000;">string2</span><span style="color: #000000;">"</span><span style="color: #000000;">)</span><span style="color: #000000;">,</span><span style="color: #000000;"><br/>       </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> xmlrpcval(</span><span style="color: #000000;">"</span><span style="color: #000000;">string3</span><span style="color: #000000;">"</span><span style="color: #000000;">)</span><span style="color: #000000;">,</span><span style="color: #000000;">)</span><span style="color: #000000;">,</span><span style="color: #000000;">"</span><span style="color: #000000;">array</span><span style="color: #000000;">"</span><span style="color: #000000;">);<br/> <br/></span><span style="color: #0000ff;">new</span><span style="color: #000000;"> xmlrpcval(    </span><span style="color: #008000;">//</span><span style="color: #008000;">XML-RPC struct类型        </span><span style="color: #008000;"><br/></span><span style="color: #0000ff;">array</span><span style="color: #000000;">(    </span><span style="color: #000000;">"</span><span style="color: #000000;">name</span><span style="color: #000000;">"</span><span style="color: #000000;">=></span><span style="color: #0000ff;">new</span><span style="color: #000000;"> xmlrpcval(</span><span style="color: #000000;">"</span><span style="color: #000000;">codebean</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">"</span><span style="color: #000000;">string</span><span style="color: #000000;">"</span><span style="color: #000000;">)</span><span style="color: #000000;">,</span><span style="color: #000000;"><br/>    </span><span style="color: #000000;">"</span><span style="color: #000000;">age</span><span style="color: #000000;">"</span><span style="color: #000000;">=></span><span style="color: #0000ff;">new</span><span style="color: #000000;"> xmlrpcval(</span><span style="color: #000000;">34</span><span style="color: #000000;">,</span><span style="color: #000000;">"</span><span style="color: #000000;">int</span><span style="color: #000000;">"</span><span style="color: #000000;">)</span><span style="color: #000000;">,</span><span style="color: #000000;"><br/>                </span><span style="color: #000000;">"</span><span style="color: #000000;">address</span><span style="color: #000000;">"</span><span style="color: #000000;">=></span><span style="color: #0000ff;">new</span><span style="color: #000000;"> xmlrpcval(        <br/>                           </span><span style="color: #0000ff;">array</span><span style="color: #000000;">(                          </span><span style="color: #000000;">"</span><span style="color: #000000;">street</span><span style="color: #000000;">"</span><span style="color: #000000;">=></span><span style="color: #0000ff;">new</span><span style="color: #000000;"> xmlrpcva(</span><span style="color: #000000;">"</span><span style="color: #000000;">xiaoyinxilu</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">"</span><span style="color: #000000;">string</span><span style="color: #000000;">"</span><span style="color: #000000;">)</span><span style="color: #000000;">,</span><span style="color: #000000;">"</span><span style="color: #000000;">city</span><span style="color: #000000;">"</span><span style="color: #000000;">=></span><span style="color: #0000ff;">new</span><span style="color: #000000;"> xmlrpcval(</span><span style="color: #000000;">"</span><span style="color: #000000;">beijing</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">"</span><span style="color: #000000;">string</span><span style="color: #000000;">"</span><span style="color: #000000;">)        )</span><span style="color: #000000;">,</span><span style="color: #000000;">        <br/>                          </span><span style="color: #000000;">"</span><span style="color: #000000;">struct</span><span style="color: #000000;">"</span><span style="color: #000000;">))</span><span style="color: #000000;">,</span><span style="color: #000000;"><br/></span><span style="color: #000000;">"</span><span style="color: #000000;">struct</span><span style="color: #000000;">"</span><span style="color: #000000;">);</span>

相关方法:
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

<span style="color: #008000;">//</span><span style="color: #008000;">请求服务器的examples.getStateName方法,参数是123</span><span style="color: #008000;"><br></span><span style="color: #800080;">$message</span><span style="color: #000000;">=</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> xmlrpcmsg(</span><span style="color: #000000;">"</span><span style="color: #000000;">examples.getStateName</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #0000ff;">array</span><span style="color: #000000;">(</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> xmlrpcval(</span><span style="color: #000000;">123</span><span style="color: #000000;">,</span><span style="color: #000000;">"</span><span style="color: #000000;">int</span><span style="color: #000000;">"</span><span style="color: #000000;">)));</span>

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

<span style="color: #008000;">//</span><span style="color: #008000;">在服务端www.test.com上面的interface.php路径建立相关链接。    </span><span style="color: #008000;"><br></span><span style="color: #800080;">$client</span><span style="color: #000000;">=</span><span style="color: #000000;"> nw xmlrpc_client(</span><span style="color: #000000;">"</span><span style="color: #000000;">/interface.php</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">"</span><span style="color: #000000;">ww.test.com</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">80</span><span style="color: #000000;">);</span>

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

<span style="color: #0000ff;">new</span><span style="color: #000000;">  xmlrpcresp(xmlrpcval </span><span style="color: #800080;">$val</span><span style="color: #000000;">);</span><span style="color: #008000;">//</span><span style="color: #008000;">在服务端生成一个xmlrpcresp对象</span><span style="color: #008000;"><br></span><span style="color: #0000ff;">new</span><span style="color: #000000;"> xmlrpcresp(</span><span style="color: #000000;">0</span><span style="color: #000000;">,</span><span style="color: #000000;">int </span><span style="color: #800080;">$errcode</span><span style="color: #000000;">,</span><span style="color: #0000ff;">string</span><span style="color: #800080;">$err_string</span><span style="color: #000000;">);</span><span style="color: #008000;">//</span><span style="color: #008000;">服务端出错时,可以返回该类型。</span>

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

<span style="color: #0000ff;">function</span><span style="color: #000000;"> test(</span><span style="color: #800080;">$xmlrpcval</span><span style="color: #000000;">){<br>  </span><span style="color: #0000ff;">return</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> xmlrpcresp(</span><span style="color: #800080;">$val</span><span style="color: #000000;">);<br>}<br><br></span><span style="color: #008000;">//</span><span style="color: #008000;">客户端可以访问examples.myTest来实际访问test()函数     </span><span style="color: #008000;"><br></span><span style="color: #0000ff;">new</span><span style="color: #000000;"> xmlrpc_server(        <br>  </span><span style="color: #0000ff;">array</span><span style="color: #000000;">(            <br>     </span><span style="color: #000000;">"</span><span style="color: #000000;">examples.myTest</span><span style="color: #000000;">"</span><span style="color: #000000;">=></span><span style="color: #0000ff;">array</span><span style="color: #000000;">(</span><span style="color: #000000;">"</span><span style="color: #000000;">function</span><span style="color: #000000;">"</span><span style="color: #000000;">=></span><span style="color: #000000;">"</span><span style="color: #000000;">test</span><span style="color: #000000;">"</span><span style="color: #000000;">)    <br>));</span>

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

在服务度的interface.php中:

<span style="color: #000000;">468c2b77e4fef2805dfec2efe841102a</span><span style="color: #000000;">getParam(</span><span style="color: #000000;">0</span><span style="color: #000000;">);    </span><span style="color: #008000;">//</span><span style="color: #008000;">获取第一个参数</span><span style="color: #008000;"><br></span><span style="color: #800080;">$val1</span><span style="color: #000000;">=</span><span style="color: #800080;">$par1</span><span style="color: #000000;">-></span><span style="color: #000000;">scalarval();            </span><span style="color: #008000;">//</span><span style="color: #008000;">转换成PHP对应的值</span><span style="color: #008000;"><br></span><span style="color: #000000;">    <br>    </span><span style="color: #800080;">$par2</span><span style="color: #000000;">=</span><span style="color: #800080;">$xmlrpcmsg</span><span style="color: #000000;">-></span><span style="color: #000000;">getParam(</span><span style="color: #000000;">1</span><span style="color: #000000;">);    </span><span style="color: #008000;">//</span><span style="color: #008000;">获取第二个参数</span><span style="color: #008000;"><br></span><span style="color: #800080;">$val2</span><span style="color: #000000;">=</span><span style="color: #800080;">$par2</span><span style="color: #000000;">-></span><span style="color: #000000;">scalarval();            </span><span style="color: #008000;">//</span><span style="color: #008000;">转换成PHP对应的值</span><span style="color: #008000;"><br></span><span style="color: #000000;">    <br>    </span><span style="color: #800080;">$par3</span><span style="color: #000000;">=</span><span style="color: #800080;">$xmlrpcmsg</span><span style="color: #000000;">-></span><span style="color: #000000;">getParam(</span><span style="color: #000000;">2</span><span style="color: #000000;">);    </span><span style="color: #008000;">//</span><span style="color: #008000;">获取第二个参数<br><br>    //转换成PHP对应的值</span><span style="color: #008000;"><br></span><span style="color: #0000ff;">for</span><span style="color: #000000;">(</span><span style="color: #800080;">$i</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">; </span><span style="color: #800080;">$i</span><span style="color: #000000;">7b9475fa9466ce3353fd4c3c9f56edce</span><span style="color: #000000;">arraySize(); </span><span style="color: #800080;">$i</span><span style="color: #000000;">++</span><span style="color: #000000;">){<br>        </span><span style="color: #800080;">$v</span><span style="color: #000000;">=</span><span style="color: #800080;">$par3</span><span style="color: #000000;">-></span><span style="color: #000000;">arrayMem(</span><span style="color: #800080;">$i</span><span style="color: #000000;">);<br>        </span><span style="color: #800080;">$val</span><span style="color: #000000;">[] </span><span style="color: #000000;">=</span><span style="color: #800080;">$v</span><span style="color: #000000;">-></span><span style="color: #000000;">scalarVal()</span><span style="color: #000000;">.</span><span style="color: #000000;">"</span><span style="color: #000000;"><br></span><span style="color: #000000;">"</span><span style="color: #000000;">;<br>    }<br>    <br>    </span><span style="color: #800080;">$msg1</span><span style="color: #000000;">=</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> xmlrpcval(</span><span style="color: #008080;">strrev</span><span style="color: #000000;">(</span><span style="color: #800080;">$val1</span><span style="color: #000000;">)</span><span style="color: #000000;">,</span><span style="color: #000000;">"</span><span style="color: #000000;">string</span><span style="color: #000000;">"</span><span style="color: #000000;">);<br>    </span><span style="color: #800080;">$msg2</span><span style="color: #000000;">=</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> xmlrpcval(</span><span style="color: #008080;">strrev</span><span style="color: #000000;">(</span><span style="color: #800080;">$val2</span><span style="color: #000000;">)</span><span style="color: #000000;">,</span><span style="color: #000000;">"</span><span style="color: #000000;">int</span><span style="color: #000000;">"</span><span style="color: #000000;">);<br><br>    </span><span style="color: #800080;">$msg</span><span style="color: #000000;">=</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> xmlrpcval(</span><span style="color: #0000ff;">array</span><span style="color: #000000;">(</span><span style="color: #800080;">$msg1</span><span style="color: #000000;">,</span><span style="color: #800080;">$msg2</span><span style="color: #000000;">)</span><span style="color: #000000;">,</span><span style="color: #000000;">"</span><span style="color: #000000;">array</span><span style="color: #000000;">"</span><span style="color: #000000;">);        </span><span style="color: #008000;">//</span><span style="color: #008000;">返回一个array</span><span style="color: #008000;"><br></span><span style="color: #000000;">    <br>    </span><span style="color: #0000ff;">return</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> xmlrpcresp(</span><span style="color: #800080;">$msg</span><span style="color: #000000;">);<br>}<br><br></span><span style="color: #0000ff;">new</span><span style="color: #000000;"> xmlrpc_server(<br>    </span><span style="color: #0000ff;">array</span><span style="color: #000000;">(<br>        </span><span style="color: #000000;">"</span><span style="color: #000000;">example.test</span><span style="color: #000000;">"</span><span style="color: #000000;">=></span><span style="color: #0000ff;">array</span><span style="color: #000000;">(</span><span style="color: #000000;">"</span><span style="color: #000000;">function</span><span style="color: #000000;">"</span><span style="color: #000000;">=></span><span style="color: #000000;">"</span><span style="color: #000000;">foo</span><span style="color: #000000;">"</span><span style="color: #000000;">)</span><span style="color: #000000;">,</span><span style="color: #000000;"><br>    )<br>);<br></span><span style="color: #000000;">?></span>

在客户端的client.php中:

<span style="color: #000000;">a73def7c7a6a1924a3e26fb6c897e693setDebug(2);</span><span style="color: #008000;"><br></span><span style="color: #800080;">$res</span><span style="color: #000000;">=</span><span style="color: #800080;">$client</span><span style="color: #000000;">-></span><span style="color: #000000;">send(</span><span style="color: #800080;">$message</span><span style="color: #000000;">,</span><span style="color: #000000;">30</span><span style="color: #000000;">);<br><br></span><span style="color: #0000ff;">if</span><span style="color: #000000;">(</span><span style="color: #000000;">!</span><span style="color: #800080;">$res</span><span style="color: #000000;">-></span><span style="color: #000000;">faultCode()){<br>    </span><span style="color: #800080;">$v</span><span style="color: #000000;">=</span><span style="color: #800080;">$res</span><span style="color: #000000;">-></span><span style="color: #000000;">value();<br>    </span><span style="color: #0000ff;">for</span><span style="color: #000000;">(</span><span style="color: #800080;">$i</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">; </span><span style="color: #800080;">$i</span><span style="color: #000000;">c5b5e5eb7dc1a4e26f3d7a5ce5a90a28</span><span style="color: #000000;">arraySize(); </span><span style="color: #800080;">$i</span><span style="color: #000000;">++</span><span style="color: #000000;">){<br>        </span><span style="color: #800080;">$vv</span><span style="color: #000000;">=</span><span style="color: #800080;">$v</span><span style="color: #000000;">-></span><span style="color: #000000;">arrayMem(</span><span style="color: #800080;">$i</span><span style="color: #000000;">);<br>        </span><span style="color: #0000ff;">echo</span><span style="color: #800080;">$vv</span><span style="color: #000000;">-></span><span style="color: #000000;">scalarVal()</span><span style="color: #000000;">.</span><span style="color: #000000;">"</span><span style="color: #000000;"><br></span><span style="color: #000000;">"</span><span style="color: #000000;">;<br>    }<br>}</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{<br>    </span><span style="color: #0000ff;">echo</span><span style="color: #800080;">$res</span><span style="color: #000000;">-></span><span style="color: #000000;">faultcode()</span><span style="color: #000000;">.</span><span style="color: #000000;">"</span><span style="color: #000000;">:</span><span style="color: #000000;">"</span><span style="color: #000000;">.</span><span style="color: #800080;">$res</span><span style="color: #000000;">-></span><span style="color: #000000;">faultString()</span><span style="color: #000000;">.</span><span style="color: #000000;">"</span><span style="color: #000000;"><br></span><span style="color: #000000;">"</span><span style="color: #000000;">;<br>}<br></span><span style="color: #000000;">?></span>

敢死队2在线观看

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn