Zend_XmlRpc_Client.xml 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- EN-Revision: 15103 -->
  3. <!-- Reviewed: no -->
  4. <sect1 id="zend.xmlrpc.client">
  5. <title>Zend_XmlRpc_Client</title>
  6. <sect2 id="zend.xmlrpc.client.introduction">
  7. <title>Introdución</title>
  8. <para>
  9. Zend Framework provee soporte para consumo remoto para servicios XML-RPC
  10. como un cliente en el paquete <classname>Zend_XmlRpc_Client</classname>
  11. . Su mejor característica es la conversión automática de tipos
  12. entre PHP y XML-RPC, un servidor de objeto proxy, y acceso a
  13. capacidades de instrospección del servidor.
  14. </para>
  15. </sect2>
  16. <sect2 id="zend.xmlrpc.client.method-calls">
  17. <title>Method Calls</title>
  18. <para>
  19. El constructor de <classname>Zend_XmlRpc_Client</classname> recibe la
  20. URL del servidor XML-RPC como su primer parámetro.
  21. La nueva instacia devuelta puede ser usada para llamar cualquier número de métodos remotos en el punto final.
  22. </para>
  23. <para>
  24. Para llamar un método remoto con el cliente XML-RPC, instáncealo
  25. y usa el método de instancia <code>call()</code> . El código de ejemplo a continuación utiliza una demostración en el servidor XML-RPC en el sitio web de Zend Framework
  26. . Puede utilizarlo para probar o explorar los componentes
  27. <code>Zend_XmlRpc</code>.
  28. </para>
  29. <example id="zend.xmlrpc.client.method-calls.example-1">
  30. <title>XML-RPC Method Call</title>
  31. <programlisting role="php"><![CDATA[
  32. $client = new Zend_XmlRpc_Client('http://framework.zend.com/xmlrpc');
  33. echo $client->call('test.sayHello');
  34. // hello
  35. ]]></programlisting>
  36. </example>
  37. <para>
  38. El valor XML-RPC devuelto desde la llamada al método remoto automáticamente será convertida al tipo nativo PHP equivalente
  39. . En el ejemplo anterior, es devuelto un <code>string</code> PHP
  40. y está listo para ser usado inmediatamente.
  41. </para>
  42. <para>
  43. El primer parámetro del método <code>call()</code> recibe el nombre del método remoto que llamar. Si el método remoto requiere algún parámetro, éste puede ser enviado por el suministro de un segundo, parámetro opcional a <code>call()</code> con un <code>array</code> de
  44. valores para pasar el método remoto:
  45. </para>
  46. <example id="zend.xmlrpc.client.method-calls.example-2">
  47. <title>XML-RPC Method Call with Parameters</title>
  48. <programlisting role="php"><![CDATA[
  49. $client = new Zend_XmlRpc_Client('http://framework.zend.com/xmlrpc');
  50. $arg1 = 1.1;
  51. $arg2 = 'foo';
  52. $result = $client->call('test.sayHello', array($arg1, $arg2));
  53. // $result es un tipo nativo PHP
  54. ]]></programlisting>
  55. </example>
  56. <para>
  57. si el método remoto no requiere parámetros, este parámetro opcional
  58. podrá ser excluido o se puede pasar un <code>array()</code>
  59. vacío. El array de parámeters para el método repoto puede contener tipos nativos PHPs, objetos <code>Zend_XmlRpc_Value</code>
  60. , o una combinación de estos.
  61. </para>
  62. <para>
  63. El método <code>call()</code> convertirá automáticamente la respuesta
  64. XML-RPC y devolverá su tipo nativo PHP equivalente. Un objeto
  65. <code>Zend_XmlRpc_Response</code> para el valor devuelto también estará
  66. disponible para llamar el método <code>getLastResponse()</code>
  67. después de la llamada.
  68. </para>
  69. </sect2>
  70. <sect2 id="zend.xmlrpc.value.parameters">
  71. <title>Tipos y Conversiones</title>
  72. <para>
  73. Algunas llamadas a métodos remoto requieren parámetros. Éstos son
  74. dados al método <code>call()</code> de <classname>Zend_XmlRpc_Client</classname>
  75. como un array en el segundo parámetro. Cada parámetro puede ser dado
  76. como un tipo nativo PHP, que será convertido automáticamente,
  77. o como un objeto que representa un tipo específico de XML-RPC
  78. (uno de los objetos <code>Zend_XmlRpc_Value</code>).
  79. </para>
  80. <sect3 id="zend.xmlrpc.value.parameters.php-native">
  81. <title>Tipos Nativos PHP como Parámetro</title>
  82. <para>
  83. Los parámetros pueden ser pasados a <code>call()</code> como variables
  84. PHP nativas, ya sea un <code>string</code>,
  85. <code>integer</code>, <code>float</code>,
  86. <code>boolean</code>, <code>array</code>, o un
  87. <code>object</code>. En este caso, cada tipo PHP nativo será
  88. autodetectado y convertido en uno de los tipos XML-RPC
  89. de acuerdo con esta tabla:
  90. </para>
  91. <table id="zend.xmlrpc.value.parameters.php-native.table-1">
  92. <title>Tipos de Conversión entre PHP y XML-RPC</title>
  93. <tgroup cols="2">
  94. <thead>
  95. <row>
  96. <entry>Tipo Nativo PHP</entry>
  97. <entry>Tipo XML-RPC</entry>
  98. </row>
  99. </thead>
  100. <tbody>
  101. <row>
  102. <entry>integer</entry>
  103. <entry>int</entry>
  104. </row>
  105. <row>
  106. <entry>double</entry>
  107. <entry>double</entry>
  108. </row>
  109. <row>
  110. <entry>boolean</entry>
  111. <entry>boolean</entry>
  112. </row>
  113. <row>
  114. <entry>string</entry>
  115. <entry>string</entry>
  116. </row>
  117. <row>
  118. <entry>array</entry>
  119. <entry>array</entry>
  120. </row>
  121. <row>
  122. <entry>array asociativo</entry>
  123. <entry>struct</entry>
  124. </row>
  125. <row>
  126. <entry>object</entry>
  127. <entry>array</entry>
  128. </row>
  129. </tbody>
  130. </tgroup>
  131. </table>
  132. <note>
  133. <title>¿A qué tipo se convierten los arrays Vacios?</title>
  134. <para>
  135. Pasar un array vacío a un método XML-RPC es problemático,
  136. as it could represent either an array or a struct.
  137. <classname>Zend_XmlRpc_Client</classname> detects such conditions and
  138. makes a request to the server's
  139. <code>system.methodSignature</code> method to determine the
  140. appropriate XML-RPC type to cast to.
  141. </para>
  142. <para>
  143. However, this in itself can lead to issues. First off,
  144. servers that do not support
  145. <code>system.methodSignature</code> will log failed
  146. requests, and <classname>Zend_XmlRpc_Client</classname> will resort to
  147. casting the value to an XML-RPC array type. Additionally,
  148. this means that any call with array arguments will result in
  149. an additional call to the remote server.
  150. </para>
  151. <para>
  152. To disable the lookup entirely, you can call the
  153. <code>setSkipSystemLookup()</code> method prior to making
  154. your XML-RPC call:
  155. </para>
  156. <programlisting role="php"><![CDATA[
  157. $client->setSkipSystemLookup(true);
  158. $result = $client->call('foo.bar', array(array()));
  159. ]]></programlisting>
  160. </note>
  161. </sect3>
  162. <sect3 id="zend.xmlrpc.value.parameters.xmlrpc-value">
  163. <title><code>Zend_XmlRpc_Value</code> Objects as Parameters</title>
  164. <para>
  165. Parameters may also be created as <code>Zend_XmlRpc_Value</code>
  166. instances to specify an exact XML-RPC type. The primary reasons
  167. for doing this are:
  168. <itemizedlist>
  169. <listitem>
  170. <para>
  171. When you want to make sure the correct parameter
  172. type is passed to the procedure (i.e. the
  173. procedure requires an integer and you may get it
  174. from a database as a string)
  175. </para>
  176. </listitem>
  177. <listitem>
  178. <para>
  179. When the procedure requires <code>base64</code> or
  180. <code>dateTime.iso8601</code> type (which doesn't exists as a
  181. PHP native type)
  182. </para>
  183. </listitem>
  184. <listitem>
  185. <para>
  186. When auto-conversion may fail (i.e. you want to
  187. pass an empty XML-RPC struct as a parameter. Empty
  188. structs are represented as empty arrays in PHP
  189. but, if you give an empty array as a parameter it
  190. will be auto-converted to an XML-RPC array since
  191. it's not an associative array)
  192. </para>
  193. </listitem>
  194. </itemizedlist>
  195. </para>
  196. <para>
  197. There are two ways to create a <code>Zend_XmlRpc_Value</code>
  198. object: instantiate one of the <code>Zend_XmlRpc_Value</code>
  199. subclasses directly, or use the static factory method
  200. <code>Zend_XmlRpc_Value::getXmlRpcValue()</code>.
  201. </para>
  202. <table id="zend.xmlrpc.value.parameters.xmlrpc-value.table-1">
  203. <title><code>Zend_XmlRpc_Value</code> Objects for XML-RPC Types</title>
  204. <tgroup cols="3">
  205. <thead>
  206. <row>
  207. <entry>XML-RPC Type</entry>
  208. <entry><code>Zend_XmlRpc_Value</code> Constant</entry>
  209. <entry><code>Zend_XmlRpc_Value</code> Object</entry>
  210. </row>
  211. </thead>
  212. <tbody>
  213. <row>
  214. <entry>int</entry>
  215. <entry><code>Zend_XmlRpc_Value::XMLRPC_TYPE_INTEGER</code></entry>
  216. <entry><code>Zend_XmlRpc_Value_Integer</code></entry>
  217. </row>
  218. <row>
  219. <entry>double</entry>
  220. <entry><code>Zend_XmlRpc_Value::XMLRPC_TYPE_DOUBLE</code></entry>
  221. <entry><code>Zend_XmlRpc_Value_Double</code></entry>
  222. </row>
  223. <row>
  224. <entry>boolean</entry>
  225. <entry><code>Zend_XmlRpc_Value::XMLRPC_TYPE_BOOLEAN</code></entry>
  226. <entry><code>Zend_XmlRpc_Value_Boolean</code></entry>
  227. </row>
  228. <row>
  229. <entry>string</entry>
  230. <entry><code>Zend_XmlRpc_Value::XMLRPC_TYPE_STRING</code></entry>
  231. <entry><code>Zend_XmlRpc_Value_String</code></entry>
  232. </row>
  233. <row>
  234. <entry>base64</entry>
  235. <entry><code>Zend_XmlRpc_Value::XMLRPC_TYPE_BASE64</code></entry>
  236. <entry><code>Zend_XmlRpc_Value_Base64</code></entry>
  237. </row>
  238. <row>
  239. <entry>dateTime.iso8601</entry>
  240. <entry><code>Zend_XmlRpc_Value::XMLRPC_TYPE_DATETIME</code></entry>
  241. <entry><code>Zend_XmlRpc_Value_DateTime</code></entry>
  242. </row>
  243. <row>
  244. <entry>array</entry>
  245. <entry><code>Zend_XmlRpc_Value::XMLRPC_TYPE_ARRAY</code></entry>
  246. <entry><code>Zend_XmlRpc_Value_Array</code></entry>
  247. </row>
  248. <row>
  249. <entry>struct</entry>
  250. <entry><code>Zend_XmlRpc_Value::XMLRPC_TYPE_STRUCT</code></entry>
  251. <entry><code>Zend_XmlRpc_Value_Struct</code></entry>
  252. </row>
  253. </tbody>
  254. </tgroup>
  255. </table>
  256. <para>
  257. <note>
  258. <title>Automatic Conversion</title>
  259. <para>
  260. When building a new <code>Zend_XmlRpc_Value</code>
  261. object, its value is set by a PHP type. The PHP type
  262. will be converted to the specified type using
  263. PHP casting. For example, if a string is given as a
  264. value to the <code>Zend_XmlRpc_Value_Integer</code>
  265. object, it will be converted using
  266. <code>(int)$value</code>.
  267. </para>
  268. </note>
  269. </para>
  270. </sect3>
  271. </sect2>
  272. <sect2 id="zend.xmlrpc.client.requests-and-responses">
  273. <title>Server Proxy Object</title>
  274. <para>
  275. Another way to call remote methods with the XML-RPC client is to
  276. use the server proxy. This is a PHP object that proxies a remote
  277. XML-RPC namespace, making it work as close to a native PHP object
  278. as possible.
  279. </para>
  280. <para>
  281. To instantiate a server proxy, call the <code>getProxy()</code>
  282. instance method of <classname>Zend_XmlRpc_Client</classname>. This will
  283. return an instance of <code>Zend_XmlRpc_Client_ServerProxy</code>.
  284. Any method call on the server proxy object will be forwarded to
  285. the remote, and parameters may be passed like any other PHP
  286. method.
  287. </para>
  288. <example id="zend.xmlrpc.client.requests-and-responses.example-1">
  289. <title>Proxy the Default Namespace</title>
  290. <programlisting role="php"><![CDATA[
  291. $client = new Zend_XmlRpc_Client('http://framework.zend.com/xmlrpc');
  292. $server = $client->getProxy(); // Proxy the default namespace
  293. $hello = $server->test->sayHello(1, 2); // test.Hello(1, 2) returns "hello"
  294. ]]></programlisting>
  295. </example>
  296. <para>
  297. The <code>getProxy()</code> method receives an optional argument
  298. specifying which namespace of the remote server to proxy. If it
  299. does not receive a namespace, the default namespace will be
  300. proxied. In the next example, the <code>test</code> namespace
  301. will be proxied:
  302. </para>
  303. <example id="zend.xmlrpc.client.requests-and-responses.example-2">
  304. <title>Proxy Any Namespace</title>
  305. <programlisting role="php"><![CDATA[
  306. $client = new Zend_XmlRpc_Client('http://framework.zend.com/xmlrpc');
  307. $test = $client->getProxy('test'); // Proxy the "test" namespace
  308. $hello = $test->sayHello(1, 2); // test.Hello(1,2) returns "hello"
  309. ]]></programlisting>
  310. </example>
  311. <para>
  312. If the remote server supports nested namespaces of any depth,
  313. these can also be used through the server proxy. For example, if
  314. the server in the example above had a method
  315. <code>test.foo.bar()</code>, it could be called as
  316. <code>$test-&gt;foo-&gt;bar()</code>.
  317. </para>
  318. </sect2>
  319. <sect2 id="zend.xmlrpc.client.error-handling">
  320. <title>Error Handling</title>
  321. <para>
  322. Two kinds of errors can occur during an XML-RPC method call: HTTP
  323. errors and XML-RPC faults. The <classname>Zend_XmlRpc_Client</classname>
  324. recognizes each and provides the ability to detect and trap them
  325. independently.
  326. </para>
  327. <sect3 id="zend.xmlrpc.client.error-handling.http">
  328. <title>HTTP Errors</title>
  329. <para>
  330. If any HTTP error occurs, such as the remote HTTP server
  331. returns a <code>404 Not Found</code>, a
  332. <code>Zend_XmlRpc_Client_HttpException</code> will be thrown.
  333. </para>
  334. <example id="zend.xmlrpc.client.error-handling.http.example-1">
  335. <title>Handling HTTP Errors</title>
  336. <programlisting role="php"><![CDATA[
  337. $client = new Zend_XmlRpc_Client('http://foo/404');
  338. try {
  339. $client->call('bar', array($arg1, $arg2));
  340. } catch (Zend_XmlRpc_Client_HttpException $e) {
  341. // $e->getCode() returns 404
  342. // $e->getMessage() returns "Not Found"
  343. }
  344. ]]></programlisting>
  345. </example>
  346. <para>
  347. Regardless of how the XML-RPC client is used, the
  348. <code>Zend_XmlRpc_Client_HttpException</code> will be thrown
  349. whenever an HTTP error occurs.
  350. </para>
  351. </sect3>
  352. <sect3 id="zend.xmlrpc.client.error-handling.faults">
  353. <title>XML-RPC Faults</title>
  354. <para>
  355. An XML-RPC fault is analogous to a PHP exception. It is a
  356. special type returned from an XML-RPC method call that has
  357. both an error code and an error message. XML-RPC faults are
  358. handled differently depending on the context of how the
  359. <classname>Zend_XmlRpc_Client</classname> is used.
  360. </para>
  361. <para>
  362. When the <code>call()</code> method or the server
  363. proxy object is used, an XML-RPC fault will result in a
  364. <code>Zend_XmlRpc_Client_FaultException</code> being thrown.
  365. The code and message of the exception will map directly to
  366. their respective values in the original XML-RPC fault
  367. response.
  368. </para>
  369. <example id="zend.xmlrpc.client.error-handling.faults.example-1">
  370. <title>Handling XML-RPC Faults</title>
  371. <programlisting role="php"><![CDATA[
  372. $client = new Zend_XmlRpc_Client('http://framework.zend.com/xmlrpc');
  373. try {
  374. $client->call('badMethod');
  375. } catch (Zend_XmlRpc_Client_FaultException $e) {
  376. // $e->getCode() returns 1
  377. // $e->getMessage() returns "Unknown method"
  378. }
  379. ]]></programlisting>
  380. </example>
  381. <para>
  382. Cuando el método <code>call()</code> es usado para realizar la
  383. petición, <code>Zend_XmlRpc_Client_FaultException</code> será
  384. lanzado como error. Un objeto <code>Zend_XmlRpc_Response</code> conteniendo
  385. el error estará disponible llamando a
  386. <code>getLastResponse()</code>.
  387. </para>
  388. <para>
  389. Cuando el método <code>doRequest()</code> sea usado para realizar una
  390. petición, no lanzará una excepción. En vez de eso, devolverá un objeto
  391. <code>Zend_XmlRpc_Response</code> que contendrá el error.
  392. Esto puede comprobarse con
  393. <code>isFault()</code> método instancia de
  394. <code>Zend_XmlRpc_Response</code>.
  395. </para>
  396. </sect3>
  397. </sect2>
  398. <sect2 id="zend.xmlrpc.client.introspection">
  399. <title>Server Introspection</title>
  400. <para>
  401. Some XML-RPC servers support the de facto introspection methods under the XML-RPC
  402. <code>system.</code> namespace. <classname>Zend_XmlRpc_Client</classname> provides special
  403. support for servers with these capabilities.
  404. </para>
  405. <para>
  406. A <code>Zend_XmlRpc_Client_ServerIntrospection</code> instance may be retrieved by calling
  407. the <code>getIntrospector()</code> method of <code>Zend_XmlRpcClient</code>. It can
  408. then be used to perform introspection operations on the server.
  409. </para>
  410. </sect2>
  411. <sect2 id="zend.xmlrpc.client.request-to-response">
  412. <title>From Request to Response</title>
  413. <para>
  414. Under the hood, the <code>call()</code> instance method of <classname>Zend_XmlRpc_Client</classname>
  415. builds a request object (<code>Zend_XmlRpc_Request</code>) and sends it to another method,
  416. <code>doRequest()</code>, that returns a response object (<code>Zend_XmlRpc_Response</code>).
  417. </para>
  418. <para>
  419. The <code>doRequest()</code> method is also available for use directly:
  420. </para>
  421. <example id="zend.xmlrpc.client.request-to-response.example-1">
  422. <title>Processing Request to Response</title>
  423. <programlisting role="php"><![CDATA[
  424. $client = new Zend_XmlRpc_Client('http://framework.zend.com/xmlrpc');
  425. $request = new Zend_XmlRpc_Request();
  426. $request->setMethod('test.sayHello');
  427. $request->setParams(array('foo', 'bar'));
  428. $client->doRequest($request);
  429. // $server->getLastRequest() returns instanceof Zend_XmlRpc_Request
  430. // $server->getLastResponse() returns instanceof Zend_XmlRpc_Response
  431. ]]></programlisting>
  432. </example>
  433. <para>
  434. Whenever an XML-RPC method call is made by the client through any
  435. means, either the <code>call()</code> method,
  436. <code>doRequest()</code> method, or server proxy, the last request
  437. object and its resultant response object will always be available
  438. through the methods <code>getLastRequest()</code> and
  439. <code>getLastResponse()</code> respectively.
  440. </para>
  441. </sect2>
  442. <sect2 id="zend.xmlrpc.client.http-client">
  443. <title>HTTP Client and Testing</title>
  444. <para>
  445. In all of the prior examples, an HTTP client was never specified.
  446. When this is the case, a new instance of
  447. <code>Zend_Http_Client</code> will be created with its default
  448. options and used by <classname>Zend_XmlRpc_Client</classname> automatically.
  449. </para>
  450. <para>
  451. The HTTP client can be retrieved at any time with the
  452. <code>getHttpClient()</code> method. For most cases, the default
  453. HTTP client will be sufficient. However, the
  454. <code>setHttpClient()</code> method allows for a different HTTP
  455. client instance to be injected.
  456. </para>
  457. <para>
  458. The <code>setHttpClient()</code> is particularly useful for unit testing. When combined
  459. with the <classname>Zend_Http_Client_Adapter_Test</classname>, remote services can be mocked
  460. out for testing. See the unit tests for <classname>Zend_XmlRpc_Client</classname> for examples
  461. of how to do this.
  462. </para>
  463. </sect2>
  464. </sect1>
  465. <!--
  466. vim:se ts=4 sw=4 et:
  467. -->