blob: 77cabe43765c950d433dfb5c0ab8a7417dc18e79 [file] [log] [blame]
adminb0dd10f2006-08-25 17:25:49 +00001<!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
Derek Allard404e35d2007-08-07 01:00:45 +00005<title>CodeIgniter User Guide : XML-RPC and XML-RPC Server Classes</title>
adminb0dd10f2006-08-25 17:25:49 +00006
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
admin17a890d2006-09-27 20:42:42 +000010<script type="text/javascript" src="../nav/nav.js"></script>
admin2296fc32006-09-27 21:07:02 +000011<script type="text/javascript" src="../nav/prototype.lite.js"></script>
admin17a890d2006-09-27 20:42:42 +000012<script type="text/javascript" src="../nav/moo.fx.js"></script>
Derek Allardb3412372007-10-25 12:15:16 +000013<script type="text/javascript" src="../nav/user_guide_menu.js"></script>
adminb0dd10f2006-08-25 17:25:49 +000014
15<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
16<meta http-equiv='expires' content='-1' />
17<meta http-equiv= 'pragma' content='no-cache' />
18<meta name='robots' content='all' />
19<meta name='author' content='Rick Ellis' />
Derek Allardd2df9bc2007-04-15 17:41:17 +000020<meta name='description' content='CodeIgniter User Guide' />
adminb0dd10f2006-08-25 17:25:49 +000021
22</head>
23<body>
24
25<!-- START NAVIGATION -->
26<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
27<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>
28<div id="masthead">
29<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
30<tr>
Derek Allard60ca9b72007-07-12 19:53:27 +000031<td><h1>CodeIgniter User Guide Version 1.5.4</h1></td>
adminc0d5d522006-10-30 19:40:35 +000032<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
adminb0dd10f2006-08-25 17:25:49 +000033</tr>
34</table>
35</div>
36<!-- END NAVIGATION -->
37
38
39<!-- START BREADCRUMB -->
40<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
41<tr>
42<td id="breadcrumb">
Derek Allardd2df9bc2007-04-15 17:41:17 +000043<a href="http://www.codeigniter.com/">CodeIgniter Home</a> &nbsp;&#8250;&nbsp;
adminb0dd10f2006-08-25 17:25:49 +000044<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
45XML-RPC and XML-RPC Server Classes
46</td>
Derek Allardbc030912007-06-24 18:25:29 +000047<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="codeigniter.com/user_guide/" />Search User Guide&nbsp; <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" />&nbsp;<input type="submit" class="submit" name="sa" value="Go" /></form></td>
adminb0dd10f2006-08-25 17:25:49 +000048</tr>
49</table>
50<!-- END BREADCRUMB -->
51
52<br clear="all" />
53
54
55<!-- START CONTENT -->
56<div id="content">
57
58
59<h1>XML-RPC and XML-RPC Server Classes</h1>
60
61
Derek Allardd2df9bc2007-04-15 17:41:17 +000062<p>CodeIgniter's XML-RPC classes permit you to send requests to another server, or set up
adminb0dd10f2006-08-25 17:25:49 +000063your own XML-RPC server to receive requests.</p>
64
65
66<h2>What is XML-RPC?</h2>
67
admine334c472006-10-21 19:44:22 +000068<p>Quite simply it is a way for two computers to communicate over the internet using XML.
69One computer, which we will call the <dfn>client</dfn>, sends an XML-RPC <strong>request</strong> to
adminb0dd10f2006-08-25 17:25:49 +000070another computer, which we will call the <dfn>server</dfn>. Once the server receives and processes the request it
71will send back a <strong>response</strong> to the client.</p>
72
admine334c472006-10-21 19:44:22 +000073<p>For example, using the MetaWeblog API, an XML-RPC Client (usually a desktop publishing tool) will
adminb0dd10f2006-08-25 17:25:49 +000074send a request to an XML-RPC Server running on your site. This request might be a new weblog entry
75being sent for publication, or it could be a request for an existing entry for editing.
76
77When the XML-RPC Server receives this request it will examine it to determine which class/method should be called to process the request.
78Once processed, the server will then send back a response message.</p>
79
admine7e1dcd2006-10-21 18:04:01 +000080<p>For detailed specifications, you can visit the <a href="http://www.xmlrpc.com/">XML-RPC</a> site.</p>
adminb0dd10f2006-08-25 17:25:49 +000081
82<h2>Initializing the Class</h2>
83
Derek Allardd2df9bc2007-04-15 17:41:17 +000084<p>Like most other classes in CodeIgniter, the XML-RPC and XML-RPCS classes are initialized in your controller using the <dfn>$this->load->library</dfn> function:</p>
adminb0dd10f2006-08-25 17:25:49 +000085
Derek Allardc6441282007-07-04 23:54:32 +000086<p>To load the XML-RPC class you will use:</p>
adminb0dd10f2006-08-25 17:25:49 +000087<code>$this->load->library('xmlrpc');</code>
88<p>Once loaded, the xml-rpc library object will be available using: <dfn>$this->xmlrpc</dfn></p>
89
Derek Allardc6441282007-07-04 23:54:32 +000090<p>To load the XML-RPC Server class you will use:</p>
admine5bb9362006-09-27 00:31:22 +000091<code>
92$this->load->library('xmlrpc');<br />
93$this->load->library('xmlrpcs');
94</code>
adminb0dd10f2006-08-25 17:25:49 +000095<p>Once loaded, the xml-rpcs library object will be available using: <dfn>$this->xmlrpcs</dfn></p>
96
Derek Allard55c880f2007-07-11 23:50:33 +000097<p class="important"><strong>Note:</strong>&nbsp; When using the XML-RPC Server class you must load BOTH the XML-RPC class and the XML-RPC Server class.</p>
admine5bb9362006-09-27 00:31:22 +000098
adminb0dd10f2006-08-25 17:25:49 +000099
100
101<h2>Sending XML-RPC Requests</h2>
102
103<p>To send a request to an XML-RPC server you must specify the following information:</p>
104
105<ul>
106<li>The URL of the server</li>
107<li>The method on the server you wish to call</li>
108<li>The <em>request</em> data (explained below).</li>
109</ul>
110
111<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>
112
113
114<code>$this->load->library('xmlrpc');<br />
115<br />
116$this->xmlrpc->server('http://rpc.pingomatic.com/', 80);<br />
117$this->xmlrpc->method('weblogUpdates.ping');<br />
118
119<br />
120$request = array('My Photoblog', 'http://www.my-site.com/photoblog/');<br />
121$this->xmlrpc->request($request);<br />
122<br />
123if ( ! $this->xmlrpc->send_request())<br />
124{<br />
125&nbsp;&nbsp;&nbsp;&nbsp;echo $xmlrpc->display_error();<br />
126}
127</code>
128
129<h3>Explanation</h3>
130
131<p>The above code initializes the XML-RPC class, sets the server URL and method to be called (weblogUpdates.ping). The
132request (in this case, the title and URL of your site) is placed into an array for transportation, and
133compiled using the request() function.
134Lastly, the full request is sent. If the <dfn>send_request()</dfn> method returns false we will display the error message
135sent back from the XML-RPC Server.</p>
136
137<h2>Anatomy of a Request</h2>
138
admine334c472006-10-21 19:44:22 +0000139<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
140is referred to as a <dfn>request parameter</dfn>. The above example has two parameters:
adminb0dd10f2006-08-25 17:25:49 +0000141The URL and title of your site. When the XML-RPC server receives your request, it will look for parameters it requires.</p>
142
admine7e1dcd2006-10-21 18:04:01 +0000143<p>Request parameters must be placed into an array for transportation, and each parameter can be one
admine334c472006-10-21 19:44:22 +0000144of seven data types (strings, numbers, dates, etc.). If your parameters are something other than strings
adminb0dd10f2006-08-25 17:25:49 +0000145you will have to include the data type in the request array.</p>
146
147<p>Here is an example of a simple array with three parameters:</p>
148
paulburdickce90c172007-10-10 15:08:25 +0000149<code>$request = array('John', 'Doe', 'www.some-site.com');<br />
adminb0dd10f2006-08-25 17:25:49 +0000150$this->xmlrpc->request($request);</code>
151
152<p>If you use data types other than strings, or if you have several different data types, you will place
153each parameter into its own array, with the data type in the second position:</p>
154
155<code>
156$request = array (<br />
157&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array('John', 'string'),<br />
158&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array('Doe', 'string'),<br />
159&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array(FALSE, 'boolean'),<br />
160&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array(12345, 'int')<br />
161&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);
162<br />
163$this->xmlrpc->request($request);</code>
164
165The <a href="#datatypes">Data Types</a> section below has a full list of data types.</p>
166
167
168
169<h2>Creating an XML-RPC Server</h2>
170
admine334c472006-10-21 19:44:22 +0000171<p>An XML-RPC Server acts as a traffic cop of sorts, waiting for incoming requests and redirecting them to the
adminb0dd10f2006-08-25 17:25:49 +0000172appropriate functions for processing.</p>
173
admine334c472006-10-21 19:44:22 +0000174<p>To create your own XML-RPC server involves initializing the XML-RPC Server class in your controller where you expect the incoming
adminb0dd10f2006-08-25 17:25:49 +0000175request to appear, then setting up an array with mapping instructions so that incoming requests can be sent to the appropriate
176class and method for processing.</p>
177
178<p>Here is an example to illustrate:</p>
179
admine5bb9362006-09-27 00:31:22 +0000180<code>
Derek Allard090b2f72007-03-01 12:57:52 +0000181$this->load->library('xmlrpc');<br />
admine5bb9362006-09-27 00:31:22 +0000182$this->load->library('xmlrpcs');<br />
adminb0dd10f2006-08-25 17:25:49 +0000183<br />
Derek Allard65fdfbf2007-11-21 19:15:11 +0000184$config['functions']['<var>new_post</var>']&nbsp;&nbsp;= array('function' => '<dfn>My_blog.new_entry</dfn>');<br />
adminb0dd10f2006-08-25 17:25:49 +0000185$config['functions']['<var>update_post</var>'] = array('function' => '<dfn>My_blog.update_entry</dfn>');<br />
186<br />
187$this->xmlrpcs->initialize($config);<br />
188$this->xmlrpcs->serve();</code>
189
admine334c472006-10-21 19:44:22 +0000190<p>The above example contains an array specifying two method requests that the Server allows.
Derek Allardc6441282007-07-04 23:54:32 +0000191The 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.</p>
adminb0dd10f2006-08-25 17:25:49 +0000192
admine334c472006-10-21 19:44:22 +0000193<p>In other words, if an XML-RPC Client sends a request for the <var>new_post</var> method, your
adminb0dd10f2006-08-25 17:25:49 +0000194server will load the <dfn>My_blog</dfn> class and call the <dfn>new_entry</dfn> function.
admine334c472006-10-21 19:44:22 +0000195If the request is for the <var>update_post</var> method, your
adminb0dd10f2006-08-25 17:25:49 +0000196server will load the <dfn>My_blog</dfn> class and call the <dfn>update_entry</dfn> function.</p>
197
admine334c472006-10-21 19:44:22 +0000198<p>The function names in the above example are arbitrary. You'll decide what they should be called on your server,
adminb0dd10f2006-08-25 17:25:49 +0000199or if you are using standardized APIs, like the Blogger or MetaWeblog API, you'll use their function names.</p>
200
201
202<h2>Processing Server Requests</h2>
203
204<p>When the XML-RPC Server receives a request and loads the class/method for processing, it will pass
205an object to that method containing the data sent by the client.</p>
206
207<p>Using the above example, if the <var>new_post</var> method is requested, the server will expect a class
208to exist with this prototype:</p>
209
210<code>class <kbd>My_blog</kbd> extends Controller {<br />
211<br />
212&nbsp;&nbsp;&nbsp;&nbsp;function <kbd>new_post</kbd>(<var>$request</var>)<br />
213&nbsp;&nbsp;&nbsp;&nbsp;{<br />
214 <br />
215&nbsp;&nbsp;&nbsp;&nbsp;}<br />
216}
217</code>
218
219<p>The <var>$request</var> variable is an object compiled by the Server, which contains the data sent by the XML-RPC Client.
220Using this object you will have access to the <em>request parameters</em> enabling you to process the request. When
Derek Allardc6441282007-07-04 23:54:32 +0000221you are done you will send a <dfn>Response</dfn> back to the Client.</p>
adminb0dd10f2006-08-25 17:25:49 +0000222
admine334c472006-10-21 19:44:22 +0000223<p>Below is a real-world example, using the Blogger API. One of the methods in the Blogger API is <dfn>getUserInfo()</dfn>.
adminb0dd10f2006-08-25 17:25:49 +0000224Using this method, an XML-RPC Client can send the Server a username and password, in return the Server sends
225back information about that particular user (nickname, user ID, email address, etc.). Here is how the processing
226function might look:</p>
227
228
229<code>class <kbd>My_blog</kbd> extends Controller {<br />
230<br />
231&nbsp;&nbsp;&nbsp;&nbsp;function <kbd>getUserInfo</kbd>(<var>$request</var>)<br />
232&nbsp;&nbsp;&nbsp;&nbsp;{<br />
233
234&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$username = 'smitty';<br />
235&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$password = 'secretsmittypass';<br /><br />
236
237&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->load->library('xmlrpc');<br />
238&nbsp;&nbsp;&nbsp;&nbsp;<br />
239&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$parameters&nbsp;=&nbsp;$request->output_parameters();<br />
240&nbsp;&nbsp;&nbsp;&nbsp;<br />
241&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;($parameters['1'] != $username AND $parameters['2'] != $password)<br />
242&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />
243&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$this->xmlrpc->send_error_message('100',&nbsp;'Invalid&nbsp;Access');<br />
244&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
245&nbsp;&nbsp;&nbsp;&nbsp;<br />
246&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$response&nbsp;=&nbsp;array(array('nickname'&nbsp;&nbsp;=>&nbsp;array('Smitty','string'),<br />
247&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'userid'&nbsp;&nbsp;&nbsp;&nbsp;=>&nbsp;array('99','string'),<br />
248&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'url'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=>&nbsp;array('http://yoursite.com','string'),<br />
249&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'email'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=>&nbsp;array('jsmith@yoursite.com','string'),<br />
250&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'lastname'&nbsp;&nbsp;=>&nbsp;array('Smith','string'),<br />
251&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'firstname'&nbsp;=>&nbsp;array('John','string')<br />
252&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;),<br />
253&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'struct');<br />
254<br />
paulburdickce90c172007-10-10 15:08:25 +0000255&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return $this->xmlrpc->send_response($response);<br />
adminb0dd10f2006-08-25 17:25:49 +0000256&nbsp;&nbsp;&nbsp;&nbsp;}<br />
257}
258</code>
259
260<h3>Notes:</h3>
261<p>The <dfn>output_parameters()</dfn> function retrieves an indexed array corresponding to the request parameters sent by the client.
262In the above example, the output parameters will be the username and password.</p>
263
264<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>
265
266<p>If the operation was successful, the client will be sent back a response array containing the user's info.</p>
267
268
269<h2>Formatting a Response</h2>
270
271<p>Similar to <em>Requests</em>, <em>Responses</em> must be formatted as an array. However, unlike requests, a response is an array
272<strong>that contains a single item</strong>. This item can be an array with several additional arrays, but there
273can be only one primary array index. In other words, the basic prototype is this:</p>
274
paulburdickce90c172007-10-10 15:08:25 +0000275<code>$response = array('Response data', 'array');</code>
adminb0dd10f2006-08-25 17:25:49 +0000276
admine334c472006-10-21 19:44:22 +0000277<p>Responses, however, usually contain multiple pieces of information. In order to accomplish this we must put the response into its own
adminb0dd10f2006-08-25 17:25:49 +0000278array so that the primary array continues to contain a single piece of data. Here's an example showing how this might be accomplished:</p>
279
280<code>
paulburdickce90c172007-10-10 15:08:25 +0000281$response = array (<br />
adminb0dd10f2006-08-25 17:25:49 +0000282&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array(<br />
283&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'first_name' => array('John', 'string'),<br />
284&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'last_name' => array('Doe', 'string'),<br />
285&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'member_id' => array(123435, 'int'),<br />
286&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'todo_list' => array(array('clean house', 'call mom', 'water plants'), 'array'),<br />
287&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;),<br />
288&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'struct'<br />
289&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);
290</code>
291
292<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>
293
294<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>
295
296
297<h2>Sending an Error Response</h2>
298
299<p>If you need to send the client an error response you will use the following:</p>
300
301<code>return $this->xmlrpc->send_error_message('123', 'Requested data not available');</code>
302
303<p>The first parameter is the error number while the second parameter is the error message.</p>
304
305
306
307
308
309
310<h2>Creating Your Own Client and Server</h2>
311
admine334c472006-10-21 19:44:22 +0000312<p>To help you understand everything we've covered thus far, let's create a couple controllers that act as
adminb0dd10f2006-08-25 17:25:49 +0000313XML-RPC Client and Server. You'll use the Client to send a request to the Server and receive a response.</p>
314
315<h3>The Client</h3>
316
admine334c472006-10-21 19:44:22 +0000317<p>Using a text editor, create a controller called <dfn>xmlrpc_client.php</dfn>.
adminb0dd10f2006-08-25 17:25:49 +0000318In it, place this code and save it to your <samp>applications/controllers/</samp> folder:</p>
319
320<textarea class="textarea" style="width:100%" cols="50" rows="32"><?php
321
322class Xmlrpc_client extends Controller {
323
324 function index()
325 {
326 $this->load->helper('url');
327 $server_url = site_url('xmlrpc_server');
328
329 $this->load->library('xmlrpc');
330
331 $this->xmlrpc->server($server_url, 80);
332 $this->xmlrpc->method('Greetings');
333
334 $request = array('How is it going?');
335 $this->xmlrpc->request($request);
336
337 if ( ! $this->xmlrpc->send_request())
338 {
339 echo $this->xmlrpc->display_error();
340 }
341 else
342 {
343 echo '<pre>';
344 print_r($this->xmlrpc->display_response());
345 echo '</pre>';
346 }
347 }
348}
349?></textarea>
350
351<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>
352
353<h3>The Server</h3>
354
admine334c472006-10-21 19:44:22 +0000355<p>Using a text editor, create a controller called <dfn>xmlrpc_server.php</dfn>.
adminb0dd10f2006-08-25 17:25:49 +0000356In it, place this code and save it to your <samp>applications/controllers/</samp> folder:</p>
357
358<textarea class="textarea" style="width:100%" cols="50" rows="30"><?php
359
360class Xmlrpc_server extends Controller {
361
362 function index()
363 {
admine5bb9362006-09-27 00:31:22 +0000364 $this->load->library('xmlrpc');
adminb0dd10f2006-08-25 17:25:49 +0000365 $this->load->library('xmlrpcs');
366
367 $config['functions']['Greetings'] = array('function' => 'Xmlrpc_server.process');
368
369 $this->xmlrpcs->initialize($config);
370 $this->xmlrpcs->serve();
371 }
372
373
374 function process($request)
375 {
376 $parameters = $request->output_parameters();
377
378 $response = array(
379 array(
380 'you_said' => $parameters['0'],
381 'i_respond' => 'Not bad at all.'),
382 'struct');
admine334c472006-10-21 19:44:22 +0000383
adminb0dd10f2006-08-25 17:25:49 +0000384 return $this->xmlrpc->send_response($response);
385 }
386}
387?></textarea>
388
389<h3>Try it!</h3>
390
391<p>Now visit the your site using a URL similar to this:</p>
392<code>www.your-site.com/index.php/<var>xmlrpc_client</var>/</code>
393
394<p>You should now see the message you sent to the server, and its response back to you.</p>
395
396<p>The client you created sends a message ("How's is going?") to the server, along with a reqest for the "Greetings" method.
397The Server receives the request and maps it to the "process" function, where a response is sent back.</p>
398
399
400
401<p>&nbsp;</p>
402<h1>XML-RPC Function Reference</h1>
403
404<h2>$this->xmlrpc->server()</h2>
405<p>Sets the URL and port number of the server to which a request is to be sent:</p>
406<code>$this->xmlrpc->server('http://www.sometimes.com/pings.php', 80);</code>
407
408<h2>$this->xmlrpc->timeout()</h2>
409<p>Set a time out period (in seconds) after which the request will be canceled:</p>
410<code>$this->xmlrpc->timeout(6);</code>
411
412<h2>$this->xmlrpc->method()</h2>
413<p>Sets the method that will be requested from the XML-RPC server:</p>
414<code>$this->xmlrpc->method('<var>method</var>');</code>
415
416<p>Where <var>method</var> is the name of the method.</p>
417
418<h2>$this->xmlrpc->request()</h2>
419<p>Takes an array of data and builds request to be sent to XML-RPC server:</p>
420<code>$request = array(array('My Photoblog', 'string'), 'http://www.yoursite.com/photoblog/');<br />
421$this->xmlrpc->request($request);</code>
422
423<h2>$this->xmlrpc->send_request()</h2>
424<p>The request sending function. Returns boolean TRUE or FALSE based on success for failure, enabling it to be used conditionally.</p>
425
426<h2>$this->xmlrpc->set_debug(TRUE);</h2>
427<p>Enables debugging, which will display a variety of information and error data helpful during development.</p>
428
429
430<h2>$this->xmlrpc->display_error()</h2>
431<p>Returns an error message as a string if your request failed for some reason.</p>
432<code>echo $this->xmlrpc->display_error();</code>
433
434<h2>$this->xmlrpc->display_response()</h2>
435<p>Returns the response from the remote server once request is received. The response will typically be an associative array.</p>
436<code>$this->xmlrpc->display_response();</code>
437
438<h2>$this->xmlrpc->send_error_message()</h2>
admine334c472006-10-21 19:44:22 +0000439<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
adminb0dd10f2006-08-25 17:25:49 +0000440is the error message.</p>
441<code>return $this->xmlrpc->send_error_message('123', 'Requested data not available');</code>
442
443<h2>$this->xmlrpc->send_response()</h2>
admine7e1dcd2006-10-21 18:04:01 +0000444<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>
adminb0dd10f2006-08-25 17:25:49 +0000445<code>$response = array(<br />
446&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array(<br />
447&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'flerror' => array(FALSE, 'boolean'),<br />
448&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'message' => "Thanks for the ping!")<br />
449&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)<br />
450&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'struct');<br />
451return $this->xmlrpc->send_response($response);</code>
452
453
454<a name="datatypes"></a>
455<h2>Data Types</h2>
456
457<p>According to the <a href="http://www.xmlrpc.com/spec">XML-RPC spec</a> there are seven types
458of values that you can send via XML-RPC:</p>
459
460<ul>
461<li><em>int</em> or <em>i4</em></li>
462<li><em>boolean</em></li>
463<li><em>string</em></li>
464<li><em>double</em></li>
465<li><em>dateTime.iso8601</em></li>
466<li><em>base64</em></li>
467<li><em>struct</em> (contains array of values)</li>
468<li><em>array</em> (contains array of values)</li>
469</ul>
470
471
472</div>
473<!-- END CONTENT -->
474
475
476<div id="footer">
477<p>
478Previous Topic:&nbsp;&nbsp;<a href="validation.html">Validation Class</a>
479&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
480<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
481<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
Rick Ellis325197e2006-11-20 17:29:05 +0000482Next Topic:&nbsp;&nbsp;<a href="zip.html">Zip Encoding Class</a>
Derek Allardc6441282007-07-04 23:54:32 +0000483</p>
Derek Allardd2df9bc2007-04-15 17:41:17 +0000484<p><a href="http://www.codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2007 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
adminb0dd10f2006-08-25 17:25:49 +0000485</div>
486
487</body>
488</html>