| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571 |
- <?xml version="1.0" encoding="UTF-8"?>
- <!-- Reviewed: no -->
- <sect1 id="zend.xmlrpc.client">
- <title>Zend_XmlRpc_Client</title>
- <sect2 id="zend.xmlrpc.client.introduction">
- <title>Introduction</title>
- <para>
- Zend Framework provides support for consuming remote XML-RPC
- services as a client in the <classname>Zend_XmlRpc_Client</classname>
- package. Its major features include automatic type conversion
- between PHP and XML-RPC, a server proxy object, and access to
- server introspection capabilities.
- </para>
- </sect2>
- <sect2 id="zend.xmlrpc.client.method-calls">
- <title>Method Calls</title>
- <para>
- The constructor of <classname>Zend_XmlRpc_Client</classname> receives the
- URL of the remote XML-RPC server endpoint as its first parameter.
- The new instance returned may be used to call any number of
- remote methods at that endpoint.
- </para>
- <para>
- To call a remote method with the XML-RPC client, instantiate it
- and use the <code>call()</code> instance method. The code sample
- below uses a demonstration XML-RPC server on the Zend Framework
- website. You can use it for testing or exploring the
- <classname>Zend_XmlRpc</classname> components.
- </para>
- <example id="zend.xmlrpc.client.method-calls.example-1">
- <title>XML-RPC Method Call</title>
- <programlisting language="php"><![CDATA[
- $client = new Zend_XmlRpc_Client('http://framework.zend.com/xmlrpc');
- echo $client->call('test.sayHello');
- // hello
- ]]></programlisting>
- </example>
- <para>
- The XML-RPC value returned from the remote method call will be
- automatically unmarshaled and cast to the equivalent PHP native
- type. In the example above, a PHP <type>String</type> is returned
- and is immediately ready to be used.
- </para>
- <para>
- The first parameter of the <code>call()</code> method receives the
- name of the remote method to call. If the remote method requires
- any parameters, these can be sent by supplying a second, optional
- parameter to <code>call()</code> with an <type>Array</type> of
- values to pass to the remote method:
- </para>
- <example id="zend.xmlrpc.client.method-calls.example-2">
- <title>XML-RPC Method Call with Parameters</title>
- <programlisting language="php"><![CDATA[
- $client = new Zend_XmlRpc_Client('http://framework.zend.com/xmlrpc');
- $arg1 = 1.1;
- $arg2 = 'foo';
- $result = $client->call('test.sayHello', array($arg1, $arg2));
- // $result is a native PHP type
- ]]></programlisting>
- </example>
- <para>
- If the remote method doesn't require parameters, this optional
- parameter may either be left out or an empty <code>array()</code>
- passed to it. The array of parameters for the remote method can
- contain native PHP types, <classname>Zend_XmlRpc_Value</classname>
- objects, or a mix of each.
- </para>
- <para>
- The <code>call()</code> method will automatically convert the
- XML-RPC response and return its equivalent PHP native type. A
- <classname>Zend_XmlRpc_Response</classname> object for the return value will
- also be available by calling the <code>getLastResponse()</code>
- method after the call.
- </para>
- </sect2>
- <sect2 id="zend.xmlrpc.value.parameters">
- <title>Types and Conversions</title>
- <para>
- Some remote method calls require parameters. These are given to
- the <code>call()</code> method of <classname>Zend_XmlRpc_Client</classname>
- as an array in the second parameter. Each parameter may be
- given as either a native PHP type which will be automatically
- converted, or as an object representing a specific XML-RPC type
- (one of the <classname>Zend_XmlRpc_Value</classname> objects).
- </para>
- <sect3 id="zend.xmlrpc.value.parameters.php-native">
- <title>PHP Native Types as Parameters</title>
- <para>
- Parameters may be passed to <code>call()</code> as native PHP
- variables, meaning as a <type>String</type>,
- <code>integer</code>, <code>float</code>,
- <type>Boolean</type>, <type>Array</type>, or an
- <code>object</code>. In this case, each PHP native type will
- be auto-detected and converted into one of the XML-RPC types
- according to this table:
- </para>
- <table id="zend.xmlrpc.value.parameters.php-native.table-1">
- <title>PHP and XML-RPC Type Conversions</title>
- <tgroup cols="2">
- <thead>
- <row>
- <entry>PHP Native Type</entry>
- <entry>XML-RPC Type</entry>
- </row>
- </thead>
- <tbody>
- <row>
- <entry>integer</entry>
- <entry>int</entry>
- </row>
- <row>
- <entry>double</entry>
- <entry>double</entry>
- </row>
- <row>
- <entry>boolean</entry>
- <entry>boolean</entry>
- </row>
- <row>
- <entry>string</entry>
- <entry>string</entry>
- </row>
- <row>
- <entry>array</entry>
- <entry>array</entry>
- </row>
- <row>
- <entry>associative array</entry>
- <entry>struct</entry>
- </row>
- <row>
- <entry>object</entry>
- <entry>array</entry>
- </row>
- </tbody>
- </tgroup>
- </table>
- <note>
- <title>What type do empty arrays get cast to?</title>
- <para>
- Passing an empty array to an XML-RPC method is problematic,
- as it could represent either an array or a struct.
- <classname>Zend_XmlRpc_Client</classname> detects such conditions and
- makes a request to the server's
- <code>system.methodSignature</code> method to determine the
- appropriate XML-RPC type to cast to.
- </para>
- <para>
- However, this in itself can lead to issues. First off,
- servers that do not support
- <code>system.methodSignature</code> will log failed
- requests, and <classname>Zend_XmlRpc_Client</classname> will resort to
- casting the value to an XML-RPC array type. Additionally,
- this means that any call with array arguments will result in
- an additional call to the remote server.
- </para>
- <para>
- To disable the lookup entirely, you can call the
- <code>setSkipSystemLookup()</code> method prior to making
- your XML-RPC call:
- </para>
- <programlisting language="php"><![CDATA[
- $client->setSkipSystemLookup(true);
- $result = $client->call('foo.bar', array(array()));
- ]]></programlisting>
- </note>
- </sect3>
- <sect3 id="zend.xmlrpc.value.parameters.xmlrpc-value">
- <title>Zend_XmlRpc_Value Objects as Parameters</title>
- <para>
- Parameters may also be created as <classname>Zend_XmlRpc_Value</classname>
- instances to specify an exact XML-RPC type. The primary reasons
- for doing this are:
- <itemizedlist>
- <listitem>
- <para>
- When you want to make sure the correct parameter
- type is passed to the procedure (i.e. the
- procedure requires an integer and you may get it
- from a database as a string)
- </para>
- </listitem>
- <listitem>
- <para>
- When the procedure requires <code>base64</code> or
- <code>dateTime.iso8601</code> type (which doesn't exists as a
- PHP native type)
- </para>
- </listitem>
- <listitem>
- <para>
- When auto-conversion may fail (i.e. you want to
- pass an empty XML-RPC struct as a parameter. Empty
- structs are represented as empty arrays in PHP
- but, if you give an empty array as a parameter it
- will be auto-converted to an XML-RPC array since
- it's not an associative array)
- </para>
- </listitem>
- </itemizedlist>
- </para>
- <para>
- There are two ways to create a <classname>Zend_XmlRpc_Value</classname>
- object: instantiate one of the <classname>Zend_XmlRpc_Value</classname>
- subclasses directly, or use the static factory method
- <classname>Zend_XmlRpc_Value::getXmlRpcValue()</classname>.
- </para>
- <table id="zend.xmlrpc.value.parameters.xmlrpc-value.table-1">
- <title>Zend_XmlRpc_Value Objects for XML-RPC Types</title>
- <tgroup cols="3">
- <thead>
- <row>
- <entry>XML-RPC Type</entry>
- <entry><classname>Zend_XmlRpc_Value</classname> Constant</entry>
- <entry><classname>Zend_XmlRpc_Value</classname> Object</entry>
- </row>
- </thead>
- <tbody>
- <row>
- <entry>int</entry>
- <entry>
- <classname>Zend_XmlRpc_Value::XMLRPC_TYPE_INTEGER</classname>
- </entry>
- <entry><classname>Zend_XmlRpc_Value_Integer</classname></entry>
- </row>
- <row>
- <entry>double</entry>
- <entry>
- <classname>Zend_XmlRpc_Value::XMLRPC_TYPE_DOUBLE</classname>
- </entry>
- <entry><classname>Zend_XmlRpc_Value_Double</classname></entry>
- </row>
- <row>
- <entry>boolean</entry>
- <entry>
- <classname>Zend_XmlRpc_Value::XMLRPC_TYPE_BOOLEAN</classname>
- </entry>
- <entry><classname>Zend_XmlRpc_Value_Boolean</classname></entry>
- </row>
- <row>
- <entry>string</entry>
- <entry>
- <classname>Zend_XmlRpc_Value::XMLRPC_TYPE_STRING</classname>
- </entry>
- <entry><classname>Zend_XmlRpc_Value_String</classname></entry>
- </row>
- <row>
- <entry>base64</entry>
- <entry>
- <classname>Zend_XmlRpc_Value::XMLRPC_TYPE_BASE64</classname>
- </entry>
- <entry><classname>Zend_XmlRpc_Value_Base64</classname></entry>
- </row>
- <row>
- <entry>dateTime.iso8601</entry>
- <entry>
- <classname>Zend_XmlRpc_Value::XMLRPC_TYPE_DATETIME</classname>
- </entry>
- <entry><classname>Zend_XmlRpc_Value_DateTime</classname></entry>
- </row>
- <row>
- <entry>array</entry>
- <entry>
- <classname>Zend_XmlRpc_Value::XMLRPC_TYPE_ARRAY</classname>
- </entry>
- <entry><classname>Zend_XmlRpc_Value_Array</classname></entry>
- </row>
- <row>
- <entry>struct</entry>
- <entry>
- <classname>Zend_XmlRpc_Value::XMLRPC_TYPE_STRUCT</classname>
- </entry>
- <entry><classname>Zend_XmlRpc_Value_Struct</classname></entry>
- </row>
- </tbody>
- </tgroup>
- </table>
- <para>
- <note>
- <title>Automatic Conversion</title>
- <para>
- When building a new <classname>Zend_XmlRpc_Value</classname>
- object, its value is set by a PHP type. The PHP type
- will be converted to the specified type using
- PHP casting. For example, if a string is given as a
- value to the <classname>Zend_XmlRpc_Value_Integer</classname>
- object, it will be converted using
- <code>(int)$value</code>.
- </para>
- </note>
- </para>
- </sect3>
- </sect2>
- <sect2 id="zend.xmlrpc.client.requests-and-responses">
- <title>Server Proxy Object</title>
- <para>
- Another way to call remote methods with the XML-RPC client is to
- use the server proxy. This is a PHP object that proxies a remote
- XML-RPC namespace, making it work as close to a native PHP object
- as possible.
- </para>
- <para>
- To instantiate a server proxy, call the <code>getProxy()</code>
- instance method of <classname>Zend_XmlRpc_Client</classname>. This will
- return an instance of <classname>Zend_XmlRpc_Client_ServerProxy</classname>.
- Any method call on the server proxy object will be forwarded to
- the remote, and parameters may be passed like any other PHP
- method.
- </para>
- <example id="zend.xmlrpc.client.requests-and-responses.example-1">
- <title>Proxy the Default Namespace</title>
- <programlisting language="php"><![CDATA[
- $client = new Zend_XmlRpc_Client('http://framework.zend.com/xmlrpc');
- $server = $client->getProxy(); // Proxy the default namespace
- $hello = $server->test->sayHello(1, 2); // test.Hello(1, 2) returns "hello"
- ]]></programlisting>
- </example>
- <para>
- The <code>getProxy()</code> method receives an optional argument
- specifying which namespace of the remote server to proxy. If it
- does not receive a namespace, the default namespace will be
- proxied. In the next example, the <code>test</code> namespace
- will be proxied:
- </para>
- <example id="zend.xmlrpc.client.requests-and-responses.example-2">
- <title>Proxy Any Namespace</title>
- <programlisting language="php"><![CDATA[
- $client = new Zend_XmlRpc_Client('http://framework.zend.com/xmlrpc');
- $test = $client->getProxy('test'); // Proxy the "test" namespace
- $hello = $test->sayHello(1, 2); // test.Hello(1,2) returns "hello"
- ]]></programlisting>
- </example>
- <para>
- If the remote server supports nested namespaces of any depth,
- these can also be used through the server proxy. For example, if
- the server in the example above had a method
- <code>test.foo.bar()</code>, it could be called as
- <code>$test->foo->bar()</code>.
- </para>
- </sect2>
- <sect2 id="zend.xmlrpc.client.error-handling">
- <title>Error Handling</title>
- <para>
- Two kinds of errors can occur during an XML-RPC method call: HTTP
- errors and XML-RPC faults. The <classname>Zend_XmlRpc_Client</classname>
- recognizes each and provides the ability to detect and trap them
- independently.
- </para>
- <sect3 id="zend.xmlrpc.client.error-handling.http">
- <title>HTTP Errors</title>
- <para>
- If any HTTP error occurs, such as the remote HTTP server
- returns a <code>404 Not Found</code>, a
- <classname>Zend_XmlRpc_Client_HttpException</classname> will be thrown.
- </para>
- <example id="zend.xmlrpc.client.error-handling.http.example-1">
- <title>Handling HTTP Errors</title>
- <programlisting language="php"><![CDATA[
- $client = new Zend_XmlRpc_Client('http://foo/404');
- try {
- $client->call('bar', array($arg1, $arg2));
- } catch (Zend_XmlRpc_Client_HttpException $e) {
- // $e->getCode() returns 404
- // $e->getMessage() returns "Not Found"
- }
- ]]></programlisting>
- </example>
- <para>
- Regardless of how the XML-RPC client is used, the
- <classname>Zend_XmlRpc_Client_HttpException</classname> will be thrown
- whenever an HTTP error occurs.
- </para>
- </sect3>
- <sect3 id="zend.xmlrpc.client.error-handling.faults">
- <title>XML-RPC Faults</title>
- <para>
- An XML-RPC fault is analogous to a PHP exception. It is a
- special type returned from an XML-RPC method call that has
- both an error code and an error message. XML-RPC faults are
- handled differently depending on the context of how the
- <classname>Zend_XmlRpc_Client</classname> is used.
- </para>
- <para>
- When the <code>call()</code> method or the server
- proxy object is used, an XML-RPC fault will result in a
- <classname>Zend_XmlRpc_Client_FaultException</classname> being thrown.
- The code and message of the exception will map directly to
- their respective values in the original XML-RPC fault
- response.
- </para>
- <example id="zend.xmlrpc.client.error-handling.faults.example-1">
- <title>Handling XML-RPC Faults</title>
- <programlisting language="php"><![CDATA[
- $client = new Zend_XmlRpc_Client('http://framework.zend.com/xmlrpc');
- try {
- $client->call('badMethod');
- } catch (Zend_XmlRpc_Client_FaultException $e) {
- // $e->getCode() returns 1
- // $e->getMessage() returns "Unknown method"
- }
- ]]></programlisting>
- </example>
- <para>
- When the <code>call()</code> method is used to make the
- request, the <classname>Zend_XmlRpc_Client_FaultException</classname> will be
- thrown on fault. A <classname>Zend_XmlRpc_Response</classname> object
- containing the fault will also be available by calling
- <code>getLastResponse()</code>.
- </para>
- <para>
- When the <code>doRequest()</code> method is used to make the
- request, it will not throw the exception. Instead, it will
- return a <classname>Zend_XmlRpc_Response</classname> object returned
- will containing the fault. This can be checked with
- <code>isFault()</code> instance method of
- <classname>Zend_XmlRpc_Response</classname>.
- </para>
- </sect3>
- </sect2>
- <sect2 id="zend.xmlrpc.client.introspection">
- <title>Server Introspection</title>
- <para>
- Some XML-RPC servers support the de facto introspection methods under the XML-RPC
- <code>system.</code> namespace. <classname>Zend_XmlRpc_Client</classname> provides
- special support for servers with these capabilities.
- </para>
- <para>
- A <classname>Zend_XmlRpc_Client_ServerIntrospection</classname> instance may be
- retrieved by calling the <code>getIntrospector()</code> method of
- <classname>Zend_XmlRpcClient</classname>. It can then be used to perform introspection
- operations on the server.
- </para>
- </sect2>
- <sect2 id="zend.xmlrpc.client.request-to-response">
- <title>From Request to Response</title>
- <para>
- Under the hood, the <code>call()</code> instance method of
- <classname>Zend_XmlRpc_Client</classname> builds a request object
- (<classname>Zend_XmlRpc_Request</classname>) and sends it to another method,
- <code>doRequest()</code>, that returns a response object
- (<classname>Zend_XmlRpc_Response</classname>).
- </para>
- <para>
- The <code>doRequest()</code> method is also available for use directly:
- </para>
- <example id="zend.xmlrpc.client.request-to-response.example-1">
- <title>Processing Request to Response</title>
- <programlisting language="php"><![CDATA[
- $client = new Zend_XmlRpc_Client('http://framework.zend.com/xmlrpc');
- $request = new Zend_XmlRpc_Request();
- $request->setMethod('test.sayHello');
- $request->setParams(array('foo', 'bar'));
- $client->doRequest($request);
- // $server->getLastRequest() returns instanceof Zend_XmlRpc_Request
- // $server->getLastResponse() returns instanceof Zend_XmlRpc_Response
- ]]></programlisting>
- </example>
- <para>
- Whenever an XML-RPC method call is made by the client through any
- means, either the <code>call()</code> method,
- <code>doRequest()</code> method, or server proxy, the last request
- object and its resultant response object will always be available
- through the methods <code>getLastRequest()</code> and
- <code>getLastResponse()</code> respectively.
- </para>
- </sect2>
- <sect2 id="zend.xmlrpc.client.http-client">
- <title>HTTP Client and Testing</title>
- <para>
- In all of the prior examples, an HTTP client was never specified.
- When this is the case, a new instance of
- <classname>Zend_Http_Client</classname> will be created with its default
- options and used by <classname>Zend_XmlRpc_Client</classname> automatically.
- </para>
- <para>
- The HTTP client can be retrieved at any time with the
- <code>getHttpClient()</code> method. For most cases, the default
- HTTP client will be sufficient. However, the
- <code>setHttpClient()</code> method allows for a different HTTP
- client instance to be injected.
- </para>
- <para>
- The <code>setHttpClient()</code> is particularly useful for unit testing. When combined
- with the <classname>Zend_Http_Client_Adapter_Test</classname>, remote services can be
- mocked out for testing. See the unit tests for <classname>Zend_XmlRpc_Client</classname>
- for examples of how to do this.
- </para>
- </sect2>
- </sect1>
- <!--
- vim:se ts=4 sw=4 et:
- -->
|