ch07s05.html.svn-base
来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· SVN-BASE 代码 · 共 115 行 · 第 1/2 页
SVN-BASE
115 行
<html><head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>5. xmlrpc_server</title><link rel="stylesheet" href="html.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets V1.71.1"><link rel="start" href="index.html" title="XML-RPC for PHP"><link rel="up" href="ch07.html" title="Chapter 7. Class documentation"><link rel="prev" href="ch07s04.html" title="4. xmlrpcresp"><link rel="next" href="ch08.html" title="Chapter 8. Global variables"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">5. xmlrpc_server</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch07s04.html">Prev</a> </td><th width="60%" align="center">Chapter 7. Class documentation</th><td width="20%" align="right"> <a accesskey="n" href="ch08.html">Next</a></td></tr></table><hr></div><div class="sect1" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="xmlrpc-server"></a>5. xmlrpc_server</h2></div></div></div><p>The implementation of this class has been kept as simple to use as possible. The constructor for the server basically does all the work. Here's a minimal example:</p><pre class="programlisting"> function foo ($xmlrpcmsg) { ... return new xmlrpcresp($some_xmlrpc_val); } class bar { function foobar($xmlrpcmsg) { ... return new xmlrpcresp($some_xmlrpc_val); } } $s = new xmlrpc_server( array( "examples.myFunc1" => array("function" => "foo"), "examples.myFunc2" => array("function" => "bar::foobar"), ));</pre><p>This performs everything you need to do with a server. The single constructor argument is an associative array from xmlrpc method names to php function names. The incoming request is parsed and dispatched to the relevant php function, which is responsible for returning a <code class="classname">xmlrpcresp</code> object, that will be serialized back to the caller.</p><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="d0e2386"></a>5.1. Method handler functions</h3></div></div></div><p>Both php functions and class methods can be registered as xmlrpc method handlers.</p><p>The synopsis of a method handler function is:</p><pre class="synopsis">xmlrpcresp $resp = function (xmlrpcmsg $msg)</pre><p>No text should be echoed 'to screen' by the handler function, or it will break the xml response sent back to the client. This applies also to error and warning messages that PHP prints to screen unless the appropriate parameters have been set in the php.in file. Another way to prevent echoing of errors inside the response and facilitate debugging is to use the server SetDebug method with debug level 3 (see below).</p><p>Note that if you implement a method with a name prefixed by <code class="code">system.</code> the handler function will be invoked by the server with two parameters, the first being the server itself and the second being the <code class="classname">xmlrpcmsg</code> object.</p><p>The same php function can be registered as handler of multiple xmlrpc methods.</p><p>Here is a more detailed example of what the handler function <code class="function">foo</code> may do:</p><pre class="programlisting"> function foo ($xmlrpcmsg) { global $xmlrpcerruser; // import user errcode base value $meth = $xmlrpcmsg->method(); // retrieve method name $par = $xmlrpcmsg->getParam(0); // retrieve value of first parameter - assumes at least one param received $val = $par->scalarval(); // decode value of first parameter - assumes it is a scalar value ... if ($err) { // this is an error condition return new xmlrpcresp(0, $xmlrpcerruser+1, // user error 1 "There's a problem, Captain"); } else { // this is a successful value being returned return new xmlrpcresp(new xmlrpcval("All's fine!", "string")); } }</pre><p>See <code class="filename">server.php</code> in this distribution for more examples of how to do this.</p><p>Since release 2.0RC3 there is a new, even simpler way of registering php functions with the server. See section 5.7 below</p></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="d0e2422"></a>5.2. The dispatch map</h3></div></div></div><p>The first argument to the <code class="function">xmlrpc_server</code> constructor is an array, called the <span class="emphasis"><em>dispatch map</em></span>. In this array is the information the server needs to service the XML-RPC methods you define.</p><p>The dispatch map takes the form of an associative array of associative arrays: the outer array has one entry for each method, the key being the method name. The corresponding value is another associative array, which can have the following members:</p><div class="itemizedlist"><ul type="disc"><li><p><code class="function"><code class="literal">function</code></code> - this entry is mandatory. It must be either a name of a function in the global scope which services the XML-RPC method, or an array containing an instance of an object and a static method name (for static class methods the 'class::method' syntax is also supported).</p></li><li><p><code class="function"><code class="literal">signature</code></code> - this entry is an array containing the possible signatures (see <a href="ch07s05.html#signatures">Signatures</a>) for the method. If this entry is present then the server will check that the correct number and type of parameters have been sent for this method before dispatching it.</p></li><li><p><code class="function"><code class="literal">docstring</code></code> - this entry is a string containing documentation for the method. The documentation may contain HTML markup.</p></li><li><p><code class="literal">signature_docs</code> - this entry can be used to provide documentation for the single parameters. It must match in structure the 'signature' member. By default, only the <code class="classname">documenting_xmlrpc_server</code> class in the extras package will take advantage of this, since the "system.methodHelp" protocol does not support documenting method parameters individually.</p></li></ul></div><p>Look at the <code class="filename">server.php</code> example in the distribution to see what a dispatch map looks like.</p></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="signatures"></a>5.3. Method signatures</h3></div></div></div><p>A signature is a description of a method's return type and its parameter types. A method may have more than one signature.</p><p>Within a server's dispatch map, each method has an array of possible signatures. Each signature is an array of types. The first entry is the return type. For instance, the method </p><pre class="programlisting">string examples.getStateName(int)</pre><p> has the signature </p><pre class="programlisting">array($xmlrpcString, $xmlrpcInt)</pre><p> and, assuming that it is the only possible signature for the method, it might be used like this in server creation: </p><pre class="programlisting">$findstate_sig = array(array($xmlrpcString, $xmlrpcInt));$findstate_doc = 'When passed an integer between 1 and 51 returns thename of a US state, where the integer is the index of that state namein an alphabetic order.';$s = new xmlrpc_server( array( "examples.getStateName" => array( "function" => "findstate", "signature" => $findstate_sig, "docstring" => $findstate_doc )));</pre><p>Note that method signatures do not allow to check nested parameters, e.g. the number, names and types of the members of a struct param cannot be validated.</p><p>If a method that you want to expose has a definite number of parameters, but each of those parameters could reasonably be of multiple types, the array of acceptable signatures will easily grow into a combinatorial explosion. To avoid such a situation, the lib defines the global var <code class="varname">$xmlrpcValue</code>, which can be used in method signatures as a placeholder for 'any xmlrpc type':</p><pre class="programlisting">
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?