CI官方文档推荐的RPC库是XMLrpc,其中官方demo如下
客户端
使用文本编辑器创建一个控制器 Xmlrpc_client.php ,在这个控制器中, 粘贴以下的代码并保存到 applications/controllers/ 目录:
<?php
class Xmlrpc_client extends CI_Controller {
public function index()
{
$this->load->helper('url');
$server_url = site_url('xmlrpc_server');
$this->load->library('xmlrpc');
$this->xmlrpc->server($server_url, 80);
$this->xmlrpc->method('Greetings');
$request = array('How is it going?');
$this->xmlrpc->request($request);
if ( ! $this->xmlrpc->send_request())
{
echo $this->xmlrpc->display_error();
}
else
{
echo '<pre>';
print_r($this->xmlrpc->display_response());
echo '</pre>';
}
}
}
?>
服务端
使用文本编辑器创建一个控制器 Xmlrpc_server.php ,在这个控制器中, 粘贴以下的代码并保存到 applications/controllers/ 目录:
<?php
class Xmlrpc_server extends CI_Controller {
public function index()
{
$this->load->library('xmlrpc');
$this->load->library('xmlrpcs');
$config['functions']['Greetings'] = array('function' => 'Xmlrpc_server.process');
$this->xmlrpcs->initialize($config);
$this->xmlrpcs->serve();
}
public function process($request)
{
$parameters = $request->output_parameters();
$response = array(
array(
'you_said' => $parameters[0],
'i_respond' => 'Not bad at all.'
),
'struct'
);
return $this->xmlrpc->send_response($response);
}
}
使用的版本是CodeIgniter v2.1.0
然而运行结果是(换3.1.9版本也同样报错)
The XML data received was either invalid or not in the correct form
for XML-RPC. Turn on debugging to examine the XML data further.
要看实际返回值, 猜测基本上是服务器上报错了. 根据错误, 调整代码.