CodeIgniter and XMLRPC

What are we going to do?

We’ll mimic what we already achieved in CodeIgniter and NuSOAP post using XMLRPC method.

So, we’ll create two endpoints, one is for getting a CD info (function name : getCdInfo) and the other is for getting all of CDs listed (function name : getCds).

We’ll still implement all of our function inside Welcome controller. The difference is that the registration process is placed inside index() function while the implementation is outside index() function but still within Welcome class.

The Server

Here’s the configuration and registration process inside index() function:

        public function index()
    {
    $this->load->library('xmlrpc');
        $this->load->library('xmlrpcs');
        
        //REGISTRATION AND CONFIGURATION
        $config['functions']['getCdInfo'] = array('function' => 'Welcome.getCdInfo');
        $config['functions']['getCds'] = array('function' => 'Welcome.getCds');
        $config['object'] = $this;

        $this->xmlrpcs->initialize($config);
        $this->xmlrpcs->serve();
    }

Notice how I defined the endpoint name and register it. So, getCdInfo endpoint will be implemented in Welcome controller and getCdInfo function, while getCds endpoint will be implemented in Welcome controller as getCds function.
Now, here’s the implementation:

        //IMPLEMENTATION
    public function getCdInfo($request)
    {
    $this->load->library('xmlrpc');

        $parameters = $request->output_parameters();

        $id = $parameters[0];  // first parameter
        $this->db->where('id', $id);
        $qcd = $this->db->get('cds');

        if ($qcd->num_rows()==1) {
            $ret = $qcd->row_array();
        } else {
            return $this->xmlrpc->send_error_message('404', 'Invalid results');
        }

        $response = array(
            array(
                'titel'  => array($ret['titel'], 'string'),
                'interpret'    => array($ret['interpret'], 'string'),
                'jahr'       => array($ret['jahr'], 'int'),
                'id'     => array($ret['id'], 'int')
            ),
             'struct'
        );

        return $this->xmlrpc->send_response($response);
    }

    public function getCds()
    {
    $this->load->library('xmlrpc');

        $qcd = $this->db->get('cds');

        if ($qcd->num_rows()>0) {
            $ret = array();
            $i = 0;
            foreach ($qcd->result_array() as $row) {
                $ret[$i] = array(array(
                        'titel'  => array($row['titel'], 'string'),
                        'interpret'    => array($row['interpret'], 'string'),
                        'jahr'       => array($row['jahr'], 'int'),
                        'id'     => array($row['id'], 'int')
                ), 'struct');
                $i++;
            }
        } else {
            return $this->xmlrpc->send_error_message('404', 'Empty results');
        }

        $response = array(
                $ret,
                 'array'
        );

        return $this->xmlrpc->send_response($response);
    }

getCdInfo takes one parameter, so, we need to pass $request as getCdInfo function parameter while getCds function doesn’t take any parameter, so we omit it.
Pay attention on getCds function, you’ll learn how I implement an XMLRPC endpoint that return an array of array (a data table).

The Client

Same as CodeIgniter and NuSOAP, for simplicity, we’ll implement the client as part of Welcome controller. Here’s the client code:

        public function testClient()
    {
    $server_url = site_url('welcome');

        $this->load->library('xmlrpc');

        //Call getCdInfo
        $this->xmlrpc->server($server_url, 80);
        $this->xmlrpc->method('getCdInfo');

        $request = array(array(1, 'int'));
        $this->xmlrpc->request($request);

        if ( ! $this->xmlrpc->send_request())
        {
            echo $this->xmlrpc->display_error();
        }
        else
        {
            var_dump($this->xmlrpc->display_response());
        }

        //Call getCDs
        $this->xmlrpc->method('getCds');

        if ( ! $this->xmlrpc->send_request())
        {
            echo $this->xmlrpc->display_error();
        }
        else
        {
            var_dump($this->xmlrpc->display_response());
        }
    }

getCdInfo call will output something like this:

