0

I am able to authenticate but when trying to get data its giving me error

enter image description here

This is the error I am getting

Is there anyone who can help me to resolve this issue quickly please.

I am using Laravel version 9.46, check using via 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);
            }
        }
    }
}

I am using "phpxmlrpc/phpxmlrpc": "^4.10" package

Can anyone help me get authenticate and get the tickets and tasks data from odoo

0

0

Browse other questions tagged or ask your own question.