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 http://example.org:8080/rpcserv/).
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.

PHP5
Apache2

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

Please follow the following links for this.

http://connectwww.com/how-to-install-and-configure-apache-php-mysql-and-phpmyadmin-on-linux-mint/1443/

https://help.ubuntu.com/community/ApacheMySQLPHP

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

var/www/
    index.php
    OpenERPPHPXMLRPC
        controllers
            xmlrpc.inc(The lib downloaded from http://phpxmlrpc.sourceforge.net/)
            login.php
            res_partner.php(a file to do partner operation)
        static
            css
                openerp_phpxmlrpc.css
            js
               openerp_phpxmlrpc.js
           lib
        views

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.

http://netbeans.org/kb/docs/php/configure-php-environment-ubuntu.html

Now let move onwards coding part.

My index.php file contains...

<html>
    <head>
        <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(){
                    this.init();
                }
                login.prototype.init = function() {
                    jQuery("#login_button").click(this.submit_login);
                }
                login.prototype.submit_login = function() {
                    params = {};
                    params['db'] = jQuery("#db").val();
                    params['username'] = jQuery("#username").val();
                    params['password'] = jQuery("#password").val();
                    params['action'] = "login"
                    jQuery.ajax({
                        type: "POST",
                        url: "/OpenERPPHPXMLRPC/Controllers/login.php",
                        data: params,
                        success: function(result){
                            console.log("Result is inside success ",result)
                            alert(result)
                        },
                        error: function(result){
                            console.log("Result is inside fail ",result)
                            alert(result)
                        }
                       
                    });
                }
                login.prototype.success_login = function(result){
                    console.log("Result is inside success ",result)
                    alert(result)
                }
                login.prototype.fail_login = function(result){
                    console.log("Result is inside fail ",result)
                    alert(result)
                }
                login_obj = new login();
            });
        </script>
    </head>
    <body>
        <div id="app">
            <div id="header">
                <div>OpenERP PHP XMLRPC Small Application</div>
            </div>
            <div id="app_content">
                <div id="login">
                    <table>
                        <tr>
                            <td colspan="2">Login</td>
                        </tr>
                        <tr>
                            <td>Database</td>
                            <td><input type="text" id="db" name="db"/></td>
                        </tr>
                        <tr>
                            <td>Username</td>
                            <td><input type="text" id="username" name="username"/></td>
                        </tr>
                        <tr>
                            <td>Password</td>
                            <td><input type="text" id="password" name="password"/></td>
                        </tr>
                        <tr>
                            <td colspan="2"><button id="login_button">Login</button></td>
                        </tr>
                    </table>
                </div>
            </div>
            <div id="footer">
                <div>Experimented by Mohammed Shekha(MSH)</div>
            </div>
        </div>
    </body>
</html>

<?php
   
?>

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...

<?php

    $db = $_POST['db'];
    $username = $_POST['username'];
    $password = $_POST['password'];
    $action = $_POST['action'];
    require_once('xmlrpc.inc');
   
    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();
        setcookie("user_id",$id,time()+3600);
        if($id > 0) {
            echo $id;
        }else{
            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

http://doc.openerp.com/v6.0/developer/6_22_XML-RPC_web_services/index.html

http://phpxmlrpc.sourceforge.net/doc-2/