Welcome Guest, Not a member yet? Register   Sign In
XML-RPC using HTTP Basic Authentification
#5

[eluser]sramage[/eluser]
I don't know how put it on the wiki so I post here:

XML-RPC client library:

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
Client xml-rpc utilisant la bibliothèque xmlrpc de sourceforge
http://phpxmlrpc.sourceforge.net
*/
include(APPPATH.'libraries/xmlrpc/xmlrpc.inc');

class Xmlrpc_sf extends xmlrpc_client{
  var $method_name;
  var $request_val;
  var $fault_code;
  var $fault_string;
  var $response;
  
  function Xmlrpc_sf($params)
  {
    $server_url = $params[0];
    parent::xmlrpc_client($server_url);

  }
  function method($method) #indique la methode à utiliser
  {
    $this->method_name = $method;
  }
  function request($request=null) #indique les paramètres à envoyer pour la méthode
  {
    $this->request_val = php_xmlrpc_encode($request);
  }
  function send_request()
  {
    $message = new xmlrpcmsg($this->method_name,array($this->request_val));
    $response = $this->send($message);
    $this->fault_code = $response->faultCode();
    $this->fault_string = 'Error : '.$this->fault_code.' '.$response->faultString();
    if ($this->fault_code == 0){
      $this->response = $response->value();
    }
    return $this->fault_code == 0;
  }
  function display_error()
  {
    return $this->fault_string;
  }
  function display_response()
  {
    return php_xmlrpc_decode($this->response);
  }
}

?>

XML-RPC server library
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
Serveur xml-rpc utilisant la bibliothèque xmlrpc de sourceforge
http://phpxmlrpc.sourceforge.net
*/
include(APPPATH.'libraries/xmlrpc/xmlrpc.inc');
include(APPPATH.'libraries/xmlrpc/xmlrpcs.inc');

class Xmlrpcs_sf extends xmlrpc_server{

  function Xmlrpcs_sf($functions)
  {
    $functions['test.test'] = array('function' => array($this,'test'));
    $functions['test.echo_test'] = array('function' => array($this,'echo_test'));
    
    parent::xmlrpc_server($functions,0);
  }
  function send_response($resp)
  {
    $val_resp = php_xmlrpc_encode($resp);
    return new xmlrpcresp($val_resp);
  }
  
  #test functions
  function test()
  {
    #$response = array(date('d/m/Y H:i:s'),'string');
    $response = date('d/m/Y H:i:s');
    return $this->send_response($response);
  }
  function echo_test($request)
  {
    $params = php_xmlrpc_decode($request);
    $response = 'you said : "'.$params[0].'"';
    return $this->send_response($response);
  }

}

?>


the folder APPPATH/libraries/xmlrpc contain the xmlrpc files.

usage example :
client :
Code:
$server_url = 'http://username:[email protected]/index.php?/rpc/xmlrpc_srv';
$this->load->library('xmlrpc_sf',array($server_url));
    
$this->xmlrpc_sf->setRequestCompression('gzip');
$this->xmlrpc_sf->setAcceptedCompression('gzip');
$this->xmlrpc_sf->request_charset_encoding = 'UTF-8';

$this->xmlrpc_sf->request();
$this->xmlrpc_sf->method('test.test');
$response = $this->xmlrpc_sf->send_request();
echo $this->xmlrpc_sf->display_response();

$this->xmlrpc_sf->request('this is a string');
$this->xmlrpc_sf->method('my.function');
$response = $this->xmlrpc_sf->send_request();
echo $this->xmlrpc_sf->display_response();

server:

Code:
<?php

class Xmlrpc_srv extends Controller {

    function Xmlrpc_srv()
    {
        parent::Controller();    
        
        $RPC_USER = "username";
    $RPC_PASSWORD = "password";
        
      #Authentification

    $user = $this->input->server('PHP_AUTH_USER');
    $password = $this->input->server('PHP_AUTH_PW');
    if ($user != $RPC_USER OR $password != $RPC_PASSWORD){
      Header('WWW-Authenticate: Basic realm="rpc"');
      Header('HTTP/1.0 401 Unauthorized');
      exit();
    }

    }
        function index()
    {

  
    $functions = array();
$functions['my.function'] = array('function' => array($this,'sample_function'));
              
    $this->load->library('xmlrpcs_sf',$functions);
        $this->xmlrpcs_sf->service();
    }
function sample_function($request)
  {
    $params = php_xmlrpc_decode($request);
    $response = 'you said : "'.$params[0].'"';
    return $this->send_response($response);
  }
?>


Messages In This Thread
XML-RPC using HTTP Basic Authentification - by El Forum - 11-05-2008, 07:02 AM
XML-RPC using HTTP Basic Authentification - by El Forum - 11-06-2008, 07:35 AM
XML-RPC using HTTP Basic Authentification - by El Forum - 11-06-2008, 08:05 AM
XML-RPC using HTTP Basic Authentification - by El Forum - 11-06-2008, 08:43 AM
XML-RPC using HTTP Basic Authentification - by El Forum - 11-07-2008, 02:00 AM
XML-RPC using HTTP Basic Authentification - by El Forum - 06-04-2009, 08:44 PM
XML-RPC using HTTP Basic Authentification - by El Forum - 06-05-2009, 12:31 AM
XML-RPC using HTTP Basic Authentification - by El Forum - 07-28-2011, 06:06 AM



Theme © iAndrew 2016 - Forum software by © MyBB