Blame | Letzte Änderung | Log anzeigen | RSS feed
#LyX 1.3 created this file. For more info see http://www.lyx.org/\lyxformat 221\textclass docbook-section\language english\inputencoding auto\fontscheme default\graphics default\paperfontsize default\spacing single\papersize Default\paperpackage a4\use_geometry 0\use_amsmath 0\use_natbib 0\use_numerical_citations 0\paperorientation portrait\secnumdepth 3\tocdepth 3\paragraph_separation indent\defskip medskip\quotes_language english\quotes_times 2\papercolumns 1\papersides 1\paperpagestyle default\layout Title\added_space_top vfill \added_space_bottom vfillXML_RPC2 Tutorial\layout AbstractThis tutorial introduces basic usage of XML_RPC2 as a client/server libraryin XML_RPC operations.XML_RPC2 is a pear package providing XML_RPC client and server services.XML-RPC is a simple remote procedure call protocol built using HTTP astransport and XML as encoding.\layout AbstractAs a client library, XML_RPC2 is capable of creating a proxy class whichexposes the methods exported by the server.As a server library, XML_RPC2 is capable of exposing methods from a classor object instance, seamlessly exporting local methods as remotely callableprocedures.\layout SubsectionClient usage\layout SubsubsectionBasic Usage\layout StandardThe most simple way to use the XML_RPC client is by letting XML_RPC2 selectthe backend for you, and just give the client factory method the data referringto the server:\layout ItemizeThe server URI.\layout ItemizeThe HTTP proxy URI (null if no proxy).\layout ItemizeThe method prefix\layout Coderequire_once('XML/RPC2/Client.php');\layout Code$client = XML_RPC2_Client::create('http://rpc.example.com:80/', null, '');\layout StandardThe factory will produce a client proxy.This class exports whichever methods the server exports.These methods are called just like regular local methods:\layout Codeprint($client->hello('World'));\layout Standardfor a server that exports the method hello.If the server has methods prefixed by a classname (example.hello), thereare two solutions.Either call the method using brackets enclosing the otherwise php-invalidmethod name:\layout Codeprint($client->{example.hello}('World'));\layout StandardOr specify a method prefix when creating the client instance:\layout Code$client = XML_RPC2_Client::create('http://rpc.example.com:80/', null, 'example.');\layout Codeprint($client->hello('World'));\layout SubsubsectionError handling\layout StandardXML_RPC2 uses exceptions to signal errors.The phpdoc reference contains a class hierarchy useful to get a grasp ofpossible errors.The most important characteristics of the XML_RPC2 exception tree are:\layout ItemizeAll XML_RPC2 exceptions are children of XML_RPC2_Exception.If you want to filter out exceptions from this package, catch XML_RPC2_Exception\layout ItemizeNetwork failure is signaled by an XML_RPC2_TransportException\layout ItemizeRegular XML-RPC fault responses are signaled by an XML_RPC2_FaultException\layout ItemizeAll other types of XML_RPC2_Exception signal package misuse or bug-inducedmisbehaviour\layout StandardStandard usage:\layout Coderequire_once('XMLrequire_once('XML/RPC2/Client.php');\layout Codetry {\layout Code$client = XML_RPC2_Client::create('http://rpc.example.com:80/', null,'');\layout Codeprint($client->hello('World'));\layout Code} catch (XML_RPC2_TransportException transportException) {\layout Code// Handle network-induced exception\layout Code} catch (XML_RPC2_FaultException fault) {\layout Code// Handle fault returned by remote server\layout Code} catch (XML_RPC2_Exception xmlRpcException) {\layout Code// Handle abnormal XML_RPC2 package exception\layout Code} catch (Exception e) {\layout Code// Handle someone else's fault exception\layout Code}\layout StandardIt is good practice to at least expect XML_RPC2_TransportException as networkfailure can't ever be ruled out.\layout SubsectionServer usage\layout SubsubsectionBasic Usage\layout StandardTo export an XML-RPC server using XML_RPC2, the first step is writing themethods to export.XML_RPC2 can export class methods (static methods) for a class, or allmethods for an object instance.For this example, we'll export a class' static methods:\layout Codeclass EchoServer {\layout Code/**\layout Code* echoecho echoes the message received\layout Code*\layout Code* @param string Message\layout Code* @return string The echo\layout Code*/\layout Code\layout Codepublic static function echoecho($string)\layout Code{\layout Codereturn $string;\layout Code}\layout Code\layout Code/**\layout Code* Dummy method which won't be exported\layout Code*\layout Code* @xmlrpc.hidden\layout Code*/\layout Codepublic static function dummy()\layout Code{\layout Codereturn false;\layout Code}\layout Code\layout Code/**\layout Code* hello says hello\layout Code*\layout Code* @param string Name\layout Code* @return string Hello 'name'\layout Code*/\layout Codepublic function hello($name)\layout Code{\layout Codereturn "Hello $name";\layout Code}\layout Code\layout Code}\layout StandardNote that the method is documented using phpDoc docblocks.The docblock is used to deduce the signature and method documentation,required by the XML-RPC spec.Non-documented methods are not exported.Methods tagged with the tag @xmlrpc.hidden are not exported either (thedummy method above won't be exported).\layout StandardAfter creating the class, we need to get an XML_RPC2 server to export itsmethods remotely:\layout Coderequire_once 'XML/RPC2/Server.php';\layout Code$server = XML_RPC2_Server::create('EchoServer');\layout Code$server->handleCall();\layout StandardThe XML_RPC2_Server automatically exports all of the EchoServer class publicstatic methods (echoecho in this case).You may also export all of an instance's public methods (static or otherwise):\layout Coderequire_once 'XML/RPC2/Server.php';\layout Code$server = XML_RPC2_Server::create(new EchoServer());\layout Code$server->handleCall();\the_end