Thursday, 22 November 2012

Connect OpenERP on PHP XMLRPC

XML-RPC Web services

XML-RPC is known as a web service. Web services are a set of tools that let one build distributed applications on top of existing web infrastructures. These applications use the Web as a kind of “transport layer” but don’t offer a direct human interface via the browser.[1] Extensible Markup Language (XML) provides a vocabulary for describing Remote Procedure Calls (RPC), which is then transmitted between computers using the HyperText Transfer Protocol (HTTP). Effectively, RPC gives developers a mechanism for defining interfaces that can be called over a network. These interfaces can be as simple as a single function call or as complex as a large API.
XML-RPC therefore allows two or more computers running different operating systems and programs written in different languages to share processing. For example, a Java application could talk with a Perl program, which in turn talks with Python application that talks with ASP, and so on. System integrators often build custom connections between different systems, creating their own formats and protocols to make communications possible, but one can often end up with a large number of poorly documented single-use protocols. The RPC approach spares programmers the trouble of having to learn about underlying protocols, networking, and various implementation details.
XML-RPC can be used with Python, Java, Perl, PHP, C, C++, Ruby, Microsoft’s .NET and many other programming languages. Implementations are widely available for platforms such as Unix, Linux, Windows and the Macintosh.
An XML-RPC call is conducted between two parties: the client (the calling process) and the server (the called process). A server is made available at a particular URL (such as
The above text just touches the surface of XML-RPC. I recommend O’Reilly’s “Programming Web Service with XML-RPC” for further reading. One may also wish to review the following links:

XML-RPC Architecture
OpenERP is a based on a client/server architecture. The server and the client(s) communicate using the XML-RPC protocol. XML-RPC is a very simple protocol which allows the client to do remote procedure calls. The called function, its arguments, and the result of the call are transported using HTTP and encoded using XML.

First to work with PHP install following packages.


Or either you can directly work with LAMP(Linux, apache, MySQL, PHP)  technology.

Please follow the following links for this.

Now let me explain you with example which I have created with MVC architecture,  the directory structure is as follow.

   lib downloaded from
            res_partner.php(a file to do partner operation)

Now when you write http://localhost in you browser then it will load your index.php file by default from var/www, you can configure settings of apache for loading files also you can change the directory for your project rather than var/www with apache configuration

for details settings of apache refer following link.

Now let move onwards coding part.

My index.php file contains...

        <link rel="stylesheet" type="text/css" href="OpenERPPHPXMLRPC/static/js/lib/jquery.ui/css/smoothness/jquery-ui-1.8.17.custom.css" />
        <link rel="stylesheet" type="text/css" href="OpenERPPHPXMLRPC/static/css/openerp_phpxmlrpc.css" />
        <script type="text/javascript" src="OpenERPPHPXMLRPC/static/js/openerp_phpxmlrpc.js"></script>
        <script type="text/javascript" src="OpenERPPHPXMLRPC/static/js/lib/jquery/jquery-1.6.4.js"></script>
        <script type="text/javascript" src="OpenERPPHPXMLRPC/static/js/lib/jquery.ui/js/jquery-ui-1.8.17.custom.min.js"></script>
        <script type="text/javascript">
            jQuery('document').ready(function() {
                var login = function(){
                login.prototype.init = function() {
                login.prototype.submit_login = function() {
                    params = {};
                    params['db'] = jQuery("#db").val();
                    params['username'] = jQuery("#username").val();
                    params['password'] = jQuery("#password").val();
                    params['action'] = "login"
                        type: "POST",
                        url: "/OpenERPPHPXMLRPC/Controllers/login.php",
                        data: params,
                        success: function(result){
                            console.log("Result is inside success ",result)
                        error: function(result){
                            console.log("Result is inside fail ",result)
                login.prototype.success_login = function(result){
                    console.log("Result is inside success ",result)
                login.prototype.fail_login = function(result){
                    console.log("Result is inside fail ",result)
                login_obj = new login();
        <div id="app">
            <div id="header">
                <div>OpenERP PHP XMLRPC Small Application</div>
            <div id="app_content">
                <div id="login">
                            <td colspan="2">Login</td>
                            <td><input type="text" id="db" name="db"/></td>
                            <td><input type="text" id="username" name="username"/></td>
                            <td><input type="text" id="password" name="password"/></td>
                            <td colspan="2"><button id="login_button">Login</button></td>
            <div id="footer">
                <div>Experimented by Mohammed Shekha(MSH)</div>


So this file will send a request to login.php with action login, login.php is my controller which handles this request and fetch the action(i.e.login)  now based onaction it will call function, login.php contains...


    $db = $_POST['db'];
    $username = $_POST['username'];
    $password = $_POST['password'];
    $action = $_POST['action'];
    if($action == 'login') {
        connect($db, $username, $password);
    function connect($dbname, $user, $password) {
        $server_url = 'http://localhost:8069/xmlrpc/';
        if(isset($_COOKIE["user_id"]) == true)  {
            if($_COOKIE["user_id"]>0) {
            return $_COOKIE["user_id"];

        $sock = new xmlrpc_client($server_url.'common');
        $msg = new xmlrpcmsg('login');
        $msg->addParam(new xmlrpcval($dbname, "string"));
        $msg->addParam(new xmlrpcval($user, "string"));
        $msg->addParam(new xmlrpcval($password, "string"));
        $resp =  $sock->send($msg);
        $val = $resp->value();
        $id = $val->scalarval();
        if($id > 0) {
            echo $id;
            echo -1;


What this will do it retrieves the data and send request to OpenERP Server if login got success it will redirect to Home view(which is resided in view folder) otherwise a message popup will returned.

For PHP XMLRPC detail please follow