Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ################################## |
| 2 | XML-RPC and XML-RPC Server Classes |
| 3 | ################################## |
| 4 | |
| 5 | CodeIgniter's XML-RPC classes permit you to send requests to another |
| 6 | server, or set up your own XML-RPC server to receive requests. |
| 7 | |
Andrey Andreev | d8afb98 | 2013-09-23 16:01:46 +0300 | [diff] [blame] | 8 | .. contents:: |
Andrey Andreev | cc04209 | 2014-01-03 17:08:27 +0200 | [diff] [blame] | 9 | :local: |
Andrey Andreev | d8afb98 | 2013-09-23 16:01:46 +0300 | [diff] [blame] | 10 | |
| 11 | .. raw:: html |
| 12 | |
Andrey Andreev | cc04209 | 2014-01-03 17:08:27 +0200 | [diff] [blame] | 13 | <div class="custom-index container"></div> |
Andrey Andreev | d8afb98 | 2013-09-23 16:01:46 +0300 | [diff] [blame] | 14 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 15 | **************** |
| 16 | What is XML-RPC? |
| 17 | **************** |
| 18 | |
| 19 | Quite simply it is a way for two computers to communicate over the |
| 20 | internet using XML. One computer, which we will call the client, sends |
| 21 | an XML-RPC **request** to another computer, which we will call the |
| 22 | server. Once the server receives and processes the request it will send |
| 23 | back a **response** to the client. |
| 24 | |
| 25 | For example, using the MetaWeblog API, an XML-RPC Client (usually a |
| 26 | desktop publishing tool) will send a request to an XML-RPC Server |
| 27 | running on your site. This request might be a new weblog entry being |
| 28 | sent for publication, or it could be a request for an existing entry for |
| 29 | editing. When the XML-RPC Server receives this request it will examine |
| 30 | it to determine which class/method should be called to process the |
| 31 | request. Once processed, the server will then send back a response |
| 32 | message. |
| 33 | |
Andrey Andreev | d8afb98 | 2013-09-23 16:01:46 +0300 | [diff] [blame] | 34 | For detailed specifications, you can visit the `XML-RPC <http://www.xmlrpc.com/>`_ site. |
| 35 | |
| 36 | *********************** |
| 37 | Using the XML-RPC Class |
| 38 | *********************** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 39 | |
| 40 | Initializing the Class |
| 41 | ====================== |
| 42 | |
| 43 | Like most other classes in CodeIgniter, the XML-RPC and XML-RPCS classes |
| 44 | are initialized in your controller using the $this->load->library |
| 45 | function: |
| 46 | |
| 47 | To load the XML-RPC class you will use:: |
| 48 | |
| 49 | $this->load->library('xmlrpc'); |
| 50 | |
| 51 | Once loaded, the xml-rpc library object will be available using: |
| 52 | $this->xmlrpc |
| 53 | |
| 54 | To load the XML-RPC Server class you will use:: |
| 55 | |
Derek Jones | 92763a5 | 2011-10-05 15:28:52 -0500 | [diff] [blame] | 56 | $this->load->library('xmlrpc'); |
| 57 | $this->load->library('xmlrpcs'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 58 | |
| 59 | Once loaded, the xml-rpcs library object will be available using: |
| 60 | $this->xmlrpcs |
| 61 | |
| 62 | .. note:: When using the XML-RPC Server class you must load BOTH the |
| 63 | XML-RPC class and the XML-RPC Server class. |
| 64 | |
| 65 | Sending XML-RPC Requests |
| 66 | ======================== |
| 67 | |
| 68 | To send a request to an XML-RPC server you must specify the following |
| 69 | information: |
| 70 | |
| 71 | - The URL of the server |
| 72 | - The method on the server you wish to call |
| 73 | - The *request* data (explained below). |
| 74 | |
| 75 | Here is a basic example that sends a simple Weblogs.com ping to the |
| 76 | `Ping-o-Matic <http://pingomatic.com/>`_ |
| 77 | |
| 78 | :: |
| 79 | |
Derek Jones | 92763a5 | 2011-10-05 15:28:52 -0500 | [diff] [blame] | 80 | $this->load->library('xmlrpc'); |
| 81 | |
| 82 | $this->xmlrpc->server('http://rpc.pingomatic.com/', 80); |
| 83 | $this->xmlrpc->method('weblogUpdates.ping'); |
| 84 | |
| 85 | $request = array('My Photoblog', 'http://www.my-site.com/photoblog/'); |
| 86 | $this->xmlrpc->request($request); |
| 87 | |
| 88 | if ( ! $this->xmlrpc->send_request()) |
| 89 | { |
Andrey Andreev | db6f5f1 | 2014-05-16 11:05:16 +0300 | [diff] [blame] | 90 | echo $this->xmlrpc->display_error(); |
Derek Jones | 92763a5 | 2011-10-05 15:28:52 -0500 | [diff] [blame] | 91 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 92 | |
| 93 | Explanation |
| 94 | ----------- |
| 95 | |
| 96 | The above code initializes the XML-RPC class, sets the server URL and |
| 97 | method to be called (weblogUpdates.ping). The request (in this case, the |
| 98 | title and URL of your site) is placed into an array for transportation, |
| 99 | and compiled using the request() function. Lastly, the full request is |
| 100 | sent. If the send_request() method returns false we will display the |
| 101 | error message sent back from the XML-RPC Server. |
| 102 | |
| 103 | Anatomy of a Request |
| 104 | ==================== |
| 105 | |
| 106 | An XML-RPC request is simply the data you are sending to the XML-RPC |
| 107 | server. Each piece of data in a request is referred to as a request |
| 108 | parameter. The above example has two parameters: The URL and title of |
| 109 | your site. When the XML-RPC server receives your request, it will look |
| 110 | for parameters it requires. |
| 111 | |
| 112 | Request parameters must be placed into an array for transportation, and |
| 113 | each parameter can be one of seven data types (strings, numbers, dates, |
| 114 | etc.). If your parameters are something other than strings you will have |
| 115 | to include the data type in the request array. |
| 116 | |
| 117 | Here is an example of a simple array with three parameters:: |
| 118 | |
Derek Jones | 92763a5 | 2011-10-05 15:28:52 -0500 | [diff] [blame] | 119 | $request = array('John', 'Doe', 'www.some-site.com'); |
| 120 | $this->xmlrpc->request($request); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 121 | |
| 122 | If you use data types other than strings, or if you have several |
| 123 | different data types, you will place each parameter into its own array, |
| 124 | with the data type in the second position:: |
| 125 | |
Andrey Andreev | 053d5d6 | 2014-02-11 21:45:39 +0200 | [diff] [blame] | 126 | $request = array( |
| 127 | array('John', 'string'), |
| 128 | array('Doe', 'string'), |
| 129 | array(FALSE, 'boolean'), |
| 130 | array(12345, 'int') |
| 131 | ); |
Derek Jones | 92763a5 | 2011-10-05 15:28:52 -0500 | [diff] [blame] | 132 | $this->xmlrpc->request($request); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 133 | |
| 134 | The `Data Types <#datatypes>`_ section below has a full list of data |
| 135 | types. |
Andrey Andreev | d8afb98 | 2013-09-23 16:01:46 +0300 | [diff] [blame] | 136 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 137 | Creating an XML-RPC Server |
| 138 | ========================== |
| 139 | |
| 140 | An XML-RPC Server acts as a traffic cop of sorts, waiting for incoming |
| 141 | requests and redirecting them to the appropriate functions for |
| 142 | processing. |
| 143 | |
| 144 | To create your own XML-RPC server involves initializing the XML-RPC |
| 145 | Server class in your controller where you expect the incoming request to |
| 146 | appear, then setting up an array with mapping instructions so that |
| 147 | incoming requests can be sent to the appropriate class and method for |
| 148 | processing. |
| 149 | |
| 150 | Here is an example to illustrate:: |
| 151 | |
Derek Jones | 92763a5 | 2011-10-05 15:28:52 -0500 | [diff] [blame] | 152 | $this->load->library('xmlrpc'); |
| 153 | $this->load->library('xmlrpcs'); |
| 154 | |
Andrey Andreev | db6f5f1 | 2014-05-16 11:05:16 +0300 | [diff] [blame] | 155 | $config['functions']['new_post'] = array('function' => 'My_blog.new_entry'); |
Derek Jones | 92763a5 | 2011-10-05 15:28:52 -0500 | [diff] [blame] | 156 | $config['functions']['update_post'] = array('function' => 'My_blog.update_entry'); |
| 157 | $config['object'] = $this; |
| 158 | |
| 159 | $this->xmlrpcs->initialize($config); |
| 160 | $this->xmlrpcs->serve(); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 161 | |
| 162 | The above example contains an array specifying two method requests that |
| 163 | the Server allows. The allowed methods are on the left side of the |
| 164 | array. When either of those are received, they will be mapped to the |
| 165 | class and method on the right. |
| 166 | |
| 167 | The 'object' key is a special key that you pass an instantiated class |
| 168 | object with, which is necessary when the method you are mapping to is |
| 169 | not part of the CodeIgniter super object. |
| 170 | |
| 171 | In other words, if an XML-RPC Client sends a request for the new_post |
| 172 | method, your server will load the My_blog class and call the new_entry |
| 173 | function. If the request is for the update_post method, your server |
Andrey Andreev | 053d5d6 | 2014-02-11 21:45:39 +0200 | [diff] [blame] | 174 | will load the My_blog class and call the ``update_entry()`` method. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 175 | |
| 176 | The function names in the above example are arbitrary. You'll decide |
| 177 | what they should be called on your server, or if you are using |
| 178 | standardized APIs, like the Blogger or MetaWeblog API, you'll use their |
| 179 | function names. |
| 180 | |
| 181 | There are two additional configuration keys you may make use of when |
| 182 | initializing the server class: debug can be set to TRUE in order to |
| 183 | enable debugging, and xss_clean may be set to FALSE to prevent sending |
Andrey Andreev | 053d5d6 | 2014-02-11 21:45:39 +0200 | [diff] [blame] | 184 | data through the Security library's ``xss_clean()`` method. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 185 | |
| 186 | Processing Server Requests |
| 187 | ========================== |
| 188 | |
| 189 | When the XML-RPC Server receives a request and loads the class/method |
| 190 | for processing, it will pass an object to that method containing the |
| 191 | data sent by the client. |
| 192 | |
| 193 | Using the above example, if the new_post method is requested, the |
| 194 | server will expect a class to exist with this prototype:: |
| 195 | |
Derek Jones | 92763a5 | 2011-10-05 15:28:52 -0500 | [diff] [blame] | 196 | class My_blog extends CI_Controller { |
| 197 | |
Andrey Andreev | d8e1ac7 | 2012-03-26 22:22:37 +0300 | [diff] [blame] | 198 | public function new_post($request) |
| 199 | { |
Derek Jones | 92763a5 | 2011-10-05 15:28:52 -0500 | [diff] [blame] | 200 | |
Andrey Andreev | d8e1ac7 | 2012-03-26 22:22:37 +0300 | [diff] [blame] | 201 | } |
Derek Jones | 92763a5 | 2011-10-05 15:28:52 -0500 | [diff] [blame] | 202 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 203 | |
| 204 | The $request variable is an object compiled by the Server, which |
| 205 | contains the data sent by the XML-RPC Client. Using this object you will |
| 206 | have access to the *request parameters* enabling you to process the |
| 207 | request. When you are done you will send a Response back to the Client. |
| 208 | |
| 209 | Below is a real-world example, using the Blogger API. One of the methods |
Andrey Andreev | 053d5d6 | 2014-02-11 21:45:39 +0200 | [diff] [blame] | 210 | in the Blogger API is ``getUserInfo()``. Using this method, an XML-RPC |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 211 | Client can send the Server a username and password, in return the Server |
| 212 | sends back information about that particular user (nickname, user ID, |
| 213 | email address, etc.). Here is how the processing function might look:: |
| 214 | |
Derek Jones | 92763a5 | 2011-10-05 15:28:52 -0500 | [diff] [blame] | 215 | class My_blog extends CI_Controller { |
| 216 | |
Andrey Andreev | 053d5d6 | 2014-02-11 21:45:39 +0200 | [diff] [blame] | 217 | public function getUserInfo($request) |
| 218 | { |
| 219 | $username = 'smitty'; |
| 220 | $password = 'secretsmittypass'; |
Derek Jones | 92763a5 | 2011-10-05 15:28:52 -0500 | [diff] [blame] | 221 | |
Andrey Andreev | 053d5d6 | 2014-02-11 21:45:39 +0200 | [diff] [blame] | 222 | $this->load->library('xmlrpc'); |
Derek Jones | 92763a5 | 2011-10-05 15:28:52 -0500 | [diff] [blame] | 223 | |
Andrey Andreev | 053d5d6 | 2014-02-11 21:45:39 +0200 | [diff] [blame] | 224 | $parameters = $request->output_parameters(); |
Derek Jones | 92763a5 | 2011-10-05 15:28:52 -0500 | [diff] [blame] | 225 | |
Andrey Andreev | 053d5d6 | 2014-02-11 21:45:39 +0200 | [diff] [blame] | 226 | if ($parameters[1] != $username && $parameters[2] != $password) |
| 227 | { |
| 228 | return $this->xmlrpc->send_error_message('100', 'Invalid Access'); |
| 229 | } |
Derek Jones | 92763a5 | 2011-10-05 15:28:52 -0500 | [diff] [blame] | 230 | |
Andrey Andreev | 053d5d6 | 2014-02-11 21:45:39 +0200 | [diff] [blame] | 231 | $response = array( |
| 232 | array( |
| 233 | 'nickname' => array('Smitty', 'string'), |
| 234 | 'userid' => array('99', 'string'), |
| 235 | 'url' => array('http://yoursite.com', 'string'), |
| 236 | 'email' => array('jsmith@yoursite.com', 'string'), |
| 237 | 'lastname' => array('Smith', 'string'), |
| 238 | 'firstname' => array('John', 'string') |
| 239 | ), |
| 240 | 'struct' |
| 241 | ); |
Derek Jones | 92763a5 | 2011-10-05 15:28:52 -0500 | [diff] [blame] | 242 | |
Andrey Andreev | 053d5d6 | 2014-02-11 21:45:39 +0200 | [diff] [blame] | 243 | return $this->xmlrpc->send_response($response); |
| 244 | } |
Derek Jones | 92763a5 | 2011-10-05 15:28:52 -0500 | [diff] [blame] | 245 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 246 | |
| 247 | Notes: |
| 248 | ------ |
| 249 | |
Andrey Andreev | 053d5d6 | 2014-02-11 21:45:39 +0200 | [diff] [blame] | 250 | The ``output_parameters()`` method retrieves an indexed array |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 251 | corresponding to the request parameters sent by the client. In the above |
| 252 | example, the output parameters will be the username and password. |
| 253 | |
| 254 | If the username and password sent by the client were not valid, and |
Andrey Andreev | 053d5d6 | 2014-02-11 21:45:39 +0200 | [diff] [blame] | 255 | error message is returned using ``send_error_message()``. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 256 | |
| 257 | If the operation was successful, the client will be sent back a response |
| 258 | array containing the user's info. |
| 259 | |
| 260 | Formatting a Response |
| 261 | ===================== |
| 262 | |
| 263 | Similar to *Requests*, *Responses* must be formatted as an array. |
| 264 | However, unlike requests, a response is an array **that contains a |
| 265 | single item**. This item can be an array with several additional arrays, |
| 266 | but there can be only one primary array index. In other words, the basic |
| 267 | prototype is this:: |
| 268 | |
Andrey Andreev | 053d5d6 | 2014-02-11 21:45:39 +0200 | [diff] [blame] | 269 | $response = array('Response data', 'array'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 270 | |
| 271 | Responses, however, usually contain multiple pieces of information. In |
| 272 | order to accomplish this we must put the response into its own array so |
| 273 | that the primary array continues to contain a single piece of data. |
| 274 | Here's an example showing how this might be accomplished:: |
| 275 | |
Andrey Andreev | 053d5d6 | 2014-02-11 21:45:39 +0200 | [diff] [blame] | 276 | $response = array( |
| 277 | array( |
| 278 | 'first_name' => array('John', 'string'), |
| 279 | 'last_name' => array('Doe', 'string'), |
| 280 | 'member_id' => array(123435, 'int'), |
| 281 | 'todo_list' => array(array('clean house', 'call mom', 'water plants'), 'array'), |
| 282 | ), |
| 283 | 'struct' |
| 284 | ); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 285 | |
| 286 | Notice that the above array is formatted as a struct. This is the most |
| 287 | common data type for responses. |
| 288 | |
| 289 | As with Requests, a response can be one of the seven data types listed |
| 290 | in the `Data Types <#datatypes>`_ section. |
| 291 | |
| 292 | Sending an Error Response |
| 293 | ========================= |
| 294 | |
| 295 | If you need to send the client an error response you will use the |
| 296 | following:: |
| 297 | |
| 298 | return $this->xmlrpc->send_error_message('123', 'Requested data not available'); |
| 299 | |
| 300 | The first parameter is the error number while the second parameter is |
| 301 | the error message. |
| 302 | |
| 303 | Creating Your Own Client and Server |
| 304 | =================================== |
| 305 | |
| 306 | To help you understand everything we've covered thus far, let's create a |
| 307 | couple controllers that act as XML-RPC Client and Server. You'll use the |
| 308 | Client to send a request to the Server and receive a response. |
| 309 | |
| 310 | The Client |
| 311 | ---------- |
| 312 | |
Andrey Andreev | 2029231 | 2013-07-22 14:29:10 +0300 | [diff] [blame] | 313 | Using a text editor, create a controller called Xmlrpc_client.php. In |
Alan Jenkins | a51f8ec | 2012-11-19 10:29:52 +0000 | [diff] [blame] | 314 | it, place this code and save it to your application/controllers/ |
Derek Jones | 92763a5 | 2011-10-05 15:28:52 -0500 | [diff] [blame] | 315 | folder:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 316 | |
Derek Jones | 92763a5 | 2011-10-05 15:28:52 -0500 | [diff] [blame] | 317 | <?php |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 318 | |
Derek Jones | 92763a5 | 2011-10-05 15:28:52 -0500 | [diff] [blame] | 319 | class Xmlrpc_client extends CI_Controller { |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 320 | |
Andrey Andreev | d8e1ac7 | 2012-03-26 22:22:37 +0300 | [diff] [blame] | 321 | public function index() |
Derek Jones | 92763a5 | 2011-10-05 15:28:52 -0500 | [diff] [blame] | 322 | { |
| 323 | $this->load->helper('url'); |
| 324 | $server_url = site_url('xmlrpc_server'); |
| 325 | |
| 326 | $this->load->library('xmlrpc'); |
| 327 | |
| 328 | $this->xmlrpc->server($server_url, 80); |
| 329 | $this->xmlrpc->method('Greetings'); |
| 330 | |
| 331 | $request = array('How is it going?'); |
| 332 | $this->xmlrpc->request($request); |
| 333 | |
| 334 | if ( ! $this->xmlrpc->send_request()) |
| 335 | { |
| 336 | echo $this->xmlrpc->display_error(); |
| 337 | } |
| 338 | else |
| 339 | { |
| 340 | echo '<pre>'; |
| 341 | print_r($this->xmlrpc->display_response()); |
| 342 | echo '</pre>'; |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | ?> |
| 347 | |
| 348 | .. note:: In the above code we are using a "url helper". You can find more |
| 349 | information in the :doc:`Helpers Functions <../general/helpers>` page. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 350 | |
| 351 | The Server |
| 352 | ---------- |
| 353 | |
Andrey Andreev | 2029231 | 2013-07-22 14:29:10 +0300 | [diff] [blame] | 354 | Using a text editor, create a controller called Xmlrpc_server.php. In |
Alan Jenkins | a51f8ec | 2012-11-19 10:29:52 +0000 | [diff] [blame] | 355 | it, place this code and save it to your application/controllers/ |
Derek Jones | 92763a5 | 2011-10-05 15:28:52 -0500 | [diff] [blame] | 356 | folder:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 357 | |
Derek Jones | 92763a5 | 2011-10-05 15:28:52 -0500 | [diff] [blame] | 358 | <?php |
| 359 | |
| 360 | class Xmlrpc_server extends CI_Controller { |
| 361 | |
Andrey Andreev | d8e1ac7 | 2012-03-26 22:22:37 +0300 | [diff] [blame] | 362 | public function index() |
Derek Jones | 92763a5 | 2011-10-05 15:28:52 -0500 | [diff] [blame] | 363 | { |
| 364 | $this->load->library('xmlrpc'); |
| 365 | $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 | |
Andrey Andreev | d8e1ac7 | 2012-03-26 22:22:37 +0300 | [diff] [blame] | 374 | public function process($request) |
Derek Jones | 92763a5 | 2011-10-05 15:28:52 -0500 | [diff] [blame] | 375 | { |
| 376 | $parameters = $request->output_parameters(); |
| 377 | |
| 378 | $response = array( |
Andrey Andreev | 053d5d6 | 2014-02-11 21:45:39 +0200 | [diff] [blame] | 379 | array( |
| 380 | 'you_said' => $parameters[0], |
| 381 | 'i_respond' => 'Not bad at all.' |
| 382 | ), |
| 383 | 'struct' |
| 384 | ); |
Derek Jones | 92763a5 | 2011-10-05 15:28:52 -0500 | [diff] [blame] | 385 | |
| 386 | return $this->xmlrpc->send_response($response); |
| 387 | } |
| 388 | } |
Derek Jones | 92763a5 | 2011-10-05 15:28:52 -0500 | [diff] [blame] | 389 | |
| 390 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 391 | Try it! |
| 392 | ------- |
| 393 | |
| 394 | Now visit the your site using a URL similar to this:: |
| 395 | |
| 396 | example.com/index.php/xmlrpc_client/ |
| 397 | |
| 398 | You should now see the message you sent to the server, and its response |
| 399 | back to you. |
| 400 | |
| 401 | The client you created sends a message ("How's is going?") to the |
| 402 | server, along with a request for the "Greetings" method. The Server |
Andrey Andreev | 053d5d6 | 2014-02-11 21:45:39 +0200 | [diff] [blame] | 403 | receives the request and maps it to the ``process()`` method, where a |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 404 | response is sent back. |
| 405 | |
| 406 | Using Associative Arrays In a Request Parameter |
| 407 | =============================================== |
| 408 | |
| 409 | If you wish to use an associative array in your method parameters you |
| 410 | will need to use a struct datatype:: |
| 411 | |
Derek Jones | 92763a5 | 2011-10-05 15:28:52 -0500 | [diff] [blame] | 412 | $request = array( |
Andrey Andreev | 053d5d6 | 2014-02-11 21:45:39 +0200 | [diff] [blame] | 413 | array( |
| 414 | // Param 0 |
| 415 | array('name' => 'John'), |
| 416 | 'struct' |
| 417 | ), |
| 418 | array( |
| 419 | // Param 1 |
| 420 | array( |
| 421 | 'size' => 'large', |
| 422 | 'shape'=>'round' |
| 423 | ), |
| 424 | 'struct' |
| 425 | ) |
| 426 | ); |
| 427 | |
Derek Jones | 92763a5 | 2011-10-05 15:28:52 -0500 | [diff] [blame] | 428 | $this->xmlrpc->request($request); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 429 | |
| 430 | You can retrieve the associative array when processing the request in |
| 431 | the Server. |
| 432 | |
| 433 | :: |
| 434 | |
Derek Jones | 92763a5 | 2011-10-05 15:28:52 -0500 | [diff] [blame] | 435 | $parameters = $request->output_parameters(); |
Andrey Andreev | d8e1ac7 | 2012-03-26 22:22:37 +0300 | [diff] [blame] | 436 | $name = $parameters[0]['name']; |
| 437 | $size = $parameters[1]['size']; |
Michael Zimmer | d6b7cde | 2013-06-20 11:54:07 +0200 | [diff] [blame] | 438 | $shape = $parameters[1]['shape']; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 439 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 440 | Data Types |
| 441 | ========== |
| 442 | |
| 443 | According to the `XML-RPC spec <http://www.xmlrpc.com/spec>`_ there are |
| 444 | seven types of values that you can send via XML-RPC: |
| 445 | |
| 446 | - *int* or *i4* |
| 447 | - *boolean* |
| 448 | - *string* |
| 449 | - *double* |
| 450 | - *dateTime.iso8601* |
| 451 | - *base64* |
| 452 | - *struct* (contains array of values) |
| 453 | - *array* (contains array of values) |
| 454 | |
Andrey Andreev | d8afb98 | 2013-09-23 16:01:46 +0300 | [diff] [blame] | 455 | *************** |
| 456 | Class Reference |
| 457 | *************** |
| 458 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 459 | .. php:class:: CI_Xmlrpc |
Andrey Andreev | d8afb98 | 2013-09-23 16:01:46 +0300 | [diff] [blame] | 460 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 461 | .. php:method:: initialize([$config = array()]) |
Andrey Andreev | d8afb98 | 2013-09-23 16:01:46 +0300 | [diff] [blame] | 462 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 463 | :param array $config: Configuration data |
| 464 | :rtype: void |
Andrey Andreev | d8afb98 | 2013-09-23 16:01:46 +0300 | [diff] [blame] | 465 | |
| 466 | Initializes the XML-RPC library. Accepts an associative array containing your settings. |
| 467 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 468 | .. php:method:: server($url[, $port = 80[, $proxy = FALSE[, $proxy_port = 8080]]]) |
Andrey Andreev | d8afb98 | 2013-09-23 16:01:46 +0300 | [diff] [blame] | 469 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 470 | :param string $url: XML-RPC server URL |
| 471 | :param int $port: Server port |
| 472 | :param string $proxy: Optional proxy |
| 473 | :param int $proxy_port: Proxy listening port |
| 474 | :rtype: void |
Andrey Andreev | d8afb98 | 2013-09-23 16:01:46 +0300 | [diff] [blame] | 475 | |
| 476 | Sets the URL and port number of the server to which a request is to be sent:: |
| 477 | |
| 478 | $this->xmlrpc->server('http://www.sometimes.com/pings.php', 80); |
| 479 | |
Andrey Andreev | d192953 | 2014-01-07 12:16:16 +0200 | [diff] [blame] | 480 | Basic HTTP authentication is also supported, simply add it to the server URL:: |
| 481 | |
| 482 | $this->xmlrpc->server('http://user:pass@localhost/', 80); |
| 483 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 484 | .. php:method:: timeout($seconds = 5) |
Andrey Andreev | d8afb98 | 2013-09-23 16:01:46 +0300 | [diff] [blame] | 485 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 486 | :param int $seconds: Timeout in seconds |
| 487 | :rtype: void |
Andrey Andreev | d8afb98 | 2013-09-23 16:01:46 +0300 | [diff] [blame] | 488 | |
| 489 | Set a time out period (in seconds) after which the request will be canceled:: |
| 490 | |
| 491 | $this->xmlrpc->timeout(6); |
| 492 | |
Instructor, Computer Systems Technology | a09ffbc | 2016-10-18 15:28:05 -0700 | [diff] [blame] | 493 | This timeout period will be used both for an initial connection to |
| 494 | the remote server, as well as for getting a response from it. |
Andrey Andreev | dc44b92 | 2016-10-20 11:56:20 +0300 | [diff] [blame] | 495 | Make sure you set the timeout before calling ``send_request()``. |
Instructor, Computer Systems Technology | a09ffbc | 2016-10-18 15:28:05 -0700 | [diff] [blame] | 496 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 497 | .. php:method:: method($function) |
Andrey Andreev | d8afb98 | 2013-09-23 16:01:46 +0300 | [diff] [blame] | 498 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 499 | :param string $function: Method name |
| 500 | :rtype: void |
Andrey Andreev | d8afb98 | 2013-09-23 16:01:46 +0300 | [diff] [blame] | 501 | |
| 502 | Sets the method that will be requested from the XML-RPC server:: |
| 503 | |
| 504 | $this->xmlrpc->method('method'); |
| 505 | |
| 506 | Where method is the name of the method. |
| 507 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 508 | .. php:method:: request($incoming) |
Andrey Andreev | d8afb98 | 2013-09-23 16:01:46 +0300 | [diff] [blame] | 509 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 510 | :param array $incoming: Request data |
| 511 | :rtype: void |
Andrey Andreev | d8afb98 | 2013-09-23 16:01:46 +0300 | [diff] [blame] | 512 | |
| 513 | Takes an array of data and builds request to be sent to XML-RPC server:: |
| 514 | |
| 515 | $request = array(array('My Photoblog', 'string'), 'http://www.yoursite.com/photoblog/'); |
| 516 | $this->xmlrpc->request($request); |
| 517 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 518 | .. php:method:: send_request() |
Andrey Andreev | d8afb98 | 2013-09-23 16:01:46 +0300 | [diff] [blame] | 519 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 520 | :returns: TRUE on success, FALSE on failure |
| 521 | :rtype: bool |
Andrey Andreev | d8afb98 | 2013-09-23 16:01:46 +0300 | [diff] [blame] | 522 | |
| 523 | The request sending method. Returns boolean TRUE or FALSE based on success for failure, enabling it to be used conditionally. |
| 524 | |
| 525 | .. method set_debug($flag = TRUE) |
| 526 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 527 | :param bool $flag: Debug status flag |
| 528 | :rtype: void |
Andrey Andreev | d8afb98 | 2013-09-23 16:01:46 +0300 | [diff] [blame] | 529 | |
Andrey Andreev | db6f5f1 | 2014-05-16 11:05:16 +0300 | [diff] [blame] | 530 | Enables or disables debugging, which will display a variety of information and error data helpful during development. |
Andrey Andreev | d8afb98 | 2013-09-23 16:01:46 +0300 | [diff] [blame] | 531 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 532 | .. php:method:: display_error() |
Andrey Andreev | d8afb98 | 2013-09-23 16:01:46 +0300 | [diff] [blame] | 533 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 534 | :returns: Error message string |
| 535 | :rtype: string |
Andrey Andreev | d8afb98 | 2013-09-23 16:01:46 +0300 | [diff] [blame] | 536 | |
| 537 | Returns an error message as a string if your request failed for some reason. |
| 538 | :: |
| 539 | |
| 540 | echo $this->xmlrpc->display_error(); |
| 541 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 542 | .. php:method:: display_response() |
Andrey Andreev | d8afb98 | 2013-09-23 16:01:46 +0300 | [diff] [blame] | 543 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 544 | :returns: Response |
| 545 | :rtype: mixed |
Andrey Andreev | d8afb98 | 2013-09-23 16:01:46 +0300 | [diff] [blame] | 546 | |
| 547 | Returns the response from the remote server once request is received. The response will typically be an associative array. |
| 548 | :: |
| 549 | |
| 550 | $this->xmlrpc->display_response(); |
| 551 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 552 | .. php:method:: send_error_message($number, $message) |
Andrey Andreev | d8afb98 | 2013-09-23 16:01:46 +0300 | [diff] [blame] | 553 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 554 | :param int $number: Error number |
| 555 | :param string $message: Error message |
| 556 | :returns: XML_RPC_Response instance |
| 557 | :rtype: XML_RPC_Response |
Andrey Andreev | d8afb98 | 2013-09-23 16:01:46 +0300 | [diff] [blame] | 558 | |
| 559 | This method lets you send an error message from your server to the client. |
| 560 | First parameter is the error number while the second parameter is the error message. |
| 561 | :: |
| 562 | |
Connor Tumbleson | 75b3fb2 | 2014-01-11 06:58:43 -0600 | [diff] [blame] | 563 | return $this->xmlrpc->send_error_message(123, 'Requested data not available'); |
Andrey Andreev | d8afb98 | 2013-09-23 16:01:46 +0300 | [diff] [blame] | 564 | |
| 565 | .. method send_response($response) |
| 566 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 567 | :param array $response: Response data |
| 568 | :returns: XML_RPC_Response instance |
| 569 | :rtype: XML_RPC_Response |
Andrey Andreev | d8afb98 | 2013-09-23 16:01:46 +0300 | [diff] [blame] | 570 | |
| 571 | Lets you send the response from your server to the client. An array of valid data values must be sent with this method. |
| 572 | :: |
| 573 | |
| 574 | $response = array( |
| 575 | array( |
| 576 | 'flerror' => array(FALSE, 'boolean'), |
| 577 | 'message' => "Thanks for the ping!" |
| 578 | ), |
| 579 | 'struct' |
| 580 | ); |
| 581 | |
Andrey Andreev | dc44b92 | 2016-10-20 11:56:20 +0300 | [diff] [blame] | 582 | return $this->xmlrpc->send_response($response); |