Interaction between Odoo XML-RPC and Yii2

XML-RPC Web Service is a set of tools and specitification that allow software running on different Operating System and/or different environments to make procedure calls over the Internet.

This is the procedure call remotely via HTTP as transport and XML as the encoding. XML-RPC is designed to be as simple as possible, while allowing complex data structures to be transmitted, processed and returned.

Requirements

  1. Yii2 Basic / Advanced
  2. Odoo v8 / v9 Active Server

Installation

Download XML-RPC library from here: http://gggeek.github.io/phpxmlrpc/#download

Put files in zip into widgets directory in Yii2

Make new Widget Class in Yii2 directory named OdooXMLRPC:

namespace app\widgets;
require(__DIR__ . '\xmlrpc\xmlrpc.inc');
require(__DIR__ . '\xmlrpc\xmlrpcs.inc');
use xmlrpc_client;
use xmlrpcmsg;
use xmlrpcval;
use xmlrpcresp;
class OdooXMLRPC extends \yii\bootstrap\Widget {
    public static $user = 'admin';
public static $password = 'admin';
public static $dbname = 'dbname';
public static $server_url = 'http://server.odoo.url:8069';

public function init()
{
parent::init();
}

public static function login() {
$client = new xmlrpc_client(self::$server_url . "/xmlrpc/common");
$client->setSSLVerifyPeer(0);
		$c_msg = new xmlrpcmsg('login');
$c_msg->addParam(new xmlrpcval(self::$dbname, "string"));
$c_msg->addParam(new xmlrpcval(self::$user, "string"));
$c_msg->addParam(new xmlrpcval(self::$password, "string"));
$c_response = &$client->send($c_msg);
return $c_response;
}

public static function execute($uid, $val, $type_arr, $module, $method, $id_list=null) {
$client = new xmlrpc_client(self::$server_url . "/xmlrpc/object");
$client->setSSLVerifyPeer(0);
        $msg = new xmlrpcmsg('execute'); 
$msg->addParam(new xmlrpcval(self::$dbname, "string"));
$msg->addParam(new xmlrpcval($uid, "int"));
$msg->addParam(new xmlrpcval(self::$password, "string"));
$msg->addParam(new xmlrpcval($module, "string"));
$msg->addParam(new xmlrpcval($method, "string"));
if($id_list)
$msg->addParam(new xmlrpcval($id_list, "array"));
$msg->addParam(new xmlrpcval($val, $type_arr));
$response = $client->send($msg);
return $response;
}
}

Usage

Create

$response = OdooXMLRPC::login();
if(!$response->faultCode()) {
$uid = $response->value()->scalarval();

$val = array (
"name" => new xmlrpcval("Deny Herianto", "string"),
"company_id"=> new xmlrpcval("1", "int"),
"street" => new xmlrpcval("Jl. Asempayung 1 no. 6", "string"),
"city" => new xmlrpcval("Surabaya", "string"),
"lang" => new xmlrpcval("en_US", "string"),
"tz" => new xmlrpcval("Asia/Jakarta", "string"),
);
$response_exec = OdooXMLRPC::execute($uid, $val, "struct", "res.partner", "create");
//print_r($response_exec);
}

Search & Read

$response = OdooXMLRPC::login();
if (!$response->faultCode()) {
$uid = $response->value()->scalarval();

$domain_filter = array(
new xmlrpcval(
array(new xmlrpcval('is_company', "string"),
new xmlrpcval('!=', "string"),
new xmlrpcval("True", "string")
), "array"
),
);
$response_search = OdooXMLRPC::execute($uid, $domain_filter, "array", "res.partner", "search");
    $result = $response_search->value();
$ids = $result->scalarval();
    $id_list = array();
    for ($i = 0; $i < count($ids); $i++) {
$id_list[] = new xmlrpcval($ids[$i]->me['int'], 'int');
}
    $field_list = array(
new xmlrpcval("name", "string"),
new xmlrpcval("email", "string"),
new xmlrpcval("street", "string"),
new xmlrpcval("city", "string"),
new xmlrpcval("zip", "string"),
);
    $response_read = OdooXMLRPC::execute($uid, $field_list, "array", "res.partner", "read", $id_list);
    if (!$response_read->faultCode()) {
$result = $response_read->value()->scalarval();
echo '<h2>Result using print_r($result) :</h2>';
print_r($result);
echo '<hr />';
echo '<h2>List res.partner :</h2>';
        for ($i = 0; $i < count($result); $i++) {
if(isset($result[$i]->me['struct']['email']->me['string']))
$email = $result[$i]->me['struct']['email']->me['string'];
else
$email = $result[$i]->me['struct']['email']->me['boolean'];

if(isset($result[$i]->me['struct']['street']->me['string']))
$street = $result[$i]->me['struct']['street']->me['string'];
else
$street = $result[$i]->me['struct']['street']->me['boolean'];

if(isset($result[$i]->me['struct']['city']->me['string']))
$city = $result[$i]->me['struct']['city']->me['string'];
else
$city = $result[$i]->me['struct']['city']->me['boolean'];

echo '<h1>' . $result[$i]->me['struct']['name']->me['string'] . '</h1>'
. '<ol>'
. '<li><strong>Email</strong> : ' . $email . '</li>'
. '<li><strong>Street</strong> : ' . $street . '</li>'
. '<li><strong>City</strong> : ' . $city . '</li>'
. '</ol>'
. '<hr />';
}
}
}

Write (Update)

$response = OdooXMLRPC::login();
if (!$response->faultCode()) {
$uid = $response->value()->scalarval();
    $domain_filter = array(
new xmlrpcval(
array(new xmlrpcval('name', "string"),
new xmlrpcval('=', "string"),
new xmlrpcval("Deny Herianto", "string")
), "array"
),
);
$response_search = OdooXMLRPC::execute($uid, $domain_filter, "array", "res.partner", "search");
    $result = $response_search->value();
$ids = $result->scalarval();
    $id_list = array();
    for ($i = 0; $i < count($ids); $i++) {
$id_list[] = new xmlrpcval($ids[$i]->me['int'], 'int');
}
    $val = array ( 
"id" => new xmlrpcval("1", "int"),
"name" => new xmlrpcval("Deny", "string"),
);
$response_exec_line = OdooXMLRPC::execute($uid, $val, "account.move.line", "write", $id_list);
}

Unlink (Delete)

$response = OdooXMLRPC::login();
if (!$response->faultCode()) {
$uid = $response->value()->scalarval();
    $domain_filter = array(
new xmlrpcval(
array(new xmlrpcval('name', "string"),
new xmlrpcval('ilike', "string"),
new xmlrpcval("deny", "string")
), "array"
),
);
$response_search = OdooXMLRPC::execute($uid, $domain_filter, "array", "res.partner", "search");
    $result = $response_search->value();
$ids = $result->scalarval();
    $id_list = array();
    for ($i = 0; $i < count($ids); $i++) {
$id_list[] = new xmlrpcval($ids[$i]->me['int'], 'int');
}
    $response_unlink = OdooXMLRPC::execute($uid, $id_list, "array", "res.partner", "unlink");
    if (!$response_read->faultCode()) {
echo '<h2>List of deleted res.partner:</h2>';
        for ($i = 0; $i < count($id_list); $i++) {
echo '<li><strong>ID</strong> : ' . $id_list[$i]->me['int'] . '</li>';
}
}
}

Source: http://thierry-godin.developpez.com/openerp/openerp-xmlrpc-php-en/

Show your support

Clapping shows how much you appreciated Deny Herianto’s story.