使用codeigniter向xmlrpc服务器发送post请求

时间:2013-09-10 12:28:12

标签: php codeigniter xml-rpc xmlrpcclient

我的客户方:

<?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);
    }
}

}

?>

服务器端:

<?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);
    }

}

?>   

我正在尝试使用firebug控制台发布数据

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");

但是当我执行它时会得到404 Not Found。我怎么能测试我的xmlrpc请求? 我已尝试直接在URL中传递参数,如在REST服务器中,但无法准确了解如何查看我的方法是否响应。 谢谢你。

1 个答案:

答案 0 :(得分:0)

在请求网址中有一个错误应该是v1而不是vi现在它的响应正常。