Query XMLRPC doo with PHP

Updated example of the webkul guys. Check references.

Install phpxmlrpc

Download tarball from website https://github.com/gggeek/phpxmlrpc/releases and copy src/ directory to the working directory.

Example

The following example authenticates on database and creates a customer:

<?php
require_once __DIR__.'/src/Autoloader.php';
PhpXmlRpc\Autoloader::register();

use PhpXmlRpc\Value;
use PhpXmlRpc\Request;
use PhpXmlRpc\Client;

$user_id = 'None';
$dbname = 'nameofdatabase';
$user = 'yourusername';
$pwd = 'yourpassword';
$url = 'http://192.168.122.94:8069'; // it's demo server url
$sock = new PhpXmlRpc\Client($url.'/xmlrpc/2/common');

$sock_msg = new PhpXmlRpc\Request('login');
$sock_msg->addParam(new PhpXmlRpc\Value($dbname, "string"));
$sock_msg->addParam(new PhpXmlRpc\Value($user, "string"));
$sock_msg->addParam(new PhpXmlRpc\Value	($pwd, "string"));
$sock_resp = $sock->send($sock_msg);

if ($sock_resp->errno != 0){
	echo  'error';
}
else {

	$sock_val = $sock_resp->value();
	$user_id = $sock_val->scalarval();
}
if ($user_id != "None") {
	$customer_array = array(
		'name'=>new PhpXmlRpc\Value('Demo Customer', "string"),
		'customer_rank'=>new PhpXmlRpc\Value(1, "int")
	);
	$client = new PhpXmlRpc\Client($url.'/xmlrpc/2/object');
	$msg = new PhpXmlRpc\Request('execute');
	$msg->addParam(new PhpXmlRpc\Value($dbname, "string"));
	$msg->addParam(new PhpXmlRpc\Value($user_id, "int"));
	$msg->addParam(new PhpXmlRpc\Value($pwd, "string"));
	$msg->addParam(new PhpXmlRpc\Value("res.partner", "string"));
	$msg->addParam(new PhpXmlRpc\Value("create", "string"));
	$msg->addParam(new PhpXmlRpc\Value($customer_array, "struct"));
	$resp = $client->send($msg);
	$customer_id = $resp->value()->scalarval();
}
?>

References

query_xmlrpc_odoo_with_php.txt · Last modified: 2023/04/27 18:01
Public Domain Except where otherwise noted, content on this wiki is licensed under the following license: Public Domain