Zend_XmlRpc_Client.xml 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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 language="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 <type>String</type> 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 <type>Array</type> 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 language="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 <type>String</type>,
  91. <code>integer</code>, <code>float</code>,
  92. <type>Boolean</type>, <type>Array</type>, 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 language="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>
  222. <classname>Zend_XmlRpc_Value::XMLRPC_TYPE_INTEGER</classname>
  223. </entry>
  224. <entry><classname>Zend_XmlRpc_Value_Integer</classname></entry>
  225. </row>
  226. <row>
  227. <entry>double</entry>
  228. <entry>
  229. <classname>Zend_XmlRpc_Value::XMLRPC_TYPE_DOUBLE</classname>
  230. </entry>
  231. <entry><classname>Zend_XmlRpc_Value_Double</classname></entry>
  232. </row>
  233. <row>
  234. <entry>boolean</entry>
  235. <entry>
  236. <classname>Zend_XmlRpc_Value::XMLRPC_TYPE_BOOLEAN</classname>
  237. </entry>
  238. <entry><classname>Zend_XmlRpc_Value_Boolean</classname></entry>
  239. </row>
  240. <row>
  241. <entry>string</entry>
  242. <entry>
  243. <classname>Zend_XmlRpc_Value::XMLRPC_TYPE_STRING</classname>
  244. </entry>
  245. <entry><classname>Zend_XmlRpc_Value_String</classname></entry>
  246. </row>
  247. <row>
  248. <entry>base64</entry>
  249. <entry>
  250. <classname>Zend_XmlRpc_Value::XMLRPC_TYPE_BASE64</classname>
  251. </entry>
  252. <entry><classname>Zend_XmlRpc_Value_Base64</classname></entry>
  253. </row>
  254. <row>
  255. <entry>dateTime.iso8601</entry>
  256. <entry>
  257. <classname>Zend_XmlRpc_Value::XMLRPC_TYPE_DATETIME</classname>
  258. </entry>
  259. <entry><classname>Zend_XmlRpc_Value_DateTime</classname></entry>
  260. </row>
  261. <row>
  262. <entry>array</entry>
  263. <entry>
  264. <classname>Zend_XmlRpc_Value::XMLRPC_TYPE_ARRAY</classname>
  265. </entry>
  266. <entry><classname>Zend_XmlRpc_Value_Array</classname></entry>
  267. </row>
  268. <row>
  269. <entry>struct</entry>
  270. <entry>
  271. <classname>Zend_XmlRpc_Value::XMLRPC_TYPE_STRUCT</classname>
  272. </entry>
  273. <entry><classname>Zend_XmlRpc_Value_Struct</classname></entry>
  274. </row>
  275. </tbody>
  276. </tgroup>
  277. </table>
  278. <para>
  279. <note>
  280. <title>Automatic Conversion</title>
  281. <para>
  282. When building a new <classname>Zend_XmlRpc_Value</classname>
  283. object, its value is set by a PHP type. The PHP type
  284. will be converted to the specified type using
  285. PHP casting. For example, if a string is given as a
  286. value to the <classname>Zend_XmlRpc_Value_Integer</classname>
  287. object, it will be converted using
  288. <code>(int)$value</code>.
  289. </para>
  290. </note>
  291. </para>
  292. </sect3>
  293. </sect2>
  294. <sect2 id="zend.xmlrpc.client.requests-and-responses">
  295. <title>Server Proxy Object</title>
  296. <para>
  297. Another way to call remote methods with the XML-RPC client is to
  298. use the server proxy. This is a PHP object that proxies a remote
  299. XML-RPC namespace, making it work as close to a native PHP object
  300. as possible.
  301. </para>
  302. <para>
  303. To instantiate a server proxy, call the <code>getProxy()</code>
  304. instance method of <classname>Zend_XmlRpc_Client</classname>. This will
  305. return an instance of <classname>Zend_XmlRpc_Client_ServerProxy</classname>.
  306. Any method call on the server proxy object will be forwarded to
  307. the remote, and parameters may be passed like any other PHP
  308. method.
  309. </para>
  310. <example id="zend.xmlrpc.client.requests-and-responses.example-1">
  311. <title>Proxy the Default Namespace</title>
  312. <programlisting language="php"><![CDATA[
  313. $client = new Zend_XmlRpc_Client('http://framework.zend.com/xmlrpc');
  314. $server = $client->getProxy(); // Proxy the default namespace
  315. $hello = $server->test->sayHello(1, 2); // test.Hello(1, 2) returns "hello"
  316. ]]></programlisting>
  317. </example>
  318. <para>
  319. The <code>getProxy()</code> method receives an optional argument
  320. specifying which namespace of the remote server to proxy. If it
  321. does not receive a namespace, the default namespace will be
  322. proxied. In the next example, the <code>test</code> namespace
  323. will be proxied:
  324. </para>
  325. <example id="zend.xmlrpc.client.requests-and-responses.example-2">
  326. <title>Proxy Any Namespace</title>
  327. <programlisting language="php"><![CDATA[
  328. $client = new Zend_XmlRpc_Client('http://framework.zend.com/xmlrpc');
  329. $test = $client->getProxy('test'); // Proxy the "test" namespace
  330. $hello = $test->sayHello(1, 2); // test.Hello(1,2) returns "hello"
  331. ]]></programlisting>
  332. </example>
  333. <para>
  334. If the remote server supports nested namespaces of any depth,
  335. these can also be used through the server proxy. For example, if
  336. the server in the example above had a method
  337. <code>test.foo.bar()</code>, it could be called as
  338. <code>$test->foo->bar()</code>.
  339. </para>
  340. </sect2>
  341. <sect2 id="zend.xmlrpc.client.error-handling">
  342. <title>Error Handling</title>
  343. <para>
  344. Two kinds of errors can occur during an XML-RPC method call: HTTP
  345. errors and XML-RPC faults. The <classname>Zend_XmlRpc_Client</classname>
  346. recognizes each and provides the ability to detect and trap them
  347. independently.
  348. </para>
  349. <sect3 id="zend.xmlrpc.client.error-handling.http">
  350. <title>HTTP Errors</title>
  351. <para>
  352. If any HTTP error occurs, such as the remote HTTP server
  353. returns a <code>404 Not Found</code>, a
  354. <classname>Zend_XmlRpc_Client_HttpException</classname> will be thrown.
  355. </para>
  356. <example id="zend.xmlrpc.client.error-handling.http.example-1">
  357. <title>Handling HTTP Errors</title>
  358. <programlisting language="php"><![CDATA[
  359. $client = new Zend_XmlRpc_Client('http://foo/404');
  360. try {
  361. $client->call('bar', array($arg1, $arg2));
  362. } catch (Zend_XmlRpc_Client_HttpException $e) {
  363. // $e->getCode() returns 404
  364. // $e->getMessage() returns "Not Found"
  365. }
  366. ]]></programlisting>
  367. </example>
  368. <para>
  369. Regardless of how the XML-RPC client is used, the
  370. <classname>Zend_XmlRpc_Client_HttpException</classname> will be thrown
  371. whenever an HTTP error occurs.
  372. </para>
  373. </sect3>
  374. <sect3 id="zend.xmlrpc.client.error-handling.faults">
  375. <title>XML-RPC Faults</title>
  376. <para>
  377. An XML-RPC fault is analogous to a PHP exception. It is a
  378. special type returned from an XML-RPC method call that has
  379. both an error code and an error message. XML-RPC faults are
  380. handled differently depending on the context of how the
  381. <classname>Zend_XmlRpc_Client</classname> is used.
  382. </para>
  383. <para>
  384. When the <code>call()</code> method or the server
  385. proxy object is used, an XML-RPC fault will result in a
  386. <classname>Zend_XmlRpc_Client_FaultException</classname> being thrown.
  387. The code and message of the exception will map directly to
  388. their respective values in the original XML-RPC fault
  389. response.
  390. </para>
  391. <example id="zend.xmlrpc.client.error-handling.faults.example-1">
  392. <title>Handling XML-RPC Faults</title>
  393. <programlisting language="php"><![CDATA[
  394. $client = new Zend_XmlRpc_Client('http://framework.zend.com/xmlrpc');
  395. try {
  396. $client->call('badMethod');
  397. } catch (Zend_XmlRpc_Client_FaultException $e) {
  398. // $e->getCode() returns 1
  399. // $e->getMessage() returns "Unknown method"
  400. }
  401. ]]></programlisting>
  402. </example>
  403. <para>
  404. When the <code>call()</code> method is used to make the
  405. request, the <classname>Zend_XmlRpc_Client_FaultException</classname> will be
  406. thrown on fault. A <classname>Zend_XmlRpc_Response</classname> object
  407. containing the fault will also be available by calling
  408. <code>getLastResponse()</code>.
  409. </para>
  410. <para>
  411. When the <code>doRequest()</code> method is used to make the
  412. request, it will not throw the exception. Instead, it will
  413. return a <classname>Zend_XmlRpc_Response</classname> object returned
  414. will containing the fault. This can be checked with
  415. <code>isFault()</code> instance method of
  416. <classname>Zend_XmlRpc_Response</classname>.
  417. </para>
  418. </sect3>
  419. </sect2>
  420. <sect2 id="zend.xmlrpc.client.introspection">
  421. <title>Server Introspection</title>
  422. <para>
  423. Some XML-RPC servers support the de facto introspection methods under the XML-RPC
  424. <code>system.</code> namespace. <classname>Zend_XmlRpc_Client</classname> provides
  425. special support for servers with these capabilities.
  426. </para>
  427. <para>
  428. A <classname>Zend_XmlRpc_Client_ServerIntrospection</classname> instance may be
  429. retrieved by calling the <code>getIntrospector()</code> method of
  430. <classname>Zend_XmlRpcClient</classname>. It can then be used to perform introspection
  431. operations on the server.
  432. </para>
  433. </sect2>
  434. <sect2 id="zend.xmlrpc.client.request-to-response">
  435. <title>From Request to Response</title>
  436. <para>
  437. Under the hood, the <code>call()</code> instance method of
  438. <classname>Zend_XmlRpc_Client</classname> builds a request object
  439. (<classname>Zend_XmlRpc_Request</classname>) and sends it to another method,
  440. <code>doRequest()</code>, that returns a response object
  441. (<classname>Zend_XmlRpc_Response</classname>).
  442. </para>
  443. <para>
  444. The <code>doRequest()</code> method is also available for use directly:
  445. </para>
  446. <example id="zend.xmlrpc.client.request-to-response.example-1">
  447. <title>Processing Request to Response</title>
  448. <programlisting language="php"><![CDATA[
  449. $client = new Zend_XmlRpc_Client('http://framework.zend.com/xmlrpc');
  450. $request = new Zend_XmlRpc_Request();
  451. $request->setMethod('test.sayHello');
  452. $request->setParams(array('foo', 'bar'));
  453. $client->doRequest($request);
  454. // $server->getLastRequest() returns instanceof Zend_XmlRpc_Request
  455. // $server->getLastResponse() returns instanceof Zend_XmlRpc_Response
  456. ]]></programlisting>
  457. </example>
  458. <para>
  459. Whenever an XML-RPC method call is made by the client through any
  460. means, either the <code>call()</code> method,
  461. <code>doRequest()</code> method, or server proxy, the last request
  462. object and its resultant response object will always be available
  463. through the methods <code>getLastRequest()</code> and
  464. <code>getLastResponse()</code> respectively.
  465. </para>
  466. </sect2>
  467. <sect2 id="zend.xmlrpc.client.http-client">
  468. <title>HTTP Client and Testing</title>
  469. <para>
  470. In all of the prior examples, an HTTP client was never specified.
  471. When this is the case, a new instance of
  472. <classname>Zend_Http_Client</classname> will be created with its default
  473. options and used by <classname>Zend_XmlRpc_Client</classname> automatically.
  474. </para>
  475. <para>
  476. The HTTP client can be retrieved at any time with the
  477. <code>getHttpClient()</code> method. For most cases, the default
  478. HTTP client will be sufficient. However, the
  479. <code>setHttpClient()</code> method allows for a different HTTP
  480. client instance to be injected.
  481. </para>
  482. <para>
  483. The <code>setHttpClient()</code> is particularly useful for unit testing. When combined
  484. with the <classname>Zend_Http_Client_Adapter_Test</classname>, remote services can be
  485. mocked out for testing. See the unit tests for <classname>Zend_XmlRpc_Client</classname>
  486. for examples of how to do this.
  487. </para>
  488. </sect2>
  489. </sect1>
  490. <!--
  491. vim:se ts=4 sw=4 et:
  492. -->