Welcome Guest, Not a member yet? Register   Sign In
Movable Type XMLRPC Server API in Codeigniter
#1

[eluser]AdamP[/eluser]
For a project, I need to implement the Movable Type API (server side). Actually, it's only the mt.getCategoryList function. To achieve this, I am using the following code:

Code:
class Xmlrpc_server extends CI_Controller
{

    /**
     *  Constructor
     */
    function __construct()
    {
        parent::__construct();
    }


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

        $config['functions']['mt.getCategoryList' ] = array('function' => 'Xmlrpc_server.mt_getCategoryList');

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

    function mt_getCategoryList($request)
    {
        $parameters = $request->output_parameters();

        if ($parameters['1'] == 'LOGIN' && $parameters['2'] == 'PASSWORD')
        {
            $response[] =  array(
                                array(
                                    'categoryId'    =>  array('1','string'),
                                    'categoryName'     =>  array('Category #1','string')
                                ),'struct');

            $response[] =   array(
                                array(
                                    'categoryId'    =>  array('2','string'),
                                    'categoryName'     =>  array('Category #2','string')
                                ),'struct');

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

            return $this->xmlrpc->send_response($response);
        }
        else
        {
            return $this->xmlrpc->send_error_message('100', 'Invalid Access');
        }
    }
}

Now I am using this (non Codeigniter) code to check if the interface is working correctly:

Code:
$sJob='mt.getCategoryList';
$sRpcUrl = "http://www.example.com/xmlrpc";
$aParams = array('','LOGIN','PASSWORD');
$sRequest = xmlrpc_encode_request($sJob, $aParams);

echo "req:\n";
var_dump($sRequest);

$rCurl = curl_init();
curl_setopt($rCurl, CURLOPT_POSTFIELDS, $sRequest);
curl_setopt($rCurl, CURLOPT_URL, $sRpcUrl);
curl_setopt($rCurl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($rCurl, CURLOPT_TIMEOUT, 5);
curl_setopt($rCurl, CURLOPT_FOLLOWLOCATION, true);
$x = curl_exec($rCurl);
$aInfo = curl_getinfo($rCurl);
curl_close($rCurl);

echo "<pre>";
echo "info:\n";
var_dump($aInfo);

echo "result:\n";
var_dump($x);

echo "decode:\n";
var_dump(xmlrpc_decode($x));
echo "</pre>";

I get the following response:

Quote:req: string(329) " mt.getCategoryList LOGIN PASSWORD "

info:
array(20) {
["url"]=>
string(32) "http://www.example.com/xmlrpc"
["content_type"]=>
string(9) "text/html"
["http_code"]=>
int(200)
["header_size"]=>
int(181)
["request_size"]=>
int(463)
["filetime"]=>
int(-1)
["ssl_verify_result"]=>
int(0)
["redirect_count"]=>
int(0)
["total_time"]=>
float(0.406)
["namelookup_time"]=>
float(0.031)
["connect_time"]=>
float(0.078)
["pretransfer_time"]=>
float(0.078)
["size_upload"]=>
float(329)
["size_download"]=>
float(26)
["speed_download"]=>
float(64)
["speed_upload"]=>
float(810)
["download_content_length"]=>
float(26)
["upload_content_length"]=>
float(-1)
["starttransfer_time"]=>
float(0.406)
["redirect_time"]=>
float(0)
}
result:
string(26) "Disallowed Key Characters."
decode:
NULL

Compared to a working WordPress installation, I get this result:

Quote:req: string(326) " mt.getCategoryList LOGIN PASSWORD "

info:
array(20) {
["url"]=>
string(34) "http://www.wordpress-installation.com/xmlrpc.php"
["content_type"]=>
string(8) "text/xml"
["http_code"]=>
int(200)
["header_size"]=>
int(328)
["request_size"]=>
int(462)
["filetime"]=>
int(-1)
["ssl_verify_result"]=>
int(0)
["redirect_count"]=>
int(0)
["total_time"]=>
float(2.199)
["namelookup_time"]=>
float(0)
["connect_time"]=>
float(0.047)
["pretransfer_time"]=>
float(0.047)
["size_upload"]=>
float(326)
["size_download"]=>
float(580)
["speed_download"]=>
float(263)
["speed_upload"]=>
float(148)
["download_content_length"]=>
float(580)
["upload_content_length"]=>
float(-1)
["starttransfer_time"]=>
float(2.199)
["redirect_time"]=>
float(0)
}
string(580) "






categoryId3
categoryNameCategory 1


categoryId1
categoryNameCategory 2






"
decode:
array(2) {
[0]=>
array(2) {
["categoryId"]=>
string(1) "3"
["categoryName"]=>
string(17) "Category 1"
}
[1]=>
array(2) {
["categoryId"]=>
string(1) "1"
["categoryName"]=>
string(13) "Category 2"
}
}

Can someone see the error?




Theme © iAndrew 2016 - Forum software by © MyBB