这个实例是用PHP来开发,用的是CI的框架结构,没接触过CI的朋友也不要着急,这个框架和其它框架的原理是一样的,因此代码阅读起来不会有困难。

首先,代码文件分为三个文件,①client.php ②client_view.php ③server.php

先来讲一下简单的原理,再给出代码和注解。

原理:我们以QQ开放平台登录为例,其实不光QQ平台,其它任何平台原理基本上是一样的,只不过可能数据处理方式上有所差异。首先,我们将登录程序定义为客户端CLIENT端,QQ服务端定因为服务器端,我们在登陆QQ的时候,会通过我们的程序也就是浏览器发送一个请求给QQ服务器,当然你输入的ID和PASSWORD以及其他的PROFILE都是发送给服务端的参数,QQ服务端在受到CLIENT端的请求之后,接收到CLIENT的REQUEST和PARAMETERS之后,将你的ID和PASSWORD以及附加PROFILE与数据库中的相关数据进行比较,如果比较结果通过,那么QQ服务器端会将这个的相关资料数据发送给CLIENT,当然加密细节就不说了,CLIENT接收到之后对数据进行处理和提取,得到自己想要的数据,后续的工作就看CLIENT的需求了。基本原理就是这样的。好了,废话不说,代码给出。

①client.php

<?php
class Client extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
		
		$this->load->helper('url');
		$this->load->library('xmlrpc');
		
		$this->xmlrpc->set_debug(TRUE);
	}
	
	public function index()
	{
		$data['title'] = 'xmlrpc';
		$data['heading'] = 'Welcome to xmlrpc client';
		
		$this->load->view('client_view.php', $data);
	}
	
	public function request()
	{
		$data['title'] = 'xmlrpc';
		$data['heading'] = 'xmlrpc server response';

		$server_url = site_url('server');

		$this->xmlrpc->server($server_url, 80);
		$this->xmlrpc->method($this->uri->segment(3));
		
		switch($this->uri->segment(3))
		{
			case 'login':
				$request = array(
								array(
									array('request' => 'login'),
									'struct'),
								array(
									array('username' => 'inan',
										  'password' => sha1('inan')),
									'struct')
								);
				break;
			
			case 'add':
				$request = array(
								array(
									array('request' => 'add'),
									'struct'),
								array(
									array('username' => 'newuser',
										  'password' => sha1('newuser')),
									'struct')
								);
				break;
			
			case 'search':
				$request = array(
								array(
									array('request' => 'search'),
									'struct'),
								array(
									array('username' => 'inan'),
									'struct')
								);
				break;

			default:
				$request = NULL;
		}
		
		if(is_array($request))
		{
			$this->xmlrpc->request($request);
		
			if(! $this->xmlrpc->send_request())
			{
				$data['response'] = $this->xmlrpc->display_error();
			}
			else
			{
				$data['response'] = $this->xmlrpc->display_response();
			}
		}
		else
		{
			$data['response'] = array('Invalid request');
		}
		
		$this->load->view('xmlrpc_response_view.php', $data);
	}
}

②client_view.php

<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body>

	<h1><?php echo $heading; ?></h1>
	
	<p>All variables set in controllers though.</p>

	<p>
		<?php
		echo anchor('client/request/login', 'Login');
		echo '<br />';
		echo anchor('client/request/add', 'Add');
		echo '<br />';
		echo anchor('client/request/search', 'Search');
		?>
	</p>

</body>
</html>

③server.php

<?php
class Server extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
		
		$this->load->library('xmlrpc');
		$this->load->library('xmlrpcs');
		$this->load->model('database_model');
	}
	
	function index()
	{
		$config['functions']['login'] = array('function' => 'server.user_login');
		$config['functions']['add'] = array('function' => 'server.user_add');
		$config['functions']['search'] = array('function' => 'server.user_search');

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

	public function user_login($request)
	{
		$parameters = $request->output_parameters();
		
		$uid = $this->security->xss_clean($parameters['1']['username']);
		$psw = $this->security->xss_clean($parameters['1']['password']);
		
		$result = $this->database_model->user_login($uid, $psw);
		
		if($result->num_rows() == 1)
		{
			$response = array(
							array('REQUEST'		=> array($parameters['0']['request'], 'string'),
								  'RESPONSE'	=> array('successful', 'string')),
							'struct');
		
			return $this->xmlrpc->send_response($response);
		}
		else
		{
			return $this->xmlrpc->send_error_message('100', 'fail');
		}
	}
	
	public function user_add($request)
	{
		$parameters = $request->output_parameters();
		
		$uid = $this->security->xss_clean($parameters['1']['username']);
		$psw = $this->security->xss_clean($parameters['1']['password']);
		
		$result = $this->database_model->user_add($uid, $psw);
		
		if($this->db->affected_rows() == 1)
		{
			$response = array(
							array('REQUEST'		=> array($parameters['0']['request'], 'string'),
								  'RESPONSE'	=> array('successful', 'string')),
							'struct');
		
			return $this->xmlrpc->send_response($response);
		}
		else
		{
			return $this->xmlrpc->send_error_message('100', 'fail');
		}
	}
	
	public function user_search($request)
	{
		$parameters = $request->output_parameters();
		
		$uid = $this->security->xss_clean($parameters['1']['username']);
		
		$result = $this->database_model->user_search($uid);
		
		if($result->num_rows() > 0)
		{
			$user_list = array();

			foreach($result->result() as $row)
			{
				$user_list[] = array(
									array('USERNAME' =>	array($row->username, 'string'),
										  'PASSWORD' => array($row->password, 'string')),
									'struct');
			}
			
			$response = array($user_list, 'array');
			
			return $this->xmlrpc->send_response($response); 
		}
		else
		{
			return $this->xmlrpc->send_error_message('100', 'fail');
		}
	}
}
本文作者:爱帮竞人才网创始人高学平