How to use PHP7 Ripcord library to get Odoo data?

2020-02-15 05:16发布

问题:

I am trying to get some data from Odoo through XMLRPC, and I am working with PHP and its Ripcord library (recommended on https://www.odoo.com/documentation/8.0/api_integration.html).

So I am following the steps written on that page.

Firstly, I downloaded the Ripcord files from https://github.com/poef/ripcord.git. I saved them in a folder named ripcord, located at the index directory of my PHP page.

Secondly, I enabled the OpenSSL and XMLRPC extensions for PHP7. I think I did it well because if I execute the next sentence:

$modules = get_loaded_extensions();
foreach ($modules as $module) {
    echo $module.', ';
}

I get this result:

Core, date, libxml, openssl, pcre, zlib, filter, hash, Reflection, SPL, session, standard, apache2handler, mysqlnd, PDO, xml, calendar, ctype, curl, dom, mbstring, fileinfo, ftp, gd, gettext, iconv, json, exif, mcrypt, mysqli, pdo_mysql, Phar, posix, readline, shmop, SimpleXML, sockets, sysvmsg, sysvsem, sysvshm, tokenizer, wddx, xmlreader, xmlrpc, xmlwriter, xsl, Zend OPcache,

Now, this is the code of my index.php:

$url = 'http://localhost:30080';
$db = 'db_v80_test_01';
$username = 'admin';
$password = 'adminpwd';

require_once('ripcord/ripcord.php');

// $info = ripcord::client($url)->start(); 
// list($url, $db, $username, $password) = array($info['host'], $info['database'], $info['user'], $info['password']);

$common = ripcord::client($url.'/xmlrpc/2/common');

$uid = $common->authenticate($db, $username, $password, array());
die($uid);

The problem is that I am getting nothing in $uid variable. Can anyone tell me what is happening?

NOTE

May be this question is duplicated: Odoo API web service doesn't return anything

But as it had no answers, I tried to give more information on mine.

回答1:

Ok, I had not error log enabled in php.ini, so I always get nothing. If I had enabled it earlier, I would have seen that the error was that I was trying to print a kind of value I cannot print, so the problem was in the die command.

Now, it is working perfectly with this code:

$url = 'http://localhost:30080';
$db = 'db_v80_test_01';
$username = 'admin';
$password = 'adminpwd';

require_once('ripcord/ripcord.php');

$common = ripcord::client($url.'/xmlrpc/2/common');
$uid = $common->authenticate($db, $username, $password, array());
$models = ripcord::client("$url/xmlrpc/2/object");
$partners = $models->execute_kw(
    $db,
    $uid,
    $password,
    'res.partner',
    'search',
    array(
        array(
            array('is_company', '=', true),
            array('customer', '=', true)
        )
    )
);

echo('RESULT:<br/>');
foreach ($partners as $partner) {
    echo $partner.'<br/>';
}