array(4) {
  ["titel"]=>
  string(6) "Beauty"
  ["interpret"]=>
  string(16) "Ryuichi Sakamoto"
  ["jahr"]=>
  string(4) "1990"
  ["id"]=>
  string(1) "1"
}

While getCds will return something like this:

array(3) {
  [0]=>
  array(4) {
    ["titel"]=>
    string(6) "Beauty"
    ["interpret"]=>
    string(16) "Ryuichi Sakamoto"
    ["jahr"]=>
    string(4) "1990"
    ["id"]=>
    string(1) "1"
  }
  [1]=>
  array(4) {
    ["titel"]=>
    string(33) "Goodbye Country (Hello Nightclub)"
    ["interpret"]=>
    string(13) "Groove Armada"
    ["jahr"]=>
    string(4) "2001"
    ["id"]=>
    string(1) "4"
  }
  [2]=>
  array(4) {
    ["titel"]=>
    string(4) "Glee"
    ["interpret"]=>
    string(13) "Bran Van 3000"
    ["jahr"]=>
    string(4) "1997"
    ["id"]=>
    string(1) "5"
  }
}

As you can see, the output of XMLRPC method is the same as NuSOAP method. No fixing needed!
Here’s the complete source code of our controller:

defined('BASEPATH') or exit('No direct script access allowed');

class Welcome extends CI_Controller 
{
    public function index()
    {
    $this->load->library('xmlrpc');
        $this->load->library('xmlrpcs');

        //REGISTRATION AND CONFIGURATION
        $config['functions']['getCdInfo'] = array('function' => 'Welcome.getCdInfo');
        $config['functions']['getCds'] = array('function' => 'Welcome.getCds');
        $config['object'] = $this;

        $this->xmlrpcs->initialize($config);
        $this->xmlrpcs->serve();
    }


    //IMPLEMENTATION
    public function getCdInfo($request)
    {
    $this->load->library('xmlrpc');

        $parameters = $request->output_parameters();

        $id = $parameters[0];  // first parameter
        $this->db->where('id', $id);
        $qcd = $this->db->get('cds');

        if ($qcd->num_rows()==1) {
            $ret = $qcd->row_array();
        } else {
            return $this->xmlrpc->send_error_message('404', 'Invalid results');
        }

        $response = array(
            array(
                'titel'  => array($ret['titel'], 'string'),
                'interpret'    => array($ret['interpret'], 'string'),
                'jahr'       => array($ret['jahr'], 'int'),
                'id'     => array($ret['id'], 'int')
            ),
             'struct'
        );

        return $this->xmlrpc->send_response($response);
    }

    public function getCds()
    {
    $this->load->library('xmlrpc');

        $qcd = $this->db->get('cds');

        if ($qcd->num_rows()>0) {
            $ret = array();
            $i = 0;
            foreach ($qcd->result_array() as $row) {
                $ret[$i] = array(array(
                        'titel'  => array($row['titel'], 'string'),
                        'interpret'    => array($row['interpret'], 'string'),
                        'jahr'       => array($row['jahr'], 'int'),
                        'id'     => array($row['id'], 'int')
                ), 'struct');
                $i++;
            }
        } else {
            return $this->xmlrpc->send_error_message('404', 'Empty results');
        }

        $response = array(
                $ret,
                 'array'
        );

        return $this->xmlrpc->send_response($response);
    }

    public function testClient()
    {
    $server_url = site_url('welcome');

        $this->load->library('xmlrpc');

        //Call getCdInfo
        $this->xmlrpc->server($server_url, 80);
        $this->xmlrpc->method('getCdInfo');

        $request = array(array(1, 'int'));
        $this->xmlrpc->request($request);

        if ( ! $this->xmlrpc->send_request())
        {
            echo $this->xmlrpc->display_error();
        }
        else
        {
            var_dump($this->xmlrpc->display_response());
        }

        //Call getCDs
        $this->xmlrpc->method('getCds');

        if ( ! $this->xmlrpc->send_request())
        {
            echo $this->xmlrpc->display_error();
        }
        else
        {
            var_dump($this->xmlrpc->display_response());
        }
    }
}

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>