使用XMLRPC

问题描述 投票:0回答:1

我能够进行身份验证,但是当试图获取数据时,它会给我一个错误

enter image description here

这是我遇到的错误。 我正在使用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进行身份验证并获取门票和任务数据?
    

the错误消息和代码,在

new Value($fields, 'array')

方法中的行中看起来像是在
php laravel odoo laravel-9 xml-rpc
1个回答
0
投票
方法中,

$fields

是普通PHP字符串的数组。它应该是
XmlRpc\Value
对象的数组。
您可以手动进行转换,也可以通过使用
XmlRpc\Value
方法将本机PHP值递归包装到
XmlRpc\Encoder::encode()
对象中来简化您的代码,如Sew f.e.在
https://github.com/gggeek/phpxmlrpc/blob/master/demo/client/proxy.php

	
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.