Zend_XmlRpc_Client.xml 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.xmlrpc.client">
  4. <title>Zend_XmlRpc_Client</title>
  5. <sect2 id="zend.xmlrpc.client.introduction">
  6. <title>Introduction</title>
  7. <para>
  8. Zend Framework provides support for consuming remote XML-RPC
  9. services as a client in the <classname>Zend_XmlRpc_Client</classname>
  10. package. Its major features include automatic type conversion
  11. between PHP and XML-RPC, a server proxy object, and access to
  12. server introspection capabilities.
  13. </para>
  14. </sect2>
  15. <sect2 id="zend.xmlrpc.client.method-calls">
  16. <title>Method Calls</title>
  17. <para>
  18. The constructor of <classname>Zend_XmlRpc_Client</classname> receives the
  19. URL of the remote XML-RPC server endpoint as its first parameter.
  20. The new instance returned may be used to call any number of
  21. remote methods at that endpoint.
  22. </para>
  23. <para>
  24. To call a remote method with the XML-RPC client, instantiate it
  25. and use the <code>call()</code> instance method. The code sample
  26. below uses a demonstration XML-RPC server on the Zend Framework
  27. website. You can use it for testing or exploring the
  28. <classname>Zend_XmlRpc</classname> components.
  29. </para>
  30. <example id="zend.xmlrpc.client.method-calls.example-1">
  31. <title>XML-RPC Method Call</title>
  32. <programlisting role="php"><![CDATA[
  33. $client = new Zend_XmlRpc_Client('http://framework.zend.com/xmlrpc');
  34. echo $client->call('test.sayHello');
  35. // hello
  36. ]]></programlisting>
  37. </example>
  38. <para>
  39. The XML-RPC value returned from the remote method call will be
  40. automatically unmarshaled and cast to the equivalent PHP native
  41. type. In the example above, a PHP <code>string</code> is returned
  42. and is immediately ready to be used.
  43. </para>
  44. <para>
  45. The first parameter of the <code>call()</code> method receives the
  46. name of the remote method to call. If the remote method requires
  47. any parameters, these can be sent by supplying a second, optional
  48. parameter to <code>call()</code> with an <code>array</code> of
  49. values to pass to the remote method:
  50. </para>
  51. <example id="zend.xmlrpc.client.method-calls.example-2">
  52. <title>XML-RPC Method Call with Parameters</title>
  53. <programlisting role="php"><![CDATA[
  54. $client = new Zend_XmlRpc_Client('http://framework.zend.com/xmlrpc');
  55. $arg1 = 1.1;
  56. $arg2 = 'foo';
  57. $result = $client->call('test.sayHello', array($arg1, $arg2));
  58. // $result is a native PHP type
  59. ]]></programlisting>
  60. </example>
  61. <para>
  62. If the remote method doesn't require parameters, this optional
  63. parameter may either be left out or an empty <code>array()</code>
  64. passed to it. The array of parameters for the remote method can
  65. contain native PHP types, <classname>Zend_XmlRpc_Value</classname>
  66. objects, or a mix of each.
  67. </para>
  68. <para>
  69. The <code>call()</code> method will automatically convert the
  70. XML-RPC response and return its equivalent PHP native type. A
  71. <classname>Zend_XmlRpc_Response</classname> object for the return value will
  72. also be available by calling the <code>getLastResponse()</code>
  73. method after the call.
  74. </para>
  75. </sect2>
  76. <sect2 id="zend.xmlrpc.value.parameters">
  77. <title>Types and Conversions</title>
  78. <para>
  79. Some remote method calls require parameters. These are given to
  80. the <code>call()</code> method of <classname>Zend_XmlRpc_Client</classname>
  81. as an array in the second parameter. Each parameter may be
  82. given as either a native PHP type which will be automatically
  83. converted, or as an object representing a specific XML-RPC type
  84. (one of the <classname>Zend_XmlRpc_Value</classname> objects).
  85. </para>
  86. <sect3 id="zend.xmlrpc.value.parameters.php-native">
  87. <title>PHP Native Types as Parameters</title>
  88. <para>
  89. Parameters may be passed to <code>call()</code> as native PHP
  90. variables, meaning as a <code>string</code>,
  91. <code>integer</code>, <code>float</code>,
  92. <code>boolean</code>, <code>array</code>, or an
  93. <code>object</code>. In this case, each PHP native type will
  94. be auto-detected and converted into one of the XML-RPC types
  95. according to this table:
  96. </para>
  97. <table id="zend.xmlrpc.value.parameters.php-native.table-1">
  98. <title>PHP and XML-RPC Type Conversions</title>
  99. <tgroup cols="2">
  100. <thead>
  101. <row>
  102. <entry>PHP Native Type</entry>
  103. <entry>XML-RPC Type</entry>
  104. </row>
  105. </thead>
  106. <tbody>
  107. <row>
  108. <entry>integer</entry>
  109. <entry>int</entry>
  110. </row>
  111. <row>
  112. <entry>double</entry>
  113. <entry>double</entry>
  114. </row>
  115. <row>
  116. <entry>boolean</entry>
  117. <entry>boolean</entry>
  118. </row>
  119. <row>
  120. <entry>string</entry>
  121. <entry>string</entry>
  122. </row>
  123. <row>
  124. <entry>array</entry>
  125. <entry>array</entry>
  126. </row>
  127. <row>
  128. <entry>associative array</entry>
  129. <entry>struct</entry>
  130. </row>
  131. <row>
  132. <entry>object</entry>
  133. <entry>array</entry>
  134. </row>
  135. </tbody>
  136. </tgroup>
  137. </table>
  138. <note>
  139. <title>What type do empty arrays get cast to?</title>
  140. <para>
  141. Passing an empty array to an XML-RPC method is problematic,
  142. as it could represent either an array or a struct.
  143. <classname>Zend_XmlRpc_Client</classname> detects such conditions and
  144. makes a request to the server's
  145. <code>system.methodSignature</code> method to determine the
  146. appropriate XML-RPC type to cast to.
  147. </para>
  148. <para>
  149. However, this in itself can lead to issues. First off,
  150. servers that do not support
  151. <code>system.methodSignature</code> will log failed
  152. requests, and <classname>Zend_XmlRpc_Client</classname> will resort to
  153. casting the value to an XML-RPC array type. Additionally,
  154. this means that any call with array arguments will result in
  155. an additional call to the remote server.
  156. </para>
  157. <para>
  158. To disable the lookup entirely, you can call the
  159. <code>setSkipSystemLookup()</code> method prior to making
  160. your XML-RPC call:
  161. </para>
  162. <programlisting role="php"><![CDATA[
  163. $client->setSkipSystemLookup(true);
  164. $result = $client->call('foo.bar', array(array()));
  165. ]]></programlisting>
  166. </note>
  167. </sect3>
  168. <sect3 id="zend.xmlrpc.value.parameters.xmlrpc-value">
  169. <title>Zend_XmlRpc_Value Objects as Parameters</title>
  170. <para>
  171. Parameters may also be created as <classname>Zend_XmlRpc_Value</classname>
  172. instances to specify an exact XML-RPC type. The primary reasons
  173. for doing this are:
  174. <itemizedlist>
  175. <listitem>
  176. <para>
  177. When you want to make sure the correct parameter
  178. type is passed to the procedure (i.e. the
  179. procedure requires an integer and you may get it
  180. from a database as a string)
  181. </para>
  182. </listitem>
  183. <listitem>
  184. <para>
  185. When the procedure requires <code>base64</code> or
  186. <code>dateTime.iso8601</code> type (which doesn't exists as a
  187. PHP native type)
  188. </para>
  189. </listitem>
  190. <listitem>
  191. <para>
  192. When auto-conversion may fail (i.e. you want to
  193. pass an empty XML-RPC struct as a parameter. Empty
  194. structs are represented as empty arrays in PHP
  195. but, if you give an empty array as a parameter it
  196. will be auto-converted to an XML-RPC array since
  197. it's not an associative array)
  198. </para>
  199. </listitem>
  200. </itemizedlist>
  201. </para>
  202. <para>
  203. There are two ways to create a <classname>Zend_XmlRpc_Value</classname>
  204. object: instantiate one of the <classname>Zend_XmlRpc_Value</classname>
  205. subclasses directly, or use the static factory method
  206. <classname>Zend_XmlRpc_Value::getXmlRpcValue()</classname>.
  207. </para>
  208. <table id="zend.xmlrpc.value.parameters.xmlrpc-value.table-1">
  209. <title>Zend_XmlRpc_Value Objects for XML-RPC Types</title>
  210. <tgroup cols="3">
  211. <thead>
  212. <row>
  213. <entry>XML-RPC Type</entry>
  214. <entry><classname>Zend_XmlRpc_Value</classname> Constant</entry>
  215. <entry><classname>Zend_XmlRpc_Value</classname> Object</entry>
  216. </row>
  217. </thead>
  218. <tbody>
  219. <row>
  220. <entry>int</entry>
  221. <entry><classname>Zend_XmlRpc_Value::XMLRPC_TYPE_INTEGER</classname></entry>
  222. <entry><classname>Zend_XmlRpc_Value_Integer</classname></entry>
  223. </row>
  224. <row>
  225. <entry>double</entry>
  226. <entry><classname>Zend_XmlRpc_Value::XMLRPC_TYPE_DOUBLE</classname></entry>
  227. <entry><classname>Zend_XmlRpc_Value_Double</classname></entry>
  228. </row>
  229. <row>
  230. <entry>boolean</entry>
  231. <entry><classname>Zend_XmlRpc_Value::XMLRPC_TYPE_BOOLEAN</classname></entry>
  232. <entry><classname>Zend_XmlRpc_Value_Boolean</classname></entry>
  233. </row>
  234. <row>
  235. <entry>string</entry>
  236. <entry><classname>Zend_XmlRpc_Value::XMLRPC_TYPE_STRING</classname></entry>
  237. <entry><classname>Zend_XmlRpc_Value_String</classname></entry>
  238. </row>
  239. <row>
  240. <entry>base64</entry>
  241. <entry><classname>Zend_XmlRpc_Value::XMLRPC_TYPE_BASE64</classname></entry>
  242. <entry><classname>Zend_XmlRpc_Value_Base64</classname></entry>
  243. </row>
  244. <row>
  245. <entry>dateTime.iso8601</entry>
  246. <entry><classname>Zend_XmlRpc_Value::XMLRPC_TYPE_DATETIME</classname></entry>
  247. <entry><classname>Zend_XmlRpc_Value_DateTime</classname></entry>
  248. </row>
  249. <row>
  250. <entry>array</entry>
  251. <entry><classname>Zend_XmlRpc_Value::XMLRPC_TYPE_ARRAY</classname></entry>
  252. <entry><classname>Zend_XmlRpc_Value_Array</classname></entry>
  253. </row>
  254. <row>
  255. <entry>struct</entry>
  256. <entry><classname>Zend_XmlRpc_Value::XMLRPC_TYPE_STRUCT</classname></entry>
  257. <entry><classname>Zend_XmlRpc_Value_Struct</classname></entry>
  258. </row>
  259. </tbody>
  260. </tgroup>
  261. </table>
  262. <para>
  263. <note>
  264. <title>Automatic Conversion</title>
  265. <para>
  266. When building a new <classname>Zend_XmlRpc_Value</classname>
  267. object, its value is set by a PHP type. The PHP type
  268. will be converted to the specified type using
  269. PHP casting. For example, if a string is given as a
  270. value to the <classname>Zend_XmlRpc_Value_Integer</classname>
  271. object, it will be converted using
  272. <code>(int)$value</code>.
  273. </para>
  274. </note>
  275. </para>
  276. </sect3>
  277. </sect2>
  278. <sect2 id="zend.xmlrpc.client.requests-and-responses">
  279. <title>Server Proxy Object</title>
  280. <para>
  281. Another way to call remote methods with the XML-RPC client is to
  282. use the server proxy. This is a PHP object that proxies a remote
  283. XML-RPC namespace, making it work as close to a native PHP object
  284. as possible.
  285. </para>
  286. <para>
  287. To instantiate a server proxy, call the <code>getProxy()</code>
  288. instance method of <classname>Zend_XmlRpc_Client</classname>. This will
  289. return an instance of <classname>Zend_XmlRpc_Client_ServerProxy</classname>.
  290. Any method call on the server proxy object will be forwarded to
  291. the remote, and parameters may be passed like any other PHP
  292. method.
  293. </para>
  294. <example id="zend.xmlrpc.client.requests-and-responses.example-1">
  295. <title>Proxy the Default Namespace</title>
  296. <programlisting role="php"><![CDATA[
  297. $client = new Zend_XmlRpc_Client('http://framework.zend.com/xmlrpc');
  298. $server = $client->getProxy(); // Proxy the default namespace
  299. $hello = $server->test->sayHello(1, 2); // test.Hello(1, 2) returns "hello"
  300. ]]></programlisting>
  301. </example>
  302. <para>
  303. The <code>getProxy()</code> method receives an optional argument
  304. specifying which namespace of the remote server to proxy. If it
  305. does not receive a namespace, the default namespace will be
  306. proxied. In the next example, the <code>test</code> namespace
  307. will be proxied:
  308. </para>
  309. <example id="zend.xmlrpc.client.requests-and-responses.example-2">
  310. <title>Proxy Any Namespace</title>
  311. <programlisting role="php"><![CDATA[
  312. $client = new Zend_XmlRpc_Client('http://framework.zend.com/xmlrpc');
  313. $test = $client->getProxy('test'); // Proxy the "test" namespace
  314. $hello = $test->sayHello(1, 2); // test.Hello(1,2) returns "hello"
  315. ]]></programlisting>
  316. </example>
  317. <para>
  318. If the remote server supports nested namespaces of any depth,
  319. these can also be used through the server proxy. For example, if
  320. the server in the example above had a method
  321. <code>test.foo.bar()</code>, it could be called as
  322. <code>$test->foo->bar()</code>.
  323. </para>
  324. </sect2>
  325. <sect2 id="zend.xmlrpc.client.error-handling">
  326. <title>Error Handling</title>
  327. <para>
  328. Two kinds of errors can occur during an XML-RPC method call: HTTP
  329. errors and XML-RPC faults. The <classname>Zend_XmlRpc_Client</classname>
  330. recognizes each and provides the ability to detect and trap them
  331. independently.
  332. </para>
  333. <sect3 id="zend.xmlrpc.client.error-handling.http">
  334. <title>HTTP Errors</title>
  335. <para>
  336. If any HTTP error occurs, such as the remote HTTP server
  337. returns a <code>404 Not Found</code>, a
  338. <classname>Zend_XmlRpc_Client_HttpException</classname> will be thrown.
  339. </para>
  340. <example id="zend.xmlrpc.client.error-handling.http.example-1">
  341. <title>Handling HTTP Errors</title>
  342. <programlisting role="php"><![CDATA[
  343. $client = new Zend_XmlRpc_Client('http://foo/404');
  344. try {
  345. $client->call('bar', array($arg1, $arg2));
  346. } catch (Zend_XmlRpc_Client_HttpException $e) {
  347. // $e->getCode() returns 404
  348. // $e->getMessage() returns "Not Found"
  349. }
  350. ]]></programlisting>
  351. </example>
  352. <para>
  353. Regardless of how the XML-RPC client is used, the
  354. <classname>Zend_XmlRpc_Client_HttpException</classname> will be thrown
  355. whenever an HTTP error occurs.
  356. </para>
  357. </sect3>
  358. <sect3 id="zend.xmlrpc.client.error-handling.faults">
  359. <title>XML-RPC Faults</title>
  360. <para>
  361. An XML-RPC fault is analogous to a PHP exception. It is a
  362. special type returned from an XML-RPC method call that has
  363. both an error code and an error message. XML-RPC faults are
  364. handled differently depending on the context of how the
  365. <classname>Zend_XmlRpc_Client</classname> is used.
  366. </para>
  367. <para>
  368. When the <code>call()</code> method or the server
  369. proxy object is used, an XML-RPC fault will result in a
  370. <classname>Zend_XmlRpc_Client_FaultException</classname> being thrown.
  371. The code and message of the exception will map directly to
  372. their respective values in the original XML-RPC fault
  373. response.
  374. </para>
  375. <example id="zend.xmlrpc.client.error-handling.faults.example-1">
  376. <title>Handling XML-RPC Faults</title>
  377. <programlisting role="php"><![CDATA[
  378. $client = new Zend_XmlRpc_Client('http://framework.zend.com/xmlrpc');
  379. try {
  380. $client->call('badMethod');
  381. } catch (Zend_XmlRpc_Client_FaultException $e) {
  382. // $e->getCode() returns 1
  383. // $e->getMessage() returns "Unknown method"
  384. }
  385. ]]></programlisting>
  386. </example>
  387. <para>
  388. When the <code>call()</code> method is used to make the
  389. request, the <classname>Zend_XmlRpc_Client_FaultException</classname> will be
  390. thrown on fault. A <classname>Zend_XmlRpc_Response</classname> object
  391. containing the fault will also be available by calling
  392. <code>getLastResponse()</code>.
  393. </para>
  394. <para>
  395. When the <code>doRequest()</code> method is used to make the
  396. request, it will not throw the exception. Instead, it will
  397. return a <classname>Zend_XmlRpc_Response</classname> object returned
  398. will containing the fault. This can be checked with
  399. <code>isFault()</code> instance method of
  400. <classname>Zend_XmlRpc_Response</classname>.
  401. </para>
  402. </sect3>
  403. </sect2>
  404. <sect2 id="zend.xmlrpc.client.introspection">
  405. <title>Server Introspection</title>
  406. <para>
  407. Some XML-RPC servers support the de facto introspection methods under the XML-RPC
  408. <code>system.</code> namespace. <classname>Zend_XmlRpc_Client</classname> provides special
  409. support for servers with these capabilities.
  410. </para>
  411. <para>
  412. A <classname>Zend_XmlRpc_Client_ServerIntrospection</classname> instance may be retrieved by calling
  413. the <code>getIntrospector()</code> method of <classname>Zend_XmlRpcClient</classname>. It can
  414. then be used to perform introspection operations on the server.
  415. </para>
  416. </sect2>
  417. <sect2 id="zend.xmlrpc.client.request-to-response">
  418. <title>From Request to Response</title>
  419. <para>
  420. Under the hood, the <code>call()</code> instance method of <classname>Zend_XmlRpc_Client</classname>
  421. builds a request object (<classname>Zend_XmlRpc_Request</classname>) and sends it to another method,
  422. <code>doRequest()</code>, that returns a response object (<classname>Zend_XmlRpc_Response</classname>).
  423. </para>
  424. <para>
  425. The <code>doRequest()</code> method is also available for use directly:
  426. </para>
  427. <example id="zend.xmlrpc.client.request-to-response.example-1">
  428. <title>Processing Request to Response</title>
  429. <programlisting role="php"><![CDATA[
  430. $client = new Zend_XmlRpc_Client('http://framework.zend.com/xmlrpc');
  431. $request = new Zend_XmlRpc_Request();
  432. $request->setMethod('test.sayHello');
  433. $request->setParams(array('foo', 'bar'));
  434. $client->doRequest($request);
  435. // $server->getLastRequest() returns instanceof Zend_XmlRpc_Request
  436. // $server->getLastResponse() returns instanceof Zend_XmlRpc_Response
  437. ]]></programlisting>
  438. </example>
  439. <para>
  440. Whenever an XML-RPC method call is made by the client through any
  441. means, either the <code>call()</code> method,
  442. <code>doRequest()</code> method, or server proxy, the last request
  443. object and its resultant response object will always be available
  444. through the methods <code>getLastRequest()</code> and
  445. <code>getLastResponse()</code> respectively.
  446. </para>
  447. </sect2>
  448. <sect2 id="zend.xmlrpc.client.http-client">
  449. <title>HTTP Client and Testing</title>
  450. <para>
  451. In all of the prior examples, an HTTP client was never specified.
  452. When this is the case, a new instance of
  453. <classname>Zend_Http_Client</classname> will be created with its default
  454. options and used by <classname>Zend_XmlRpc_Client</classname> automatically.
  455. </para>
  456. <para>
  457. The HTTP client can be retrieved at any time with the
  458. <code>getHttpClient()</code> method. For most cases, the default
  459. HTTP client will be sufficient. However, the
  460. <code>setHttpClient()</code> method allows for a different HTTP
  461. client instance to be injected.
  462. </para>
  463. <para>
  464. The <code>setHttpClient()</code> is particularly useful for unit testing. When combined
  465. with the <classname>Zend_Http_Client_Adapter_Test</classname>, remote services can be mocked
  466. out for testing. See the unit tests for <classname>Zend_XmlRpc_Client</classname> for examples
  467. of how to do this.
  468. </para>
  469. </sect2>
  470. </sect1>
  471. <!--
  472. vim:se ts=4 sw=4 et:
  473. -->