send post request to xmlrpc server using codeigniter

1.2k views Asked by At

My client side:

<?php

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

class Api extends CI_Controller {

function v1($method)
{
    $server_url = site_url('xmlrpc_server');

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

    $this->xmlrpc->server($server_url, 80);
    $this->xmlrpc->method($method);
    $_POST['ip'] = $_SERVER['REMOTE_ADDR'];
    $request = array(
        array(
            $_POST,
            'struct'
        ),            
    );

    $this->xmlrpc->request($request);

    if (!$this->xmlrpc->send_request())
    {
        $json = array("api_version"=>1.0, "status"=>"failure", "site_url"=>  site_url(), "message" =>  $this->xmlrpc->display_error());
    }
    else
    {
        $json = array("api_version"=>1.0, "status"=>"good", "site_url"=>  site_url());
        $json['data'] = $this->xmlrpc->display_response();    
        echo json_encode($json);
    }
}

}

?>

Server side:

<?php

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

class Xmlrpc_server extends CI_Controller {

    function index() {
        $this->load->library('xmlrpc');
        $this->load->library('xmlrpcs');

        $config['functions']['login'] = array('function' => 'Xmlrpc_server.login');

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

    function login($request) {
        $parameters = $request->output_parameters();
        $xml_rpc_rows = array();
        if (!isset($parameters['0']["loginid"]) || !isset($parameters['0']["password"])) {
            $xml_rpc_rows['login'] = false;

            $xml_rpc_rows['message'] = "credentials required";
        } else {
            $myusername = $parameters['0']["loginid"];
            $mypassword = $parameters['0']["password"];
            $this->load->model('Users');
            $resultArray = $this->Users->userExist($myusername, $mypassword);


            if (count($resultArray) > 0) { // && $resultArray[0]['locked'] > 0
                $xml_rpc_rows['login'] = true;
                $xml_rpc_rows['message'] = "logged in successfully";
            } else {
                $xml_rpc_rows['login'] = false;
                $xml_rpc_rows['message'] = "user not found";
            }
        }

        $response = array(
            $xml_rpc_rows,
            'struct');
        return $this->xmlrpc->send_response($response);
    }
}

?>   

I am trying to post data using firebug console

var xhr = new XMLHttpRequest();
xhr.open("POST", "http://example.com/api/vi/login?");
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.send("loginid=username1&password=pass");

but getting 404 Not Found when I execute it . how can I test my xmlrpc request? I have tried passing parameters directly in the url as in REST server but unable to understand exactly how to see if my method is responding .

1

There are 1 answers

0
Mubarak On BEST ANSWER

there was a typo it should had been v1 instead of vi in the request url now it is responding fine.