Zend_XmlRpc_Client.xml 26 KB

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