Home  >  Article  >  php教程  >  Introduction and use of XML-RPC for PHP

Introduction and use of XML-RPC for PHP

不言
不言Original
2018-05-26 14:15:063194browse

1. What is XML-RPC?
XML-RPC is a format designed by Userland Software company: it is a format designed to use HTTP protocol transfers XML format file to obtain remote procedure call (Remote Procedure Call) transmission method. The official website is www.xmlrpc.com.

There is a PHP XML-RPCFramework(Collection of classes) is used to use PHP Language to write XML-RPC client and server. The current stable release version is 2.2, and the download address of is http://sourceforge.net/projects/phpxmlrpc/files/phpxmlrpc/2.2.2/xmlrpc -2.2.2.tar.gz/download. In addition, you can also find other s language

XML-RPCwww.xmlrpc.com 🎜> list, such as Perl, Python. This article will introduce the use of XML-RPC for PHP

class library. 2. System Operation Requirements

The design goals of this class library are scalability and backward compatibility. Therefore, it supports most existing PHP

versions. The minimum required PHP version is 4.2. This library provides a compatibility layer to support PHP 4.0.5 and 4.1. Of course, the official recommendation is to use PHP 5.0 or higher.

Note that if you want to use SSL or HTTP 1.1 to communicate with the remote server, you must put "CURL” extension is compiled into PHP.

In addition, the native "xmlrpc" extension that comes with PHP is introduced here The XML-RPC class library is not the same thing, so there is no need to compile it into PHP.

3. Introduction to related classes

1.xmlrpcval

There are six basic types in XML-RPC Two composite types, the basic types are: int, boolean, string , double, dateTime.iso8601, base6, the composite type is: array, struct. The function of Xmlrpcval is to convert the types in PHP into these types and put them into XML-RPC for transmission.


<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>

Related methods:
kindOf(): Returns the basic type of the object: "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在线观看

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn