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 | { |
| 90 | echo $this->xmlrpc->display_error(); |
| 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 | |
Derek Jones | 92763a5 | 2011-10-05 15:28:52 -0500 | [diff] [blame] | 126 | $request = array ( |
| 127 | array('John', 'string'), |
| 128 | array('Doe', 'string'), |
| 129 | array(FALSE, 'boolean'), |
| 130 | array(12345, 'int') |
| 131 | ); |
| 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 | |
| 155 | $config['functions']['new_post'] = array('function' => 'My_blog.new_entry'), |
| 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 |
| 174 | will load the My_blog class and call the update_entry function. |
| 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 |
| 184 | data through the Security library's xss_clean function. |
| 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 |
| 210 | in the Blogger API is getUserInfo(). Using this method, an XML-RPC |
| 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 | |
| 217 | function getUserInfo($request) |
| 218 | { |
| 219 | $username = 'smitty'; |
| 220 | $password = 'secretsmittypass'; |
| 221 | |
| 222 | $this->load->library('xmlrpc'); |
| 223 | |
| 224 | $parameters = $request->output_parameters(); |
| 225 | |
| 226 | if ($parameters['1'] != $username AND $parameters['2'] != $password) |
| 227 | { |
| 228 | return $this->xmlrpc->send_error_message('100', 'Invalid Access'); |
| 229 | } |
| 230 | |
| 231 | $response = array(array('nickname' => array('Smitty','string'), |
| 232 | 'userid' => array('99','string'), |
| 233 | 'url' => array('http://yoursite.com','string'), |
| 234 | 'email' => array('jsmith@yoursite.com','string'), |
| 235 | 'lastname' => array('Smith','string'), |
| 236 | 'firstname' => array('John','string') |
| 237 | ), |
| 238 | 'struct'); |
| 239 | |
| 240 | return $this->xmlrpc->send_response($response); |
| 241 | } |
| 242 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 243 | |
| 244 | Notes: |
| 245 | ------ |
| 246 | |
| 247 | The output_parameters() function retrieves an indexed array |
| 248 | corresponding to the request parameters sent by the client. In the above |
| 249 | example, the output parameters will be the username and password. |
| 250 | |
| 251 | If the username and password sent by the client were not valid, and |
| 252 | error message is returned using send_error_message(). |
| 253 | |
| 254 | If the operation was successful, the client will be sent back a response |
| 255 | array containing the user's info. |
| 256 | |
| 257 | Formatting a Response |
| 258 | ===================== |
| 259 | |
| 260 | Similar to *Requests*, *Responses* must be formatted as an array. |
| 261 | However, unlike requests, a response is an array **that contains a |
| 262 | single item**. This item can be an array with several additional arrays, |
| 263 | but there can be only one primary array index. In other words, the basic |
| 264 | prototype is this:: |
| 265 | |
| 266 | $response = array('Response data', 'array'); |
| 267 | |
| 268 | Responses, however, usually contain multiple pieces of information. In |
| 269 | order to accomplish this we must put the response into its own array so |
| 270 | that the primary array continues to contain a single piece of data. |
| 271 | Here's an example showing how this might be accomplished:: |
| 272 | |
Derek Jones | 92763a5 | 2011-10-05 15:28:52 -0500 | [diff] [blame] | 273 | $response = array ( |
| 274 | array( |
| 275 | 'first_name' => array('John', 'string'), |
| 276 | 'last_name' => array('Doe', 'string'), |
| 277 | 'member_id' => array(123435, 'int'), |
| 278 | 'todo_list' => array(array('clean house', 'call mom', 'water plants'), 'array'), |
| 279 | ), |
| 280 | 'struct' |
| 281 | ); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 282 | |
| 283 | Notice that the above array is formatted as a struct. This is the most |
| 284 | common data type for responses. |
| 285 | |
| 286 | As with Requests, a response can be one of the seven data types listed |
| 287 | in the `Data Types <#datatypes>`_ section. |
| 288 | |
| 289 | Sending an Error Response |
| 290 | ========================= |
| 291 | |
| 292 | If you need to send the client an error response you will use the |
| 293 | following:: |
| 294 | |
| 295 | return $this->xmlrpc->send_error_message('123', 'Requested data not available'); |
| 296 | |
| 297 | The first parameter is the error number while the second parameter is |
| 298 | the error message. |
| 299 | |
| 300 | Creating Your Own Client and Server |
| 301 | =================================== |
| 302 | |
| 303 | To help you understand everything we've covered thus far, let's create a |
| 304 | couple controllers that act as XML-RPC Client and Server. You'll use the |
| 305 | Client to send a request to the Server and receive a response. |
| 306 | |
| 307 | The Client |
| 308 | ---------- |
| 309 | |
| 310 | Using a text editor, create a controller called xmlrpc_client.php. In |
Alan Jenkins | a51f8ec | 2012-11-19 10:29:52 +0000 | [diff] [blame] | 311 | it, place this code and save it to your application/controllers/ |
Derek Jones | 92763a5 | 2011-10-05 15:28:52 -0500 | [diff] [blame] | 312 | folder:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 313 | |
Derek Jones | 92763a5 | 2011-10-05 15:28:52 -0500 | [diff] [blame] | 314 | <?php |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 315 | |
Derek Jones | 92763a5 | 2011-10-05 15:28:52 -0500 | [diff] [blame] | 316 | class Xmlrpc_client extends CI_Controller { |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 317 | |
Andrey Andreev | d8e1ac7 | 2012-03-26 22:22:37 +0300 | [diff] [blame] | 318 | public function index() |
Derek Jones | 92763a5 | 2011-10-05 15:28:52 -0500 | [diff] [blame] | 319 | { |
| 320 | $this->load->helper('url'); |
| 321 | $server_url = site_url('xmlrpc_server'); |
| 322 | |
| 323 | $this->load->library('xmlrpc'); |
| 324 | |
| 325 | $this->xmlrpc->server($server_url, 80); |
| 326 | $this->xmlrpc->method('Greetings'); |
| 327 | |
| 328 | $request = array('How is it going?'); |
| 329 | $this->xmlrpc->request($request); |
| 330 | |
| 331 | if ( ! $this->xmlrpc->send_request()) |
| 332 | { |
| 333 | echo $this->xmlrpc->display_error(); |
| 334 | } |
| 335 | else |
| 336 | { |
| 337 | echo '<pre>'; |
| 338 | print_r($this->xmlrpc->display_response()); |
| 339 | echo '</pre>'; |
| 340 | } |
| 341 | } |
| 342 | } |
| 343 | ?> |
| 344 | |
| 345 | .. note:: In the above code we are using a "url helper". You can find more |
| 346 | information in the :doc:`Helpers Functions <../general/helpers>` page. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 347 | |
| 348 | The Server |
| 349 | ---------- |
| 350 | |
| 351 | Using a text editor, create a controller called xmlrpc_server.php. In |
Alan Jenkins | a51f8ec | 2012-11-19 10:29:52 +0000 | [diff] [blame] | 352 | it, place this code and save it to your application/controllers/ |
Derek Jones | 92763a5 | 2011-10-05 15:28:52 -0500 | [diff] [blame] | 353 | folder:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 354 | |
Derek Jones | 92763a5 | 2011-10-05 15:28:52 -0500 | [diff] [blame] | 355 | <?php |
| 356 | |
| 357 | class Xmlrpc_server extends CI_Controller { |
| 358 | |
Andrey Andreev | d8e1ac7 | 2012-03-26 22:22:37 +0300 | [diff] [blame] | 359 | public function index() |
Derek Jones | 92763a5 | 2011-10-05 15:28:52 -0500 | [diff] [blame] | 360 | { |
| 361 | $this->load->library('xmlrpc'); |
| 362 | $this->load->library('xmlrpcs'); |
| 363 | |
| 364 | $config['functions']['Greetings'] = array('function' => 'Xmlrpc_server.process'); |
| 365 | |
| 366 | $this->xmlrpcs->initialize($config); |
| 367 | $this->xmlrpcs->serve(); |
| 368 | } |
| 369 | |
| 370 | |
Andrey Andreev | d8e1ac7 | 2012-03-26 22:22:37 +0300 | [diff] [blame] | 371 | public function process($request) |
Derek Jones | 92763a5 | 2011-10-05 15:28:52 -0500 | [diff] [blame] | 372 | { |
| 373 | $parameters = $request->output_parameters(); |
| 374 | |
| 375 | $response = array( |
Andrey Andreev | d8e1ac7 | 2012-03-26 22:22:37 +0300 | [diff] [blame] | 376 | array( |
| 377 | 'you_said' => $parameters[0], |
| 378 | 'i_respond' => 'Not bad at all.' |
| 379 | ), |
| 380 | 'struct' |
| 381 | ); |
Derek Jones | 92763a5 | 2011-10-05 15:28:52 -0500 | [diff] [blame] | 382 | |
| 383 | return $this->xmlrpc->send_response($response); |
| 384 | } |
| 385 | } |
| 386 | ?> |
| 387 | |
| 388 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 389 | Try it! |
| 390 | ------- |
| 391 | |
| 392 | Now visit the your site using a URL similar to this:: |
| 393 | |
| 394 | example.com/index.php/xmlrpc_client/ |
| 395 | |
| 396 | You should now see the message you sent to the server, and its response |
| 397 | back to you. |
| 398 | |
| 399 | The client you created sends a message ("How's is going?") to the |
| 400 | server, along with a request for the "Greetings" method. The Server |
| 401 | receives the request and maps it to the "process" function, where a |
| 402 | response is sent back. |
| 403 | |
| 404 | Using Associative Arrays In a Request Parameter |
| 405 | =============================================== |
| 406 | |
| 407 | If you wish to use an associative array in your method parameters you |
| 408 | will need to use a struct datatype:: |
| 409 | |
Derek Jones | 92763a5 | 2011-10-05 15:28:52 -0500 | [diff] [blame] | 410 | $request = array( |
| 411 | array( |
| 412 | // Param 0 |
| 413 | array( |
| 414 | 'name'=>'John' |
| 415 | ), |
| 416 | 'struct' |
| 417 | ), |
| 418 | array( |
| 419 | // Param 1 |
| 420 | array( |
| 421 | 'size'=>'large', |
| 422 | 'shape'=>'round' |
| 423 | ), |
| 424 | 'struct' |
| 425 | ) |
| 426 | ); |
| 427 | $this->xmlrpc->request($request); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 428 | |
| 429 | You can retrieve the associative array when processing the request in |
| 430 | the Server. |
| 431 | |
| 432 | :: |
| 433 | |
Derek Jones | 92763a5 | 2011-10-05 15:28:52 -0500 | [diff] [blame] | 434 | $parameters = $request->output_parameters(); |
Andrey Andreev | d8e1ac7 | 2012-03-26 22:22:37 +0300 | [diff] [blame] | 435 | $name = $parameters[0]['name']; |
| 436 | $size = $parameters[1]['size']; |
Michael Zimmer | d6b7cde | 2013-06-20 11:54:07 +0200 | [diff] [blame] | 437 | $shape = $parameters[1]['shape']; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 438 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 439 | Data Types |
| 440 | ========== |
| 441 | |
| 442 | According to the `XML-RPC spec <http://www.xmlrpc.com/spec>`_ there are |
| 443 | seven types of values that you can send via XML-RPC: |
| 444 | |
| 445 | - *int* or *i4* |
| 446 | - *boolean* |
| 447 | - *string* |
| 448 | - *double* |
| 449 | - *dateTime.iso8601* |
| 450 | - *base64* |
| 451 | - *struct* (contains array of values) |
| 452 | - *array* (contains array of values) |
| 453 | |
Andrey Andreev | d8afb98 | 2013-09-23 16:01:46 +0300 | [diff] [blame] | 454 | *************** |
| 455 | Class Reference |
| 456 | *************** |
| 457 | |
| 458 | .. class:: CI_Xmlrpc |
| 459 | |
| 460 | .. method:: initialize([$config = array()]) |
| 461 | |
| 462 | :param array $config: configuration data |
| 463 | :returns: void |
| 464 | |
| 465 | Initializes the XML-RPC library. Accepts an associative array containing your settings. |
| 466 | |
| 467 | .. method:: server($url[, $port = 80[, $proxy = FALSE[, $proxy_port = 8080]]]) |
| 468 | |
| 469 | :param string $url: XML-RPC server URL |
| 470 | :param int $port: server port |
| 471 | :param string $proxy: optional proxy |
| 472 | :param int $proxy_port: proxy listening port |
| 473 | :returns: void |
| 474 | |
| 475 | Sets the URL and port number of the server to which a request is to be sent:: |
| 476 | |
| 477 | $this->xmlrpc->server('http://www.sometimes.com/pings.php', 80); |
| 478 | |
Andrey Andreev | d192953 | 2014-01-07 12:16:16 +0200 | [diff] [blame] | 479 | Basic HTTP authentication is also supported, simply add it to the server URL:: |
| 480 | |
| 481 | $this->xmlrpc->server('http://user:pass@localhost/', 80); |
| 482 | |
Andrey Andreev | d8afb98 | 2013-09-23 16:01:46 +0300 | [diff] [blame] | 483 | .. method:: timeout($seconds = 5) |
| 484 | |
| 485 | :param int $seconds: timeout in seconds |
| 486 | :returns: void |
| 487 | |
| 488 | Set a time out period (in seconds) after which the request will be canceled:: |
| 489 | |
| 490 | $this->xmlrpc->timeout(6); |
| 491 | |
| 492 | .. method:: method($function) |
| 493 | |
| 494 | :param string $function: method name |
| 495 | :returns: void |
| 496 | |
| 497 | Sets the method that will be requested from the XML-RPC server:: |
| 498 | |
| 499 | $this->xmlrpc->method('method'); |
| 500 | |
| 501 | Where method is the name of the method. |
| 502 | |
| 503 | .. method:: request($incoming) |
| 504 | |
| 505 | :param array $incoming: request data |
| 506 | :returns: void |
| 507 | |
| 508 | Takes an array of data and builds request to be sent to XML-RPC server:: |
| 509 | |
| 510 | $request = array(array('My Photoblog', 'string'), 'http://www.yoursite.com/photoblog/'); |
| 511 | $this->xmlrpc->request($request); |
| 512 | |
| 513 | .. method:: send_request() |
| 514 | |
| 515 | :returns: bool |
| 516 | |
| 517 | The request sending method. Returns boolean TRUE or FALSE based on success for failure, enabling it to be used conditionally. |
| 518 | |
| 519 | .. method set_debug($flag = TRUE) |
| 520 | |
| 521 | :param bool $flag: debug status flag |
| 522 | :returns: void |
| 523 | |
| 524 | Enables or disables debugging, which will display a variety of information and error data helpful during development. |
| 525 | |
| 526 | .. method:: display_error() |
| 527 | |
| 528 | :returns: string |
| 529 | |
| 530 | Returns an error message as a string if your request failed for some reason. |
| 531 | :: |
| 532 | |
| 533 | echo $this->xmlrpc->display_error(); |
| 534 | |
| 535 | .. method:: display_response() |
| 536 | |
| 537 | :returns: mixed |
| 538 | |
| 539 | Returns the response from the remote server once request is received. The response will typically be an associative array. |
| 540 | :: |
| 541 | |
| 542 | $this->xmlrpc->display_response(); |
| 543 | |
| 544 | .. method:: send_error_message($number, $message) |
| 545 | |
| 546 | :param int $number: error number |
| 547 | :param string $message: error message |
| 548 | :returns: object |
| 549 | |
| 550 | This method lets you send an error message from your server to the client. |
| 551 | First parameter is the error number while the second parameter is the error message. |
| 552 | :: |
| 553 | |
| 554 | return $this->xmlrpc->send_error_message(123, 'Requested data not available'); |
| 555 | |
| 556 | .. method send_response($response) |
| 557 | |
| 558 | :param array $response: response data |
| 559 | :returns: object |
| 560 | |
| 561 | Lets you send the response from your server to the client. An array of valid data values must be sent with this method. |
| 562 | :: |
| 563 | |
| 564 | $response = array( |
| 565 | array( |
| 566 | 'flerror' => array(FALSE, 'boolean'), |
| 567 | 'message' => "Thanks for the ping!" |
| 568 | ), |
| 569 | 'struct' |
| 570 | ); |
| 571 | |
| 572 | return $this->xmlrpc->send_response($response); |