admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
| 2 | <html>
|
| 3 | <head>
|
| 4 |
|
| 5 | <title>Code Igniter User Guide</title>
|
| 6 |
|
| 7 | <style type='text/css' media='all'>@import url('../userguide.css');</style>
|
| 8 | <link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
|
| 9 |
|
admin | 17a890d | 2006-09-27 20:42:42 +0000 | [diff] [blame] | 10 | <script type="text/javascript" src="../nav/nav.js"></script>
|
admin | 2296fc3 | 2006-09-27 21:07:02 +0000 | [diff] [blame] | 11 | <script type="text/javascript" src="../nav/prototype.lite.js"></script>
|
admin | 17a890d | 2006-09-27 20:42:42 +0000 | [diff] [blame] | 12 | <script type="text/javascript" src="../nav/moo.fx.js"></script>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 13 | <script type="text/javascript">
|
| 14 | window.onload = function() {
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 15 | myHeight = new fx.Height('nav', {duration: 400});
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 16 | myHeight.hide();
|
| 17 | }
|
| 18 | </script>
|
| 19 |
|
| 20 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
| 21 | <meta http-equiv='expires' content='-1' />
|
| 22 | <meta http-equiv= 'pragma' content='no-cache' />
|
| 23 | <meta name='robots' content='all' />
|
| 24 | <meta name='author' content='Rick Ellis' />
|
| 25 | <meta name='description' content='Code Igniter User Guide' />
|
| 26 |
|
| 27 | </head>
|
| 28 | <body>
|
| 29 |
|
| 30 | <!-- START NAVIGATION -->
|
| 31 | <div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
|
| 32 | <div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle.jpg" width="153" height="44" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
|
| 33 | <div id="masthead">
|
| 34 | <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
|
| 35 | <tr>
|
Derek Allard | f743c73 | 2007-02-14 01:36:46 +0000 | [diff] [blame^] | 36 | <td><h1>Code Igniter User Guide Version 1.5.2</h1></td>
|
admin | c0d5d52 | 2006-10-30 19:40:35 +0000 | [diff] [blame] | 37 | <td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 38 | </tr>
|
| 39 | </table>
|
| 40 | </div>
|
| 41 | <!-- END NAVIGATION -->
|
| 42 |
|
| 43 |
|
| 44 | <!-- START BREADCRUMB -->
|
| 45 | <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
|
| 46 | <tr>
|
| 47 | <td id="breadcrumb">
|
| 48 | <a href="http://www.codeigniter.com/">Code Igniter Home</a> ›
|
| 49 | <a href="../index.html">User Guide Home</a> ›
|
| 50 | XML-RPC and XML-RPC Server Classes
|
| 51 | </td>
|
| 52 | <td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="www.codeigniter.com/user_guide/" />Search User Guide <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" /> <input type="submit" class="submit" name="sa" value="Go" /></form></td>
|
| 53 | </tr>
|
| 54 | </table>
|
| 55 | <!-- END BREADCRUMB -->
|
| 56 |
|
| 57 | <br clear="all" />
|
| 58 |
|
| 59 |
|
| 60 | <!-- START CONTENT -->
|
| 61 | <div id="content">
|
| 62 |
|
| 63 |
|
| 64 | <h1>XML-RPC and XML-RPC Server Classes</h1>
|
| 65 |
|
| 66 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 67 | <p>Code Igniter's XML-RPC classes permit you to send requests to another server, or set up
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 68 | your own XML-RPC server to receive requests.</p>
|
| 69 |
|
| 70 |
|
| 71 | <h2>What is XML-RPC?</h2>
|
| 72 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 73 | <p>Quite simply it is a way for two computers to communicate over the internet using XML.
|
| 74 | One computer, which we will call the <dfn>client</dfn>, sends an XML-RPC <strong>request</strong> to
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 75 | another computer, which we will call the <dfn>server</dfn>. Once the server receives and processes the request it
|
| 76 | will send back a <strong>response</strong> to the client.</p>
|
| 77 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 78 | <p>For example, using the MetaWeblog API, an XML-RPC Client (usually a desktop publishing tool) will
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 79 | send a request to an XML-RPC Server running on your site. This request might be a new weblog entry
|
| 80 | being sent for publication, or it could be a request for an existing entry for editing.
|
| 81 |
|
| 82 | When the XML-RPC Server receives this request it will examine it to determine which class/method should be called to process the request.
|
| 83 | Once processed, the server will then send back a response message.</p>
|
| 84 |
|
admin | e7e1dcd | 2006-10-21 18:04:01 +0000 | [diff] [blame] | 85 | <p>For detailed specifications, you can visit the <a href="http://www.xmlrpc.com/">XML-RPC</a> site.</p>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 86 |
|
| 87 | <h2>Initializing the Class</h2>
|
| 88 |
|
| 89 | <p>Like most other classes in Code Igniter, the XML-RPC and XML-RPCS classes are initialized in your controller using the <dfn>$this->load->library</dfn> function:</p>
|
| 90 |
|
| 91 | <p>To load the XML-RPC class you will use:
|
| 92 | <code>$this->load->library('xmlrpc');</code>
|
| 93 | <p>Once loaded, the xml-rpc library object will be available using: <dfn>$this->xmlrpc</dfn></p>
|
| 94 |
|
| 95 | <p>To load the XML-RPC Server class you will use:
|
admin | e5bb936 | 2006-09-27 00:31:22 +0000 | [diff] [blame] | 96 | <code>
|
| 97 | $this->load->library('xmlrpc');<br />
|
| 98 | $this->load->library('xmlrpcs');
|
| 99 | </code>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 100 | <p>Once loaded, the xml-rpcs library object will be available using: <dfn>$this->xmlrpcs</dfn></p>
|
| 101 |
|
admin | e5bb936 | 2006-09-27 00:31:22 +0000 | [diff] [blame] | 102 | <p class="important"><strong>Note:</strong> When using the XML-RPC Sever class you must load BOTH the XML-RPC class and the XML-RPC Server class.</p>
|
| 103 |
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 104 |
|
| 105 |
|
| 106 | <h2>Sending XML-RPC Requests</h2>
|
| 107 |
|
| 108 | <p>To send a request to an XML-RPC server you must specify the following information:</p>
|
| 109 |
|
| 110 | <ul>
|
| 111 | <li>The URL of the server</li>
|
| 112 | <li>The method on the server you wish to call</li>
|
| 113 | <li>The <em>request</em> data (explained below).</li>
|
| 114 | </ul>
|
| 115 |
|
| 116 | <p>Here is a basic example that sends a simple Weblogs.com ping to the <a href="http://pingomatic.com/">Ping-o-Matic</a></p>
|
| 117 |
|
| 118 |
|
| 119 | <code>$this->load->library('xmlrpc');<br />
|
| 120 | <br />
|
| 121 | $this->xmlrpc->server('http://rpc.pingomatic.com/', 80);<br />
|
| 122 | $this->xmlrpc->method('weblogUpdates.ping');<br />
|
| 123 |
|
| 124 | <br />
|
| 125 | $request = array('My Photoblog', 'http://www.my-site.com/photoblog/');<br />
|
| 126 | $this->xmlrpc->request($request);<br />
|
| 127 | <br />
|
| 128 | if ( ! $this->xmlrpc->send_request())<br />
|
| 129 | {<br />
|
| 130 | echo $xmlrpc->display_error();<br />
|
| 131 | }
|
| 132 | </code>
|
| 133 |
|
| 134 | <h3>Explanation</h3>
|
| 135 |
|
| 136 | <p>The above code initializes the XML-RPC class, sets the server URL and method to be called (weblogUpdates.ping). The
|
| 137 | request (in this case, the title and URL of your site) is placed into an array for transportation, and
|
| 138 | compiled using the request() function.
|
| 139 | Lastly, the full request is sent. If the <dfn>send_request()</dfn> method returns false we will display the error message
|
| 140 | sent back from the XML-RPC Server.</p>
|
| 141 |
|
| 142 | <h2>Anatomy of a Request</h2>
|
| 143 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 144 | <p>An XML-RPC <dfn>request</dfn> is simply the data you are sending to the XML-RPC server. Each piece of data in a request
|
| 145 | is referred to as a <dfn>request parameter</dfn>. The above example has two parameters:
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 146 | The URL and title of your site. When the XML-RPC server receives your request, it will look for parameters it requires.</p>
|
| 147 |
|
admin | e7e1dcd | 2006-10-21 18:04:01 +0000 | [diff] [blame] | 148 | <p>Request parameters must be placed into an array for transportation, and each parameter can be one
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 149 | of seven data types (strings, numbers, dates, etc.). If your parameters are something other than strings
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 150 | you will have to include the data type in the request array.</p>
|
| 151 |
|
| 152 | <p>Here is an example of a simple array with three parameters:</p>
|
| 153 |
|
| 154 | <code>$request = array('John', 'Doe', 'www.some-site.com');<br />
|
| 155 | $this->xmlrpc->request($request);</code>
|
| 156 |
|
| 157 | <p>If you use data types other than strings, or if you have several different data types, you will place
|
| 158 | each parameter into its own array, with the data type in the second position:</p>
|
| 159 |
|
| 160 | <code>
|
| 161 | $request = array (<br />
|
| 162 | array('John', 'string'),<br />
|
| 163 | array('Doe', 'string'),<br />
|
| 164 | array(FALSE, 'boolean'),<br />
|
| 165 | array(12345, 'int')<br />
|
| 166 | );
|
| 167 | <br />
|
| 168 | $this->xmlrpc->request($request);</code>
|
| 169 |
|
| 170 | The <a href="#datatypes">Data Types</a> section below has a full list of data types.</p>
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 | <h2>Creating an XML-RPC Server</h2>
|
| 175 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 176 | <p>An XML-RPC Server acts as a traffic cop of sorts, waiting for incoming requests and redirecting them to the
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 177 | appropriate functions for processing.</p>
|
| 178 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 179 | <p>To create your own XML-RPC server involves initializing the XML-RPC Server class in your controller where you expect the incoming
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 180 | request to appear, then setting up an array with mapping instructions so that incoming requests can be sent to the appropriate
|
| 181 | class and method for processing.</p>
|
| 182 |
|
| 183 | <p>Here is an example to illustrate:</p>
|
| 184 |
|
admin | e5bb936 | 2006-09-27 00:31:22 +0000 | [diff] [blame] | 185 | <code>
|
| 186 | $this->load->library('xmlrpcs');<br />
|
| 187 | $this->load->library('xmlrpcs');<br />
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 188 | <br />
|
| 189 | $config['functions']['<var>new_post</var>']; = array('function' => '<dfn>My_blog.new_entry</dfn>');<br />
|
| 190 | $config['functions']['<var>update_post</var>'] = array('function' => '<dfn>My_blog.update_entry</dfn>');<br />
|
| 191 | <br />
|
| 192 | $this->xmlrpcs->initialize($config);<br />
|
| 193 | $this->xmlrpcs->serve();</code>
|
| 194 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 195 | <p>The above example contains an array specifying two method requests that the Server allows.
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 196 | The allowed methods are on the left side of the array. When either of those are received, they will be mapped to the class and method on the right.
|
| 197 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 198 | <p>In other words, if an XML-RPC Client sends a request for the <var>new_post</var> method, your
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 199 | server will load the <dfn>My_blog</dfn> class and call the <dfn>new_entry</dfn> function.
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 200 | If the request is for the <var>update_post</var> method, your
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 201 | server will load the <dfn>My_blog</dfn> class and call the <dfn>update_entry</dfn> function.</p>
|
| 202 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 203 | <p>The function names in the above example are arbitrary. You'll decide what they should be called on your server,
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 204 | or if you are using standardized APIs, like the Blogger or MetaWeblog API, you'll use their function names.</p>
|
| 205 |
|
| 206 |
|
| 207 | <h2>Processing Server Requests</h2>
|
| 208 |
|
| 209 | <p>When the XML-RPC Server receives a request and loads the class/method for processing, it will pass
|
| 210 | an object to that method containing the data sent by the client.</p>
|
| 211 |
|
| 212 | <p>Using the above example, if the <var>new_post</var> method is requested, the server will expect a class
|
| 213 | to exist with this prototype:</p>
|
| 214 |
|
| 215 | <code>class <kbd>My_blog</kbd> extends Controller {<br />
|
| 216 | <br />
|
| 217 | function <kbd>new_post</kbd>(<var>$request</var>)<br />
|
| 218 | {<br />
|
| 219 | <br />
|
| 220 | }<br />
|
| 221 | }
|
| 222 | </code>
|
| 223 |
|
| 224 | <p>The <var>$request</var> variable is an object compiled by the Server, which contains the data sent by the XML-RPC Client.
|
| 225 | Using this object you will have access to the <em>request parameters</em> enabling you to process the request. When
|
| 226 | you are done you will send a <dfn>Response</dfn> back to the Client.<p>
|
| 227 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 228 | <p>Below is a real-world example, using the Blogger API. One of the methods in the Blogger API is <dfn>getUserInfo()</dfn>.
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 229 | Using this method, an XML-RPC Client can send the Server a username and password, in return the Server sends
|
| 230 | back information about that particular user (nickname, user ID, email address, etc.). Here is how the processing
|
| 231 | function might look:</p>
|
| 232 |
|
| 233 |
|
| 234 | <code>class <kbd>My_blog</kbd> extends Controller {<br />
|
| 235 | <br />
|
| 236 | function <kbd>getUserInfo</kbd>(<var>$request</var>)<br />
|
| 237 | {<br />
|
| 238 |
|
| 239 | $username = 'smitty';<br />
|
| 240 | $password = 'secretsmittypass';<br /><br />
|
| 241 |
|
| 242 | $this->load->library('xmlrpc');<br />
|
| 243 | <br />
|
| 244 | $parameters = $request->output_parameters();<br />
|
| 245 | <br />
|
| 246 | if ($parameters['1'] != $username AND $parameters['2'] != $password)<br />
|
| 247 | {<br />
|
| 248 | return $this->xmlrpc->send_error_message('100', 'Invalid Access');<br />
|
| 249 | }<br />
|
| 250 | <br />
|
| 251 | $response = array(array('nickname' => array('Smitty','string'),<br />
|
| 252 | 'userid' => array('99','string'),<br />
|
| 253 | 'url' => array('http://yoursite.com','string'),<br />
|
| 254 | 'email' => array('jsmith@yoursite.com','string'),<br />
|
| 255 | 'lastname' => array('Smith','string'),<br />
|
| 256 | 'firstname' => array('John','string')<br />
|
| 257 | ),<br />
|
| 258 | 'struct');<br />
|
| 259 | <br />
|
| 260 | $this->xmlrpc->send_response($response);<br />
|
| 261 | }<br />
|
| 262 | }
|
| 263 | </code>
|
| 264 |
|
| 265 | <h3>Notes:</h3>
|
| 266 | <p>The <dfn>output_parameters()</dfn> function retrieves an indexed array corresponding to the request parameters sent by the client.
|
| 267 | In the above example, the output parameters will be the username and password.</p>
|
| 268 |
|
| 269 | <p>If the username and password sent by the client were not valid, and error message is returned using <dfn>send_error_message()</dfn>.</p>
|
| 270 |
|
| 271 | <p>If the operation was successful, the client will be sent back a response array containing the user's info.</p>
|
| 272 |
|
| 273 |
|
| 274 | <h2>Formatting a Response</h2>
|
| 275 |
|
| 276 | <p>Similar to <em>Requests</em>, <em>Responses</em> must be formatted as an array. However, unlike requests, a response is an array
|
| 277 | <strong>that contains a single item</strong>. This item can be an array with several additional arrays, but there
|
| 278 | can be only one primary array index. In other words, the basic prototype is this:</p>
|
| 279 |
|
| 280 | <code>$request = array('Response data', 'array');</code>
|
| 281 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 282 | <p>Responses, however, usually contain multiple pieces of information. In order to accomplish this we must put the response into its own
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 283 | array so that the primary array continues to contain a single piece of data. Here's an example showing how this might be accomplished:</p>
|
| 284 |
|
| 285 | <code>
|
| 286 | $request = array (<br />
|
| 287 | array(<br />
|
| 288 | 'first_name' => array('John', 'string'),<br />
|
| 289 | 'last_name' => array('Doe', 'string'),<br />
|
| 290 | 'member_id' => array(123435, 'int'),<br />
|
| 291 | 'todo_list' => array(array('clean house', 'call mom', 'water plants'), 'array'),<br />
|
| 292 | ),<br />
|
| 293 | 'struct'<br />
|
| 294 | );
|
| 295 | </code>
|
| 296 |
|
| 297 | <p class="important">Notice that the above array is formatted as a <dfn>struct</dfn>. This is the most common data type for responses.</p>
|
| 298 |
|
| 299 | <p>As with Requests, a response can be on of the seven data types listed in the <a href="#datatypes">Data Types</a> section.</p>
|
| 300 |
|
| 301 |
|
| 302 | <h2>Sending an Error Response</h2>
|
| 303 |
|
| 304 | <p>If you need to send the client an error response you will use the following:</p>
|
| 305 |
|
| 306 | <code>return $this->xmlrpc->send_error_message('123', 'Requested data not available');</code>
|
| 307 |
|
| 308 | <p>The first parameter is the error number while the second parameter is the error message.</p>
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 | <h2>Creating Your Own Client and Server</h2>
|
| 316 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 317 | <p>To help you understand everything we've covered thus far, let's create a couple controllers that act as
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 318 | XML-RPC Client and Server. You'll use the Client to send a request to the Server and receive a response.</p>
|
| 319 |
|
| 320 | <h3>The Client</h3>
|
| 321 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 322 | <p>Using a text editor, create a controller called <dfn>xmlrpc_client.php</dfn>.
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 323 | In it, place this code and save it to your <samp>applications/controllers/</samp> folder:</p>
|
| 324 |
|
| 325 | <textarea class="textarea" style="width:100%" cols="50" rows="32"><?php
|
| 326 |
|
| 327 | class Xmlrpc_client extends Controller {
|
| 328 |
|
| 329 | function index()
|
| 330 | {
|
| 331 | $this->load->helper('url');
|
| 332 | $server_url = site_url('xmlrpc_server');
|
| 333 |
|
| 334 | $this->load->library('xmlrpc');
|
| 335 |
|
| 336 | $this->xmlrpc->server($server_url, 80);
|
| 337 | $this->xmlrpc->method('Greetings');
|
| 338 |
|
| 339 | $request = array('How is it going?');
|
| 340 | $this->xmlrpc->request($request);
|
| 341 |
|
| 342 | if ( ! $this->xmlrpc->send_request())
|
| 343 | {
|
| 344 | echo $this->xmlrpc->display_error();
|
| 345 | }
|
| 346 | else
|
| 347 | {
|
| 348 | echo '<pre>';
|
| 349 | print_r($this->xmlrpc->display_response());
|
| 350 | echo '</pre>';
|
| 351 | }
|
| 352 | }
|
| 353 | }
|
| 354 | ?></textarea>
|
| 355 |
|
| 356 | <p>Note: In the above code we are using a "url helper". You can find more information in the <a href="../general/helpers.html">Helpers Functions</a> page.</p>
|
| 357 |
|
| 358 | <h3>The Server</h3>
|
| 359 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 360 | <p>Using a text editor, create a controller called <dfn>xmlrpc_server.php</dfn>.
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 361 | In it, place this code and save it to your <samp>applications/controllers/</samp> folder:</p>
|
| 362 |
|
| 363 | <textarea class="textarea" style="width:100%" cols="50" rows="30"><?php
|
| 364 |
|
| 365 | class Xmlrpc_server extends Controller {
|
| 366 |
|
| 367 | function index()
|
| 368 | {
|
admin | e5bb936 | 2006-09-27 00:31:22 +0000 | [diff] [blame] | 369 |
|
| 370 | $this->load->library('xmlrpc');
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 371 | $this->load->library('xmlrpcs');
|
| 372 |
|
| 373 | $config['functions']['Greetings'] = array('function' => 'Xmlrpc_server.process');
|
| 374 |
|
| 375 | $this->xmlrpcs->initialize($config);
|
| 376 | $this->xmlrpcs->serve();
|
| 377 | }
|
| 378 |
|
| 379 |
|
| 380 | function process($request)
|
| 381 | {
|
| 382 | $parameters = $request->output_parameters();
|
| 383 |
|
| 384 | $response = array(
|
| 385 | array(
|
| 386 | 'you_said' => $parameters['0'],
|
| 387 | 'i_respond' => 'Not bad at all.'),
|
| 388 | 'struct');
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 389 |
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 390 | return $this->xmlrpc->send_response($response);
|
| 391 | }
|
| 392 | }
|
| 393 | ?></textarea>
|
| 394 |
|
| 395 | <h3>Try it!</h3>
|
| 396 |
|
| 397 | <p>Now visit the your site using a URL similar to this:</p>
|
| 398 | <code>www.your-site.com/index.php/<var>xmlrpc_client</var>/</code>
|
| 399 |
|
| 400 | <p>You should now see the message you sent to the server, and its response back to you.</p>
|
| 401 |
|
| 402 | <p>The client you created sends a message ("How's is going?") to the server, along with a reqest for the "Greetings" method.
|
| 403 | The Server receives the request and maps it to the "process" function, where a response is sent back.</p>
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 | <p> </p>
|
| 408 | <h1>XML-RPC Function Reference</h1>
|
| 409 |
|
| 410 | <h2>$this->xmlrpc->server()</h2>
|
| 411 | <p>Sets the URL and port number of the server to which a request is to be sent:</p>
|
| 412 | <code>$this->xmlrpc->server('http://www.sometimes.com/pings.php', 80);</code>
|
| 413 |
|
| 414 | <h2>$this->xmlrpc->timeout()</h2>
|
| 415 | <p>Set a time out period (in seconds) after which the request will be canceled:</p>
|
| 416 | <code>$this->xmlrpc->timeout(6);</code>
|
| 417 |
|
| 418 | <h2>$this->xmlrpc->method()</h2>
|
| 419 | <p>Sets the method that will be requested from the XML-RPC server:</p>
|
| 420 | <code>$this->xmlrpc->method('<var>method</var>');</code>
|
| 421 |
|
| 422 | <p>Where <var>method</var> is the name of the method.</p>
|
| 423 |
|
| 424 | <h2>$this->xmlrpc->request()</h2>
|
| 425 | <p>Takes an array of data and builds request to be sent to XML-RPC server:</p>
|
| 426 | <code>$request = array(array('My Photoblog', 'string'), 'http://www.yoursite.com/photoblog/');<br />
|
| 427 | $this->xmlrpc->request($request);</code>
|
| 428 |
|
| 429 | <h2>$this->xmlrpc->send_request()</h2>
|
| 430 | <p>The request sending function. Returns boolean TRUE or FALSE based on success for failure, enabling it to be used conditionally.</p>
|
| 431 |
|
| 432 | <h2>$this->xmlrpc->set_debug(TRUE);</h2>
|
| 433 | <p>Enables debugging, which will display a variety of information and error data helpful during development.</p>
|
| 434 |
|
| 435 |
|
| 436 | <h2>$this->xmlrpc->display_error()</h2>
|
| 437 | <p>Returns an error message as a string if your request failed for some reason.</p>
|
| 438 | <code>echo $this->xmlrpc->display_error();</code>
|
| 439 |
|
| 440 | <h2>$this->xmlrpc->display_response()</h2>
|
| 441 | <p>Returns the response from the remote server once request is received. The response will typically be an associative array.</p>
|
| 442 | <code>$this->xmlrpc->display_response();</code>
|
| 443 |
|
| 444 | <h2>$this->xmlrpc->send_error_message()</h2>
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 445 | <p>This function lets you send an error message from your server to the client. First parameter is the error number while the second parameter
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 446 | is the error message.</p>
|
| 447 | <code>return $this->xmlrpc->send_error_message('123', 'Requested data not available');</code>
|
| 448 |
|
| 449 | <h2>$this->xmlrpc->send_response()</h2>
|
admin | e7e1dcd | 2006-10-21 18:04:01 +0000 | [diff] [blame] | 450 | <p>Lets you send the response from your server to the client. An array of valid data values must be sent with this method.</p>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 451 | <code>$response = array(<br />
|
| 452 | array(<br />
|
| 453 | 'flerror' => array(FALSE, 'boolean'),<br />
|
| 454 | 'message' => "Thanks for the ping!")<br />
|
| 455 | )<br />
|
| 456 | 'struct');<br />
|
| 457 | return $this->xmlrpc->send_response($response);</code>
|
| 458 |
|
| 459 |
|
| 460 | <a name="datatypes"></a>
|
| 461 | <h2>Data Types</h2>
|
| 462 |
|
| 463 | <p>According to the <a href="http://www.xmlrpc.com/spec">XML-RPC spec</a> there are seven types
|
| 464 | of values that you can send via XML-RPC:</p>
|
| 465 |
|
| 466 | <ul>
|
| 467 | <li><em>int</em> or <em>i4</em></li>
|
| 468 | <li><em>boolean</em></li>
|
| 469 | <li><em>string</em></li>
|
| 470 | <li><em>double</em></li>
|
| 471 | <li><em>dateTime.iso8601</em></li>
|
| 472 | <li><em>base64</em></li>
|
| 473 | <li><em>struct</em> (contains array of values)</li>
|
| 474 | <li><em>array</em> (contains array of values)</li>
|
| 475 | </ul>
|
| 476 |
|
| 477 |
|
| 478 | </div>
|
| 479 | <!-- END CONTENT -->
|
| 480 |
|
| 481 |
|
| 482 | <div id="footer">
|
| 483 | <p>
|
| 484 | Previous Topic: <a href="validation.html">Validation Class</a>
|
| 485 | ·
|
| 486 | <a href="#top">Top of Page</a> ·
|
| 487 | <a href="../index.html">User Guide Home</a> ·
|
Rick Ellis | 325197e | 2006-11-20 17:29:05 +0000 | [diff] [blame] | 488 | Next Topic: <a href="zip.html">Zip Encoding Class</a>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 489 | <p>
|
| 490 | <p><a href="http://www.codeigniter.com">Code Igniter</a> · Copyright © 2006 · <a href="http://www.pmachine.com">pMachine, Inc.</a></p>
|
| 491 | </div>
|
| 492 |
|
| 493 | </body>
|
| 494 | </html> |