Zend_Rest_Client.xml 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.rest.client">
  4. <title>Zend_Rest_Client</title>
  5. <sect2 id="zend.rest.client.introduction">
  6. <title>Introduction</title>
  7. <para>
  8. Using the <classname>Zend_Rest_Client</classname> is very similar to using
  9. <code>SoapClient</code> objects (<ulink
  10. url="http://www.php.net/soap">SOAP web service extension</ulink>).
  11. You can simply call the REST service procedures as
  12. <classname>Zend_Rest_Client</classname> methods. Specify the service's full
  13. address in the <classname>Zend_Rest_Client</classname> constructor.
  14. </para>
  15. <example id="zend.rest.client.introduction.example-1">
  16. <title>A basic REST request</title>
  17. <programlisting language="php"><![CDATA[
  18. /**
  19. * Connect to framework.zend.com server and retrieve a greeting
  20. */
  21. $client = new Zend_Rest_Client('http://framework.zend.com/rest');
  22. echo $client->sayHello('Davey', 'Day')->get(); // "Hello Davey, Good Day"
  23. ]]></programlisting>
  24. </example>
  25. <note>
  26. <title>Differences in calling</title>
  27. <para>
  28. <classname>Zend_Rest_Client</classname> attempts to make remote methods
  29. look as much like native methods as possible, the only
  30. difference being that you must follow the method call with one
  31. of either <methodname>get()</methodname>, <methodname>post()</methodname>,
  32. <methodname>put()</methodname> or <methodname>delete()</methodname>. This call may
  33. be made via method chaining or in separate method calls:
  34. </para>
  35. <programlisting language="php"><![CDATA[
  36. $client->sayHello('Davey', 'Day');
  37. echo $client->get();
  38. ]]></programlisting>
  39. </note>
  40. </sect2>
  41. <sect2 id="zend.rest.client.return">
  42. <title>Responses</title>
  43. <para>
  44. All requests made using <classname>Zend_Rest_Client</classname> return a
  45. <classname>Zend_Rest_Client_Response</classname> object. This object has many
  46. properties that make it easier to access the results.
  47. </para>
  48. <para>
  49. When the service is based on <classname>Zend_Rest_Server</classname>,
  50. <classname>Zend_Rest_Client</classname> can make several assumptions about the
  51. response, including response status (success or failure) and return
  52. type.
  53. </para>
  54. <example id="zend.rest.client.return.example-1">
  55. <title>Response Status</title>
  56. <programlisting language="php"><![CDATA[
  57. $result = $client->sayHello('Davey', 'Day')->get();
  58. if ($result->isSuccess()) {
  59. echo $result; // "Hello Davey, Good Day"
  60. }
  61. ]]></programlisting>
  62. </example>
  63. <para>
  64. In the example above, you can see that we use the request result as
  65. an object, to call <methodname>isSuccess()</methodname>, and then because of
  66. <methodname>__toString()</methodname>, we can simply <code>echo</code> the
  67. object to get the result. <classname>Zend_Rest_Client_Response</classname>
  68. will allow you to echo any scalar value. For complex types, you can
  69. use either array or object notation.
  70. </para>
  71. <para>
  72. If however, you wish to query a service not using
  73. <classname>Zend_Rest_Server</classname> the
  74. <classname>Zend_Rest_Client_Response</classname> object will behave more like
  75. a <code>SimpleXMLElement</code>. However, to make things easier, it
  76. will automatically query the <acronym>XML</acronym> using XPath if the property is not
  77. a direct descendant of the document root element. Additionally, if
  78. you access a property as a method, you will receive the <acronym>PHP</acronym> value
  79. for the object, or an array of <acronym>PHP</acronym> value results.
  80. </para>
  81. <example id="zend.rest.client.return.example-2">
  82. <title>Using Technorati's Rest Service</title>
  83. <programlisting language="php"><![CDATA[
  84. $technorati = new Zend_Rest_Client('http://api.technorati.com/bloginfo');
  85. $technorati->key($key);
  86. $technorati->url('http://pixelated-dreams.com');
  87. $result = $technorati->get();
  88. echo $result->firstname() .' '. $result->lastname();
  89. ]]></programlisting>
  90. </example>
  91. <example id="zend.rest.client.return.example-3">
  92. <title>Example Technorati Response</title>
  93. <programlisting language="xml"><![CDATA[
  94. <?xml version="1.0" encoding="utf-8"?>
  95. <!-- generator="Technorati API version 1.0 /bloginfo" -->
  96. <!DOCTYPE tapi PUBLIC "-//Technorati, Inc.//DTD TAPI 0.02//EN"
  97. "http://api.technorati.com/dtd/tapi-002.xml">
  98. <tapi version="1.0">
  99. <document>
  100. <result>
  101. <url>http://pixelated-dreams.com</url>
  102. <weblog>
  103. <name>Pixelated Dreams</name>
  104. <url>http://pixelated-dreams.com</url>
  105. <author>
  106. <username>DShafik</username>
  107. <firstname>Davey</firstname>
  108. <lastname>Shafik</lastname>
  109. </author>
  110. <rssurl>
  111. http://pixelated-dreams.com/feeds/index.rss2
  112. </rssurl>
  113. <atomurl>
  114. http://pixelated-dreams.com/feeds/atom.xml
  115. </atomurl>
  116. <inboundblogs>44</inboundblogs>
  117. <inboundlinks>218</inboundlinks>
  118. <lastupdate>2006-04-26 04:36:36 GMT</lastupdate>
  119. <rank>60635</rank>
  120. </weblog>
  121. <inboundblogs>44</inboundblogs>
  122. <inboundlinks>218</inboundlinks>
  123. </result>
  124. </document>
  125. </tapi>
  126. ]]></programlisting>
  127. </example>
  128. <para>
  129. Here we are accessing the <code>firstname</code> and
  130. <code>lastname</code> properties. Even though these are not
  131. top-level elements, they are automatically returned when accessed by
  132. name.
  133. </para>
  134. <note>
  135. <title>Multiple items</title>
  136. <para>
  137. If multiple items are found when accessing a value by name, an
  138. array of SimpleXMLElements will be returned; accessing via
  139. method notation will return an array of <acronym>PHP</acronym> values.
  140. </para>
  141. </note>
  142. </sect2>
  143. <sect2 id="zend.rest.client.args">
  144. <title>Request Arguments</title>
  145. <para>
  146. Unless you are making a request to a <classname>Zend_Rest_Server</classname>
  147. based service, chances are you will need to send multiple arguments
  148. with your request. This is done by calling a method with the name of
  149. the argument, passing in the value as the first (and only) argument.
  150. Each of these method calls returns the object itself, allowing for
  151. chaining, or "fluent" usage. The first call, or the first argument
  152. if you pass in more than one argument, is always assumed to be the
  153. method when calling a <classname>Zend_Rest_Server</classname> service.
  154. </para>
  155. <example id="zend.rest.client.args.example-1">
  156. <title>Setting Request Arguments</title>
  157. <programlisting language="php"><![CDATA[
  158. $client = new Zend_Rest_Client('http://example.org/rest');
  159. $client->arg('value1');
  160. $client->arg2('value2');
  161. $client->get();
  162. // or
  163. $client->arg('value1')->arg2('value2')->get();
  164. ]]></programlisting>
  165. </example>
  166. <para>
  167. Both of the methods in the example above, will result in the
  168. following get args:
  169. <code>?method=arg&amp;arg1=value1&amp;arg=value1&amp;arg2=value2</code>
  170. </para>
  171. <para>
  172. You will notice that the first call of
  173. <code>$client->arg('value1');</code> resulted in both
  174. <code>method=arg&amp;arg1=value1</code> and <code>arg=value1</code>;
  175. this is so that <classname>Zend_Rest_Server</classname> can understand the
  176. request properly, rather than requiring pre-existing knowledge of
  177. the service.
  178. </para>
  179. <warning>
  180. <title>Strictness of Zend_Rest_Client</title>
  181. <para>
  182. Any REST service that is strict about the arguments it receives will likely fail
  183. using <classname>Zend_Rest_Client</classname>, because of the behavior described
  184. above. This is not a common practice and should not cause problems.
  185. </para>
  186. </warning>
  187. </sect2>
  188. </sect1>
  189. <!--
  190. vim:se ts=4 sw=4 et:
  191. -->