我能够进行身份验证,但是当尝试获取数据时,它给了我错误
这是我遇到的错误
有谁可以帮我快速解决这个问题吗?
我正在使用 Laravel 版本 9.46,通过 php artisan -v 检查使用情况
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use PhpXmlRpc\Client;
use PhpXmlRpc\Request;
use PhpXmlRpc\Value;
use DB;
class FetchOdooData extends Command
{
protected $signature = 'fetch:odoo-data';
protected $description = 'Fetch data from Odoo and update local database';
public function __construct()
{
parent::__construct();
}
public function handle()
{
// Odoo credentials
$url = 'http://urltoodoo::8049';
$db = 'DBNAME';
$username = 'username';
$password = 'APIKEY';
// Define XML-RPC endpoints
$common_url = "{$url}/xmlrpc/2/common";
$object_url = "{$url}/xmlrpc/2/object";
// Create a client instance for common services
$client = new Client($common_url);
// Prepare the authentication request
$request = new Request('authenticate', array(
new Value($db, 'string'),
new Value($username, 'string'),
new Value($password, 'string'),
new Value(array(), 'struct')
));
// Send the authentication request
$response = $client->send($request);
//var_dump($response);
if ($response->faultCode()) {
$this->error('Error: ' . $response->faultString());
return;
}
// Retrieve the user ID from the response
$user_id = $response->value()->scalarval();
if ($user_id) {
$this->info("Authenticated successfully. User ID: $user_id");
} else {
$this->error("Authentication failed.");
return;
}
// Function to call Odoo methods
function callOdooMethod($client, $method, $params) {
var_dump($method);
var_dump($params);
var_dump($client);
$msg = new Request($method, array(new Value($params, 'array')));
var_dump($msg);
$resp = $client->send($msg);
var_dump($resp);
if ($resp->faultCode()) {
return 'Error: ' . $resp->faultString();
}
return $resp->value();
}
// Fetch last sync time for a model
function getLastSyncTime($model_name) {
return DB::table('last_sync')->where('model_name', $model_name)->value('last_update');
}
// Update last sync time for a model
function updateLastSyncTime($model_name) {
DB::table('last_sync')->updateOrInsert(
['model_name' => $model_name],
['last_update' => now()]
);
}
// Fetch data from Odoo
function fetchData($models, $db, $user_id, $password, $model_name, $fields, $last_sync_time=0) {
//$domain = $last_sync_time ? [['write_date', '>', $last_sync_time]] : [];
$params = array(
new Value($db, 'string'),
new Value($user_id, 'int'),
new Value($password, 'string'),
new Value($model_name, 'string'),
new Value('search_read', 'string'),
new Value(array(
//new Value($domain, 'array'), // Domain filter
new Value($fields, 'array'), // Fields to retrieve
), 'array')
);
return callOdooMethod($models, 'execute_kw', $params);
}
// Update local database with fetched data
function updateLocalDatabase($model_name, $data) {
foreach ($data as $record) {
}
}
// Create a client instance for object services
$models = new Client($object_url);
var_dump($models);
// Define fields to retrieve for each model
$project_fields = array('name', 'user_id', 'date_start', 'date_end', 'tasks');
$task_fields = array('name', 'user_id', 'date_deadline', 'stage_id', 'description');
$ticket_fields = array('name', 'stage_id', 'user_id', 'priority', 'description');
$incident_fields = array('name', 'stage_id', 'user_id', 'priority', 'description'); // Replace with actual fields
// Define models
$models_to_fetch = array(
'project.project' => $project_fields,
'project.task' => $task_fields,
'helpdesk.ticket' => $ticket_fields
);
/*,
'your_incident_model_name' => $incident_fields // Replace with actual model name */
foreach ($models_to_fetch as $model_name => $fields) {
//$last_sync_time = getLastSyncTime($model_name);
$data = fetchData($models, $db, $user_id, $password, $model_name, $fields); //, $last_sync_time
if (is_string($data)) {
$this->error("Error fetching $model_name: " . $data);
} else {
$this->info(ucfirst(str_replace('.', ' ', $model_name)) . " Data:\n");
print_r($data);
updateLocalDatabase($model_name, $data);
updateLastSyncTime($model_name);
}
}
}
}
我正在使用“phpxmlrpc/phpxmlrpc”:“^4.10”包
任何人都可以帮我进行身份验证并从 odoo 获取票证和任务数据
鉴于错误消息和代码,看起来像在
new Value($fields, 'array')
方法中的 fetchData
行中,$fields
是一个纯 php 字符串数组。它应该是一个由 XmlRpc\Value
对象组成的数组。
您可以手动进行转换,或者通过使用
XmlRpc\Value
方法将本机 php 值递归包装到 XmlRpc\Encoder::encode()
对象中来简化代码,如 f.e. 所示。在 https://github.com/gggeek/phpxmlrpc/blob/master/demo/client/proxy.php